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

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序