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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)