dongpang2483 2014-10-27 18:20
浏览 39
已采纳

PHP Session变量没有结转

I am working with PHP session variables, but the problem is that the session variables are not carrying over. I checked using Firebug, and found that the session variables are not being created at all. print_r($_SESSION); returns an empty array after the page returns to index.php, but displays it correctly before the refresh. 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>

Please help.

  • 写回答

2条回答 默认 最新

  • douyi1966 2014-10-27 18:40
    关注

    I think the under score is missing in on test.php page in $_SESSION['loggedin']=true; change this to $_SESSION['logged_in']=true;

    As you are using this in

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

    there is no error with you session part the error is may be in you db access part check your database connection. you can check you session by commenting the query part, it is working fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效