PHP 中的 $_REQUEST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29195602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:18:05  来源:igfitidea点击:

$_REQUEST in PHP

phprequest

提问by Sachin Yadav

I have this code.

我有这个代码。

$message = "";

if($_REQUEST['msg'] == "new"){
    $message = "New User has been added successfully";
}else if($_REQUEST['msg'] == 'edit'){
    $message = "User has been saved successfully";
}else if($_REQUEST['msg'] == 'update'){
    $message = "User(s) has been Updated successfully";
}

can any one please tell me here what is ['msg']and please explain the functioning of $_REQUEST?

任何人都可以在这里告诉我什么是['msg']并请解释 $_REQUEST 的功能吗?

回答by Quentin Unknown

$_REQUEST is a super global array. Just like $_GET, $_POST, $_COOKIE, $_SESSION etc. That means it can store a list information numerically or associatively.

$_REQUEST 是一个超级全局数组。就像 $_GET、$_POST、$_COOKIE、$_SESSION 等。这意味着它可以以数字或关联方式存储列表信息。

For example: Associative: $array = array(key->value, key->value);Numeric: $array = array([0]->value, [1]->value);

例如: 关联: $array = array(key->value, key->value);数字: $array = array([0]->value, [1]->value);

In the case of $_REQUEST or $_POST or $_GET these arrays will store encoded data sent to the PHP header.

在 $_REQUEST 或 $_POST 或 $_GET 的情况下,这些数组将存储发送到 PHP 标头的编码数据。

for example: $_REQUEST['key'] = value;

例如: $_REQUEST['key'] = value;

or

或者

you have a navigation item: <a href='?key=value'>value</a> //for $_GET

你有一个导航项目: <a href='?key=value'>value</a> //for $_GET

PHP will encode that key->value into the url and save it to the super global array that you are using. To access it call: echo $_REQUEST['key']; //returns 'value'

PHP 会将该键-> 值编码到 url 中,并将其保存到您正在使用的超级全局数组中。要访问它,请调用: echo $_REQUEST['key']; //returns 'value'

In your case msg is, so far, not encoded to the browser. It needs to be passed by different means(forms, href's etc.). So,

在您的情况下, msg 到目前为止还没有编码到浏览器中。它需要通过不同的方式(表单、href 等)传递。所以,

 $_REQUEST['msg'] = 'new';
 if(isset($_REQUEST['msg'])){       //use isset() to avoid an error
    if($_REQUEST['msg'] == "new"){
        $message = "New User has been added successfully";  
    }else if($_REQUEST['msg'] == 'edit'){
        $message = "User has been saved successfully";
    }else if($_REQUEST['msg'] == 'update'){
        $message = "User(s) has been Updated successfully";
    }
}                           //returns $message = "New user..."

$_REQUEST is not suggested because it makes it hard to control what information is processed. $_GET requests show the key->value pairs in the url. Information that you don't want as visible probably shouldn't be shown there. With $_REQUEST a user can send that key->value pair over the url to see what information needs to be passed and exploit that in other ways (google cross-site request forgeries).

不建议使用 $_REQUEST,因为它很难控制要处理的信息。$_GET 请求在 url 中显示键-> 值对。您不希望显示的信息可能不应该显示在那里。使用 $_REQUEST 用户可以通过 url 发送该键-> 值对以查看需要传递哪些信息并以其他方式利用它(谷歌跨站点请求伪造)。

TL;DR : $_REQUEST['msg'] -- 'msg' is a key in a key->value pair ('new'| 'edit' | 'update' being the value)

TL;DR : $_REQUEST['msg'] -- 'msg' 是键->值对中的一个键('new'| 'edit' | 'update' 是值)

$_REQUEST is a superglobal array that saves values that can be used by the user in any scope in other parts of the website.

$_REQUEST 是一个超全局数组,用于保存用户可以在网站其他部分的任何范围内使用的值。

回答by Ashutosh Nigam

$_REQUEST contains values passed by post,get and/or cookies. As get is easy to hack so safer mechanism would be to use post when send data from one html/php file to another. Then you need to use $_POST to get the data. More detail you can find from this link.

$_REQUEST 包含通过 post、get 和/或 cookie 传递的值。由于 get 很容易被破解,因此更安全的机制是在将数据从一个 html/php 文件发送到另一个文件时使用 post。然后你需要使用 $_POST 来获取数据。您可以从此链接找到更多详细信息。

So in your case previous html page has used either of the techniques to use a variable/parameter/cookie named msg to pass data.

因此,在您的情况下,之前的 html 页面使用了其中一种技术来使用名为 msg 的变量/参数/cookie 来传递数据。

回答by Mihai Enescu

The $_REQUEST['msg'] is a key from the superglobal array. Basically $_REQUEST will access it even if the variable was sent through $_POST or $_GET :

$_REQUEST['msg'] 是来自超全局数组的键。即使变量是通过 $_POST 或 $_GET 发送的,基本上 $_REQUEST 也会访问它:

$_POST : $_GET : page.php?msg=testMsg

$_POST : $_GET : page.php?msg=testMsg