php settting $_GET value

偶尔善良 提交于 2019-12-25 08:33:06

问题


I need to set the $_GET values; can I do this?

ie I have an empty $_GET array. How can make it nonempty with php?

Maybe with help of http headers?

Thank you in advance;


回答1:


It is three ways of doing this.

One:

<form method="get" action="file.php">
<input type="text" name="TextThing" />
</form>

In the php file:

$_GET["TextThing"]; //contains the text of the texbox TextThing...

Second:

Use a questionmark behind the url, like this: http://domain.com/file.php?foo=bar&stack=overflow

Then:

$_GET["foo"]; //contains bar
$_GET["stack"]; //contains overflow

Remember that your users easily can change the values of the variables.

Third:

$_GET["foo"] = "bar";
$_GET["foo"]; //contains bar



回答2:


$_GET is populated with the params/values sent in the querystring of the url, so if you call http://example.com/my.php?foo=bar&foo2=baz then in my.php $_GET['foo'] => 'bar' and $_GET['foo2'] => 'baz'




回答3:


You can try updating $_GET

$_GET['test'] = 'abc';

And then redirect users there:

header('Location: ?' . http_build_query($_GET));

You just have to make sure to redirect only when necessary, so that you don't take users into infinite redirect.




回答4:


The $_GET array is initially populated from the query string, but you can overwrite that if you want to without redirecting. If at some point in your code you set $_GET['foo'] = 'bar'; then that is set thereafter, no matter what the query string is.



来源:https://stackoverflow.com/questions/4680741/php-settting-get-value

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