PHP Session变量没有结转

时间:2014-10-27 18:20:58

标签: php session

我正在使用PHP会话变量,但问题是会话变量没有延续。我使用Firebug进行了检查,发现根本没有创建会话变量。 页面返回index.php后,print_r($_SESSION);返回一个空数组,但在刷新之前正确显示它。 logincheck.php:

<?php
session_start();
error_reporting(-1);
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
{
$username=$_SESSION['username'];
?>
<!DOCTYPE html>
<html>
<head>
    <title>Smart Shopping Cart System</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <style type="text/css">
    .show-grid{
        background:#E0E0E0;
        }</style>
</head>
<body>
<?php
include "connection.php"
?>


//      ** Some Content**
</body>
</html>
<?php
}
else
{

    ?>
    <html>
<head>
    <title>Log In | SmartCart</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>

    <div class="navbar navbar-default">
        <div class="container">
            <div class="navbar-brand">
                SmartCart
            </div>

        </div>
    </div>

    <div class="container">
        <br />
        <div class="row">
            <div class="col-md-4">
            </div>
            <div class="col-md-4">
                <form role="form" method="post" name="login" action="test.php">
                    <div class="form-group">
                        <label for="username">Operator Username</label>
                        <input type="username" class="form-control" name="username" placeholder="Enter your username">
                    </div>
                    <div class="form-group">
                        <label for="pass">Password</label>
                        <input type="password" class="form-control" name="pass" placeholder="Password">
                    </div>


                    <button type="submit" class="btn btn-default" name="sub">Submit</button>
                </form>
            </div>
            <div class="col-md-3">
            </div>
        </div>
    </div>

</body></html>
    <?php

}
?>

test.php的

<?php
session_start();
error_reporting(-1);
?>
<html><head><title>Redirecting...</title>
</head>
<body>
Redirecting....
<?php 
include "connection.php";

if(isset($_POST['sub']))
{
    $u_name=$_POST['username'];
    $password=mysql_escape_string($_POST['pass']);
    $password=mysql_escape_string($password);
    $w="select * from operators where operator_username = '$u_name' AND operator_pass = '$password'";
    $b=mysql_query($w) or die(mysql_error()."in query $w");
    $num_rows = mysql_num_rows($b);
    if($num_rows > 0)
    {

        $_SESSION['logged_in']=true;
        $_SESSION['username']=$_POST['username'];
        print_r($_SESSION);
       echo '<meta http-equiv="refresh" content="0; url=index.php">';
    }
    else
    {
         echo "<script>
          alert('Your username or password is incorrect. Please try again'); window.location = 'index.php'; </script>";
    }
}

?>
</body></html>

请帮忙。

2 个答案:

答案 0 :(得分:1)

发现差异:

logincheck:

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
                    ^^^^^^^^^

试验:

    $_SESSION["loggedin"]=true;
               ^^^^^^^^

其中一件事与其他事情不一样......

答案 1 :(得分:0)

我认为test.php页面上缺少得分 在$_SESSION['loggedin']=true; 将其更改为$_SESSION['logged_in']=true;

正如您在

中使用它一样
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)

您的会话部分没有错误,错误可能在您的数据库访问部分检查您的数据库连接。你可以通过评论查询部分检查你的会话,它工作正常。