what is basename(__FILE__) === basename($_SERVER['SCRIPT_FILENAME'])?

54 Views Asked by At

I am working on a PHP code, I saw the following code in the connect.php file

if (basename(__FILE__) === basename($_SERVER['SCRIPT_FILENAME'])) {
   header("Location: https://example.com/");
   exit();
}

This lines are written on the first line of the file, before even making any include, I think it has to do with the http. Can you tell me what is the programmer trying to do here? I am new to PHP, so please provide a source if you have.

1

There are 1 best solutions below

3
On

This code is set "Location" http header in the web-server response which redirects the client to the "https://example.com" and interrupt the execution of the script.

It happens only if the file connect.php requested directly, i.e not included from another one.

I believe this code might be written in order to prevent any dangerous requests to be processed.