Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

PHP session variables wont work

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:

Check login:

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

and the acces page:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...