$_REQUEST in PHP

拟墨画扇 提交于 2021-02-05 06:53:56

问题


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?


回答1:


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

For example: Associative: $array = array(key->value, key->value); Numeric: $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.

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

or

you have a navigation item: <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'

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 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' is a key in a key->value pair ('new'| 'edit' | 'update' being the value)

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




回答2:


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

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




回答3:


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 :

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



来源:https://stackoverflow.com/questions/29195602/request-in-php

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