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 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历