PHP 会话不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5849898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:41:11  来源:igfitidea点击:

PHP Sessions Not working

phpsessionsession-variables

提问by Josh

I just can't get my variables to carry over to the next page. The variable assignment works fine on the initial page (tested), but the value goes null when going to the next. Plz Help!

我只是无法让我的变量延续到下一页。变量赋值在初始页面(已测试)上工作正常,但在进入下一个页面时该值变为 null。请帮忙!

Page 1:

第 1 页:

<?PHP session_start(); ?>
<HTML>
<HEAD>
    <META NAME='robots' CONTENT='noindex,nofollow'>
    <TITLE>Master Chief's Gamestore</TITLE>
    <STYLE type="text/css">
        BODY {
            color: #ffffff;
            background: url('~/halo-reach.jpg');
            background-repeat: no-repeat;
            background-position: top;
            background-color: black;
        }
    </STYLE>
</HEAD>
<body vLink='#3366CC' link='#3366CC' alink='#3366CC'>
<br/><br/><br/><br/><br/>
<table width='100%' height='80%'>
    <tr>
        <td align='center' valign='middle'>
            <table width="50%" cellspacing="0" cellpadding="25" border="0">
                <form method="post">
                    <tr bgcolor="#666633">
                        <th id="header" align="left" colspan="3">
                            <DIV align="center">
                                <font face="Verdana, Arial, Helvetica" color="white" size="3">
                                    &nbsp; Master Chief's Gamestore </font>
                            </DIV>
                        </th>
                    </tr>
                    <tr bgcolor="#A3A385">
                        <td width="100%" valign="middle" colspan="2">
                            <font face="Verdana, Arial, Helvetica" size="2">
                                <div align="center"><br/><br/>
                                    Username:&nbsp
                                    <input type="text" name="username">&nbsp
                                    Password:&nbsp
                                    <input type="password" name="password"><br/>
                                    <br/><input type="submit" name="act" value="Login"><br/>
                                    <input type="submit" name="act" value="Register"><br/>
                                    <input type="submit" name="act" value="Main"><br/> <br/><br/>
                                </div>
                            </font>
                        </td>
                    </tr>
                    <?PHP
                    if ($_POST['act'] == "Login") {
                        mysql_connect("~", "~", "~") or die("unable to connect to server");
                        mysql_select_db("~") or die("unable to select database");
                        $user = $_POST['username'];
                        $pass = $_POST['password'];
                        $query = "select * from users where user like '%" . $user . "%' and pass like '%" . $pass . "%';";
                        $result = mysql_query($query);
                        $rows = mysql_numrows($result);
                        if ($rows == 0 || strlen($user) == 0) {
                            echo "<br />Login Failure";
                        } else {
                            $_SESSION['user'] = mysql_result($result, 0, "user");
                            $_SESSION['id'] = mysql_result($result, 0, "P_Id");
                            header('Location: ~/success.php');
                        }
                    }
                    if ($_POST['act'] == "Register") {
                        header('Location: ~/register.php');
                    }
                    if ($_POST['act'] == "Main") {
                        header('Location: ~/index.php');
                    }
                    ?>
                    <div align="center">
                        <tr bgcolor="#666633">
                            <td align="left">
                                Logged in as:
                                <?PHP
                                $user = $_SESSION['user'];
                                if (!$user) {
                                    $user = 'Guest';
                                }
                                echo $user;
                                ?>
                            </td>
                            <td align="right">
                                Password:&nbsp
                                <input type="password" name="srcpw">
                                <input type="submit" name="dspphp" value="Display PHP">
                            </td>
                        </tr>
                    </div>
                </form>
            </table>
        </td>
    </tr>
</table>
</body>
</html>

Page 2:

第2页:

<?PHP session_start(); ?>
<HTML>
<HEAD>
    <META NAME='robots' CONTENT='noindex,nofollow'>
    <TITLE>Master Chief's Gamestore</TITLE>
    <STYLE type="text/css">
        BODY {
            color: #ffffff;
            background: url('~/halo-reach.jpg');
            background-repeat: no-repeat;
            background-position: top;
            background-color: black;
        }
    </STYLE>
</HEAD>
<body vLink='#3366CC' link='#3366CC' alink='#3366CC'>
<br/><br/><br/><br/><br/>
<table width='100%' height='80%'>
    <tr>
        <td align='center' valign='middle'>
            <table width="30%" cellspacing="0" cellpadding="25" border="0">
                <form method="post">
                    <tr bgcolor="#666633">
                        <th id="header" align="left" colspan="3">
                            <DIV align="center">
                                <font face="Verdana, Arial, Helvetica" color="white" size="3">
                                    &nbsp; Master Chief's Gamestore </font>
                            </DIV>
                        </th>
                    </tr>
                    <tr bgcolor="#A3A385">
                        <td width="100%" valign="middle" colspan="2">
                            <font face="Verdana, Arial, Helvetica" size="2">
                                <div align="center">
                                    <br/><br/>Success<br/><br/>
                                    <input type='submit' name='main' value='Main'>
                                    <?PHP
                                    if ($_POST['main'] == "Main") {
                                        header('Location: http://~/index.php');
                                    }
                                    if ($_POST['act'] == "Display Code") {
                                        if ($_POST['pw'] == "pass") {
                                            highlight_file("success.php");
                                        }
                                    }
                                    ?>
                                </div>
                            </font>
                        </td>
                    </tr>
                    <div align="center">
                        <tr bgcolor="#666633">
                            <td align="left">
                                Logged in as:
                                <?PHP
                                $user = $_SESSION['user'];
                                if (!$user)
                                    $user = 'Guest';
                                echo $user;
                                ?>
                            </td>
                            <td align="right">
                                Password:&nbsp
                                <input type="password" name="pw">
                                <input type="submit" name="dspphp" value="Display PHP">
                            </td>
                        </tr>
                    </div>
                </form>
            </table>
        </td>
    </tr>
</table>
</body>
</html>

EDIT: I've tried this sample code and it does NOT work.

编辑:我已经尝试过这个示例代码,但它不起作用。

Page 1:

第 1 页:

<?php
session_start();
if (isset($_GET['link'])) {
    $_session['myvariable'] = 'Hello World';
    header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php');
    exit;
}
?>
<a href="<?php print $_SERVER['REQUEST_URI'] . '?link=yes'; ?>">Click Here</a>

Page 2:

第2页:

<?php
session_start();
print 'Here is page two, and my session variable: ';
print $_session['myvariable'];
exit;
?>

But as I posted below, I have used sessions in other pages successfully on the same server. Soo frustrating....

但是正如我在下面发布的那样,我在同一台服务器上成功地使用了其他页面中的会话。好郁闷....

Thanks for all the posts btw!

感谢所有的帖子 btw!

回答by Thilo

Make sure cookies are enabled so the second page actually re-creates the session of the first page instead of creating a new, empty one. Check out http://www.php.net/manual/en/session.idpassing.phpfor details.

确保启用 cookie,以便第二页实际上重新创建第一页的会话,而不是创建一个新的、空的。查看http://www.php.net/manual/en/session.idpassing.php了解详情。

EDIT

编辑

Another possible cause for sessions not working as expected, especially if it only affects certain pages, is that you have accidental whitespace or other output before your first 'session_start() error

会话未按预期工作的另一个可能原因,特别是如果它只影响某些页面,是您在第一个 'session_start() 错误之前有意外的空格或其他输出

回答by Mikecito

It looks like you are just redirecting the user to the second page without posting anything. You would want to set hidden variables and actually submit your form to the second page. Re-directing will lose all the post variables.

看起来您只是将用户重定向到第二页而不发布任何内容。您可能希望设置隐藏变量并实际将表单提交到第二页。重定向将丢失所有后期变量。

If you want to send variables to the second page with a redirect, you can use session variables or a GET variable passed in with the URL to the second page.

如果您想通过重定向将变量发送到第二个页面,您可以使用会话变量或通过 URL 传入到第二个页面的 GET 变量。

回答by Thomas

Try using

尝试使用

$_SESSION['myvariable'] = 'Hello World'; 

in upper case

大写