2

status最初のスクリプトで設定した配列から変数を取得しようとすると$_REQUEST[](そしてリダイレクトを実行すると)、 warning しか表示されませんUndefined index: status。何故ですか ?

<?php
        $_REQUEST['status'] = "success";
        $rd_header = "location: action_script.php";
        header($rd_header);
?>

アクションスクリプト.php

<?php
echo "Unpacking the request variable : {$_REQUEST['status']}";
4

3 に答える 3

4

あなたが探しているのはセッションです:

<?php
    session_start();
    $_SESSION['status'] = "success";
    $rd_header = "location: action_script.php";
    header($rd_header);
?>

<?php
    session_start();
    echo "Unpacking the request variable : {$_SESSION['status']}";

session_start()両方のページの上部に が追加されていることに注意してください。私が投稿したリンクを読むとわかるように、これは必須であり、セッションを使用するすべてのページにある必要があります。

于 2013-03-17T16:41:41.293 に答える
4

これは、header()ステートメントがユーザーを新しい URL にリダイレクトするためです。同じページにいないため、$_GETまたは$_POSTパラメーターはもう存在しません。

いくつかのオプションがあります。

$_SESSION1- まず、ページのリダイレクト全体でデータを永続化するために使用できます。

session_start();
$_SESSIONJ['data'] = $data; 
// this variable is now held in the session and can be accessed as long as there is a valid session.

2-リダイレクト時にURLにいくつかのgetパラメーターを追加します-

$rd_header = "location: action_script.php?param1=foo&param2=bar";
header($rd_header);
// now you'll have the parameter `param1` and `param2` once the user has been redirected.

2 番目の方法については、このドキュメントが役立つ場合があります。という配列からクエリ文字列を作成するメソッドhttp_build_query()です。

于 2013-03-17T16:46:45.737 に答える
2

あなたが探しているのは、おそらく GET パラメータを送信することです:

$rd_header = "Location: action_script.php?status=success";
header($rd_header);

経由で取得できますaction_script.php

$_GET['status'];

この場合、実際にはセッションや Cookie は必要ありませんが、ユーザーが GET 投稿を簡単に編集できるという事実を考慮する必要があります。

于 2013-03-17T16:46:43.870 に答える