PHP Automatically “GET” Variables

2019-07-13 10:24发布

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?

4条回答
Root(大扎)
2楼-- · 2019-07-13 11:02

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

查看更多
够拽才男人
3楼-- · 2019-07-13 11:12

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楼-- · 2019-07-13 11:18

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.

查看更多
孤傲高冷的网名
5楼-- · 2019-07-13 11:25

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.

查看更多
登录 后发表回答