沃梦达 / 编程问答 / php问题 / 正文

PHP 中的 $_REQUEST

$_REQUEST in PHP(PHP 中的 $_REQUEST)

本文介绍了PHP 中的 $_REQUEST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码.

$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";
}

谁能在这里告诉我什么是['msg']并解释$_REQUEST的功能?

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

推荐答案

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

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

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

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

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

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

您有一个导航项:value//对于$_GET

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

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

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'

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

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,

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

$_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).

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

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

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

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

这篇关于PHP 中的 $_REQUEST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP 中的 $_REQUEST

基础教程推荐