簡體   English   中英

使用$ _GET時,$ _ SESSION變量不會持續到第一頁之外

[英]$_SESSION variable does not persist beyond first page when using a $_GET

我已經在許多論壇上搜索了有關此問題的許多答案。 許多人表示一定要使用session_start()其他人來處理托管平台,甚至有人說他們使用過$_GET並且session變量不會在第一頁之后持續存在,但是沒有確定的解決方案似乎符合我的情況。

我已經調試它到我知道它在$_GET ,並且如果我使用$_REQUEST (在這一點上沒有帖子)也做同樣的事情,所以我希望能對此問題有所解決。 這是我的代碼,出於發布目的進行了簡化,但是它確實有效,並演示了$_SESSION['test']變量如何持久化,但是由$_GET實例化的$_SESSION['sp']變量卻沒有。

index2.php

<?php
/*some comments*/
session_start();
session_unset();

//define some variables here

?>

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
    <tr>
        <td width-"750">
        <p>
            Make a choice:<br><br>
            <a href="first_page.php?page=third_page">Get the third page after first and second pages</a><br>
            <a href="first_page.php?page=fourth_page">Get the fourth page after first and second pages</a><br>
        </p>
        </td>
    </tr>
</table>
</body>
</html>

first_page.php

<?php
/*some comments*/
session_start();

$s=$_GET['page'];
$_SESSION['sp']=$s;
$_SESSION['test']="test123";

echo "s variable from get is set to " . $s . "<br>";
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";

if (isset($_POST['submit']) && !empty($_POST['submit']))
{
    header('Location: second_page.php');
}

?>

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
    <tr>
        <td width="750px">
            <form method="post" action='first_page.php'>
                <p>
                    HTML text and a select tag here for choices
                </p>
                <input type="submit" name="submit" value="Submit">
            </form>
        </td>
    </tr>
</table>
</body>
</html>

second_page.php

<?php
/*some comments*/
session_start();

echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";


//Test if submit button named submit was clicked and not empty
if (isset($_POST['submit']) && !empty($_POST['submit']))
{
        if($_SESSION['sp']=="third_page") {
            header('Location: third_page.php');
        }
        if($_SESSION['sp']=="fourth_page") {
            header('Location: fourth_page.php');
        } else {
            header('Location: index2.php');
        }
}

?>

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
    <tr>
        <td width="750px">
            <form method="post" action='second_page.php'>
                <p>
                    HTML text and a select tag here for choices
                </p>
                <input type="submit" name="submit" value="Submit">
            </form>
        </td>
    </tr>
</table>
</body>
</html>

在這種“禿頂”情況下的任何幫助將不勝感激!

一旦發送了輸出(例如,通過echo header()就無法修改header()

您可能要在first_page.php中注釋掉這些行

echo "s variable from get is set to " . $s . "<br>";
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";

而這些在second_page.php中:

echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";

取自php.org :注意:

即使啟用了session.use_trans_sid,會話ID也不會隨Location標頭一起傳遞。 必須使用SID常量手動傳遞它。

您可能希望將會話ID存儲在cookie中(或將其通過查詢字符串傳遞)。

解決:

感謝大家的協助!

表單操作是在重定向到second_page之前,再次調用first_page,並且在提交表單時,get為空白,這很有意義。 如果有人遇到此問題,這里是解決方案。 我已經保留了一些調試功能來幫助其他人,同時它也幫助了我。

index2.php-

<?php
session_start();
?>

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
    <tr>
        <td width-"750">
        <p>
            Make a choice:<br><br>
            <a href="first_page.php?page=third_page">Get the third page after first and second pages</a><br>
            <a href="first_page.php?page=fourth_page">Get the fourth page after first and second pages</a><br>
        </p>
        </td>
    </tr>
</table>
</body>
</html>

first_page.php-

<?php error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();

// is the page parameter present at all?
if (!isset($_GET['page']))
{
    http_response_code(400);
    exit('Missing URL parameter: page');
}

/* is the parameter valid?  - this will fail if the page= is not all numeric
if (!ctype_digit($_GET['page']) || $_GET['page'] == 0)
{
    http_response_code(400);
    exit('Invalid URL parameter: page');
}
*/


$s=$_GET['page'];
$_SESSION['sp']=$s;
$_SESSION['test']="test123";

echo "s variable from get is set to " . $s . "<br>";
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";

if (isset($_POST['submit']) && !empty($_POST['submit']))
{
    header('Location: second_page.php');
}

?>

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
    <tr>
        <td width="750px">
            <form method="post" **action="first_page.php?page=<?php echo $_SESSION['sp']?>">**
                <p>
                    HTML text and a select tag here for choices
                </p>
                <input type="submit" name="submit" value="Submit">
            </form>
        </td>
    </tr>
</table>
</body>
</html>

second_page.php-

<?php error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();

echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";

if (isset($_POST['submit']) && !empty($_POST['submit']))
{
        if($_SESSION['sp']=="third_page") {
            header('Location: third_page.php');
        } else {
            if($_SESSION['sp']=="fourth_page") {
                header('Location: fourth_page.php');
            } else {
                header('Location: index2.php');
            }
        }

}

?>

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
    <tr>
        <td width="750px">
            <form method="post" action="second_page.php">
                <p>
                    HTML text and a select tag here for choices
                </p>
                <input type="submit" name="submit" value="Submit">
            </form>
        </td>
    </tr>
</table>
</body>
</html>

third_page.php-

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
Congrats, you made it to the third page!
</body>
</html>

four_page.php-

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
    Congrats, you made it to the fourth page!
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM