PHP 会话变量不起作用 [英] PHP session variables wont work

查看:46
本文介绍了PHP 会话变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 PHP 学习者,我在使用 PHP 会话时遇到了一些问题.

I am a PHP learner, and I have got some problems with PHP sessions.

我正在尝试做一个简单的登录测试,但是当我定向到另一个页面时,会话变量似乎不会跟随.两个重要文件:

I am trying to do a simple login test, but it seems as session variables wont follow when I direct to another page. The two important files:

检查登录:

 <html>
<head>
    <title>
        <?php
            session_start();
        ?>
    </title>
</head>
<body>
    <?php

        $connection = mysql_connect("localhost", "root", "root");
        if(!connection)
        {
            die("could not connect to the database");
        }
        mysql_selectdb("phplogin", $connection) or die ("MySQL error i valg af database: " .mysql_error());
        $query = "Select * from users where username='".$_POST['username']."' AND password = '".$_POST['userpass']."'";
        $result = mysql_query($query);
        $count = mysql_num_rows($result);
        if($count==1){
            $_SESSION['loggedIn'] = "true";
            echo "The variable: ".$_SESSION['loggedIn'];
            header("Location: loggedinPage.php");
            exit;
        }
        if(!$count == 1){
            header("Location:Login.php");
            exit;
        }
    ?>
</body>

和访问页面:

 <html>
<head>
    <title>
        <?php
            session_start();
        ?>
    </title>
</head>
<body>
    <?php
        if($_SESSION['loggedIn'] != "true"){
            echo "You are NOT logged in";
            echo $_SESSION['loggedIn'];
            exit;
        }
            echo $_SESSION['loggedIn'];
            echo "You are logged in";
    ?>
</body>

即使您提供了正确的用户名和密码,当被定向到新页面时,它仍然表示您尚未登录.另一件事是标题(位置:等)",这不起作用,我必须手动重定向.

Even though you give correct user and password, it still says that you are NOT logged in when directed to the new page. Another thing is the "header(Location: etc. etc)", this wont work, I have to redirect manually.

有什么帮助:)?- 大卫

Any help :)? - David

谢谢,我现在登录可以工作了,但是重定向仍然不起作用?我的文件现在看起来像这样:

Thanks, I got the logging in to work now, but the redirection still wont work? my file looks like this now:

 <?php
    session_start();
    ?>
    <?php

        $connection = mysql_connect("localhost", "root", "root");
        if(!connection)
        {
            die();
        }
        mysql_selectdb("phplogin", $connection) or die ("MySQL error i valg af database: " .mysql_error());
        $query = "Select * from users where username='".$_POST['username']."' AND password = '".$_POST['userpass']."'";
        $result = mysql_query($query);
        $count = mysql_num_rows($result);
        if($count==1){
            $_SESSION['loggedIn'] = "true";
            header("Location: loggedinPage.php");
        }
        if(!$count == 1){
            header("Location:Login.php");
        }
    ?>

<head>
    <title>

    </title>
</head>
<body>

</body>

推荐答案

session_start() 应该在文档的最顶部,header() 应该只在在网站上发生任何输出之前执行.

session_start() should be at the very top of your document and header() should only be executed before any output has taken place on the website.

这篇关于PHP 会话变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆