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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化