0

First of all, I heard some web-servers allow you to reach parameter with $a instead of $_GET[a], this is not the case here.

Anyway, I have to reach a multiple times, so instead of doing $a = $_GET[a], I instead use $_GET[a] everytime. In single php tag as in <?php ?>, is that an issue, should I absolutely use variables? does it matter?

Another thing is my php file is really scrambled in my html, I wonder if does it matter with multiple gets?(should not, im just worried)

Thanks.

0

3 Answers 3

1

What you refer of using just $a instead of $_GET['a'] (or $_POST['a'] too) is an old feature known as register_globals. This feature was dangerous and leading to messy code, so it was considered deprecated in PHP 5.3 and finally removed in PHP 5.4.

Then, using $_GET['a'] everywhere in your scripts may lead to problems, because you should never trust user input (all things coming from $_GET, $_POST, $_REQUEST, $_COOKIE and some from $_FILES or $_SERVER). It is recommended to do something like $a = sanitize($_GET['a']); (the sanitize function does not exist, depending on what type of value are you expecting, you should check that what you get is an integer, or a valid date, or whatever, depending on your needs). From now on you should stop referencing $_GET['a'] and use instead the new sanitized variable you have just created $a. Because if you were using always $_GET['a'], chances are that you forget to sanitize it someplace.

Also, before sending this sanitized variable into a SQL query, you should escape it or use it inside a prepared statement to avoid SQL injections. Before outputting it to an html for the user to see, use htmlspecialchars to avoid XSS attacks.

And finally, about having multiple php blocks mixed with html blocks, this is only bad for maintenance reasons, because in the long run it will be a complete mess. Try to separate the html you send the user from the php code. Try to read something about the MVC pattern (Model-View-Controller) (this link is probably too complicated or maybe you don't see the utility right now for you that are just beginning with php (at least I didn't see how it was way better than mixing html with php, for all the complexity needed), but try to grasp the idea behind it) .

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

Comments

0

First of all, I heard some web-servers allow you to reach parameter with $a instead of $_GET[a], this is not the case here.

This is a PHP config setting called register_globals. It is insecure and should NOT be used. See this question for more information.

You can access an element in the $_GET array as many times as you like, it will not cause problems. However if you are printing an element of the $_GET array (or any other user submitted data) to the page, you should run it through htmlspecialchars() or the like before printing it out to prevent XSS vulnerabilities.

Comments

0

using a variable is a preference for you to decide it does not matter. but variable is the way forward if you use the same one multiple times.

<?php echo htmlspecialchars($_GET['a']);?>

using a variable means that it reusable again especially if you have added extra code, which mean just editing one variable for all instances.

<?php $a = htmlspecialchars($_GET['a']);

echo $a;
echo $a;
echo $a;
echo $a;

?>

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.