PHP Automatically “GET” Variables

僤鯓⒐⒋嵵緔 提交于 2019-12-10 18:43:58

问题


I am desiging a new website for my company and I am trying to implement switch navigation which is what I have used on all my sites in the past.

<?php
switch($x) {

default:
include("inc/main.php");
break;

case "products":
include("inc/products.php");
break;

}
?>

For some reason when I go to index.php?x=products nothing happens, it still displays inc/main.php, in other words it hasn't detected the X variable from the URL. Is this something to do with global variables?


回答1:


Yes, your PHP configuration has correctly got register_globals turned off, because that's incredibly insecure.

Just put:

$x = $_REQUEST['x']

at the top of your script.

You can also use $_GET if you specifically only want this to work for the GET HTTP method. I've seen some people claim that $_REQUEST is somehow insecure, but no evidence to back that up.




回答2:


It seems like your previous webhosts all used register_globals and your code relies on that. This is a dangerous setting and was rightfully removed in PHP 6.0! Use switch($_GET['x']) { instead.




回答3:


You should use $_GET to read out these variables. There is a deprecated function called register_globals, but I would definately not advise to use this, as it is a potential security risk.




回答4:


You can use http://php.net/manual/es/function.extract.php to extract the variables if you want to do it, but keep in mind this lets any user set variables with the content they want in your script, which makes it as insecure as using register_globals



来源:https://stackoverflow.com/questions/258397/php-automatically-get-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!