douye4051 2015-03-22 14:31
浏览 88
已采纳

PHP中的$ _REQUEST

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?

  • 写回答

3条回答 默认 最新

  • doumie6223 2015-03-22 15:11
    关注

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀