简体   繁体   中英

php send and catch data in url without any variable name

passing data between pages in php have ways like $_GET , $_POST , $_REQUEST etc. But if we are to send data without any variable in php, there is a way which I seem to have forgotten.

For example , let a sample url be : www.mysite.com?123

Is it possible to catch the value 123 from the corresponding php page ?

any use of $_SERVER here?

You can get it using:

$_SERVER["QUERY_STRING"]

This url: www.mysite.com?123 will provide 123 into $_SERVER["QUERY_STRING"] .

But be careful, if your url is like www.mysite.com?123&re=789 , $_SERVER["QUERY_STRING"] will be 123&re=789 . It catch every thingg after the ? .

试用:

$_SERVER["QUERY_STRING"]

I believe that browsers default to HTTP GET, so the variables would be accessible by $_GET .

If you are trying to get "123", then key($_GET) should suffice. However if there are more parameters, consider looping through the keys of the $_GET array.

echo 'First $_GET variable: ' . key($_GET);

echo "All the $_GET variables:";
print_r( array_keys($_GET) );

easy one, don't forget the $_GET is an array....

so a url like

www.mysite.com?123

$getArrayKeys = array_keys($_GET);

$firstValue = $getArrayKeys[0];  //work with the first param.

var_dump($getArrayKeys); //display all

OUTPUT

array(1) { [0]=> string(6) "123" }

however you're limiting yourself to a single param here... what happens when you need to pass a second param and they are not in the same order?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM