为什么我的会话没有储蓄?

时间:2015-07-05 13:37:06

标签: php session

<html>
<head lang="en">
    <meta charset="UTF-8">
    <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
    <title>Admin Login</title>
</head>
<style>
    .login-panel {
        margin-top: 150px;

</style>

<body>

<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-success">
                <div class="panel-heading">
                    <h3 class="panel-title">Sign In</h3>
                </div>
                <div class="panel-body">
                    <form role="form" method="post" action="admin_login.php">
                        <fieldset>
                            <div class="form-group"  >
                                <input class="form-control" placeholder="Name" name="admin_name" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" name="admin_pass" type="password" value="">
                            </div>


                            <input class="btn btn-lg btn-success btn-block" type="submit" value="login" name="admin_login" >


                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


</body>

</html>

<?php
include("database/db_conection.php");

if(isset($_POST['admin_login']))//this will tell us what to do if some data has been post through form with button.
{
    $admin_name=mysqli_real_escape_string($dbcon, $_POST['admin_name']);
    $admin_pass=mysqli_real_escape_string($dbcon, $_POST['admin_pass']);

    $admin_query="select * from users WHERE user_name='$admin_name' AND user_pass='$admin_pass' AND admin='true'";

    $run_query=mysqli_query($dbcon,$admin_query);

    if(mysqli_num_rows($run_query))
    {
        echo "<script>window.open('welcome.php','_self')</script>";
        $_SESSION['adminName']=$admin_name;
        $_SESSION['isAdmin']='true';
    }
    else {echo"<script>alert('Admin Details are incorrect..!')</script>";}

}

?>

这是来自我的admin_login.php页面,db_connection.php内部只是连接到用户数据库的代码

我已经尝试将SESSION变量放在echo之上welcome.php,但是甚至没有效果。

如果您仍然感到困惑,$_SESSION['adminName']=$admin_name;$_SESSION['isAdmin']='true';设置不正确,我也不知道原因。

1 个答案:

答案 0 :(得分:2)

你必须调用函数

session_start();

在使用$_SESSION全局变量之前的某个地方。 Here's the documentation about it