1

Tha following is working in index.php, but is it correct?

Before the html tag:

$la= array();
$la['index.php'] = 'Start page';

(Actually this is another language library that is included)

Then inside the header:

<title><?php echo $la[$_SERVER['PHP_SELF']];?></title>

For me the part "$la[$_SERVER['PHP_SELF']]" seams strange, but its working. The title is there in my browser. Is it good practice?

2
  • It's ugly but it works. Commented Jun 2, 2016 at 12:33
  • $_SERVER['PHP_SELF'] can be tampered with, so it is not ok (as in good practice). of course we do not know what measures have been taken to make it safe. or not. Commented Jun 2, 2016 at 12:46

2 Answers 2

1

Yes, current code works. If it's good practice is up for debate.

PHP (like many other language) will evaluate the statements in order. Everytime you use the brackets you are really using the arrays index operator where the index acts as the parameter.

Your code will first evaluate the $_SERVER['PHP_SELF'] statement which probably returns 'index.php'. The next call will be $la['index.php'] (since that was what your inner statement returned. This will in turn return the value 'Start page' which is what is sent to the echo.

Sign up to request clarification or add additional context in comments.

Comments

0

There's nothing wrong with your code. The superglobal $_SERVER['PHP_SELF'] holds the name of the current file. It's not very secure because it can be manipulated to execute arbitrary code if you inject it without sanitizing it properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.