English 中文(简体)
Is there a better way of recreating $_SERVER[ QUERY_STRING ]
原标题:

I want to be able to return all of the parameters that are being passed into a specific page using PHP.

$_SERVER[ QUERY_STRING ];

Seems to perform this task adequately, however I have heard many warn against the use of $_SERVER variables due to their sometimes inconsistent nature. So I was wondering if there are any best practice guidelines on creating a string consisting of everything after the ? of a URL.

最佳回答

Some $_SERVER (environment) variables are inconsistent or server-dependent. QUERY_STRING isn t though, it s a basic and well-supported part of the CGI standard from which PHP was built. It s solid.

问题回答

I don t know of any generic way to access the QUERY_STRING except that.

My solution to this is to translate all needed $_SERVER/$_ENV variables into normal ones in a separate file kept in the configuration directory. That way, they are easy to change when needed.

Anyway, "inconsistent nature" refers to the fact that they may vary on different server setups, but not within the same environment.

You can try http_build_query($_GET) :)

Although, if your query is "a[]=1" then it will generate "a[0]=1". Most times it doesn t matter.





相关问题
Global state in asp.net mvc application

Problem: Our web console shows a list of all computers on which our application is installed. Each machine has some identification information that can be associated with it. Simple strings like ...

How to avoid global variables in JavaScript?

We all know that global variables are anything but best practice. But there are several instances when it is difficult to code without them. What techniques do you use to avoid the use of global ...

Is there a better way of recreating $_SERVER[ QUERY_STRING ]

I want to be able to return all of the parameters that are being passed into a specific page using PHP. $_SERVER[ QUERY_STRING ]; Seems to perform this task adequately, however I have heard many ...

"Global" variable scope in PHP

I have a section of code like the following: ---- file.php ---- require_once("mylib.php"); function($a,$b) { $r = $_GLOBALS[ someGlobal ]; echo $r; } ---- mylib.php ---- $_GLOBALS[ ...

热门标签