PHP $ _SESSION问题

时间:2015-10-16 21:00:11

标签: php mysql session session-variables

我遇到了$ _SESSION的问题,我会粘贴代码并解释。

我有一个包含

的索引
<div class="confidential__field text-align-center">
    <table>
        <tbody>
            <tr>
                <td><label for="email">Email</label></td>
                <td><input type="text" id="email" name="email"></input></td>
            </tr>
            <tr>
                <td><label for="pwd">password</label></td>
                <td><input type="password" id="pwd" name="pwd"></input></td>
            </tr>
            <tr>
                <td><a href="?action=register">Don't have an account ?</a></td>
                <td><button form="form" id="login">Send</button></td>
            </tr>
        </tbody>
    </table>
</div>

index.php我们有这个(缩短版本,是的,我在index.php的开头有session_start()):

<?php 
session_start();
include("_include/_stuff/config.php");?>
<!doctype html>
<html>
    <?php include("_general/head.php");?>
    <body>
         <?php 
        include("_include/login.php");
        if(isset($_SESSION['ID']) && !empty($_SESSION['ID']))include("_include/header.php");
    ?>
</body>
</html>

然后是脚本:

$(document).ready(function(){
        $("#login").click(function(){
                            $.post("_ajax/clientMain.php",{
                                login: 1,
                                email: $("input#email").val(),
                                pwd: $("input#pwd").val()
                            },
                                function(data){
                                    if(data==1){
                                        //alert(data);
                                        // alert("SESSION SET");
                                        location="index.php";
                                    }else{
                                        alert(data);
                                    }
                                }
                            )
                        });
});

然后检查实际的php:

session_start();    
if(isset($_POST['login']) && !empty($_POST['login'])){
        $conn = db_connect();

    $email = db_quote($_POST['email']);
    $pwd = db_quote($_POST['pwd']);

    $query_attendance = mysqli_query($conn, "select * from user where email=$email and password=$pwd");
    if(mysqli_num_rows($query_attendance) > 0){
        $credentials = mysqli_fetch_array($query_attendance, MYSQLI_ASSOC);
        if(!empty($credentials)){
            $_SESSSION['ID'] = $credentials['userID'];
            echo 1;
        }else{
            echo 82;
        }
    }else{
        echo 45;
    }
}else{
    echo 111;
}

我在Windows 10上运行XAMPP(localhost)。

注意:我没有编码密码,因为我只是在进行基本的测试,我几乎不会感到惊讶!

编辑:在将ID添加到会话后,会话不会保存到index.php,因为如果我执行var_dump($ _ SESSION)或echo $ _SESSION [&#34; ID&#34;]我什么也没收到,但是如果我在登录脚本中回复数据而不是echo 1我会echo $_SESSION["ID"]那么我会得到会话/ ID,但它不会以某种方式被转移到index.php页面!

1 个答案:

答案 0 :(得分:3)

您有输入错误,请检查您的问题代码或实际代码中是否只有这样:

$_SESSSION['ID'] = $credentials['userID'];

应该是:

$_SESSION['ID'] = $credentials['userID'];

您还有一个S