PHP 会话不工作

发布于 2024-11-04 02:43:53 字数 8991 浏览 5 评论 0原文

我只是无法将我的变量转移到下一页。变量分配在初始页面(已测试)上工作正常,但在转到下一页时该值变为空。请帮忙!

第 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>

第 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>

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

第 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>

第 2 页:

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

但正如我在下面发布的,我已在同一服务器上的其他页面中成功使用了会话。太令人沮丧了...

顺便说一句,感谢所有帖子!

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!

Page 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">
                                      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: 
                                    <input type="text" name="username"> 
                                    Password: 
                                    <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: 
                                <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:

<?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">
                                      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: 
                                <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:

<?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:

<?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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ま昔日黯然 2024-11-11 02:43:53

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

编辑

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

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.php for details.

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

咽泪装欢 2024-11-11 02:43:53

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

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

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.

吹梦到西洲 2024-11-11 02:43:53

尝试使用

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

大写

Try using

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

in upper case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文