在php中设置cookie并通过ajax运行会话

时间:2018-05-20 10:50:08

标签: php ajax cookies session-cookies setcookie

您好我无法设置cookie并通过php中的ajax运行会话。

这是一个登录页面。

<?php
    include ("includes/main_modal.php");
    require_once('includes/mysqli_connect.php'); //function called

 ?>
 <script>
 //user validation
 var strUser = "";
 var strPsswrd = "";
 var xmlhttp = new XMLHttpRequest();

 function response(e)
 {
    if(e.button == 0)
    {
            if(login.uname.value == "")
            {
                document.getElementById("emailError").innerHTML = "Please enter useremail";
            }
            else if(login.psw.value == "")
            {
                document.getElementById("passwordError").innerHTML = "Please enter password";
            }
            else
            {
                strUser = login.uname.value;
                strPsswrd = login.psw.value;
                xmlhttp.onreadystatechange = change;

                function change()
                {
                    if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
                    {
                        if(xmlhttp.responseText == "true")
                        {
                            location.href="index.php";
                        }
                        else {
                            document.getElementById("passwordError").innerHTML = xmlhttp.responseText;
                        }
                    }
                }
                xmlhttp.open("GET", "loginUser.php?q=" + strUser + "&p=" + strPsswrd, true);
                xmlhttp.send();
            }
    }
 }

这将转到loginUser php文件

<?php
    $q = $_GET["q"];
    $p = $_GET["p"];

    $dbc = mysqli_connect("localhost", "lucy", "abc123", "userDB");
    if(mysqli_connect_error())
    {
        echo "Database cannot be connected", mysqli_connect_error();
        exit();
    }

    $query = "select * from users where (userEmail = '$q'and userPassword = SHA('$p'))";
    $result = mysqli_query($dbc, $query);
    $num = mysqli_num_rows($result);

    if($num != 0)
    {
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

        $_SESSION ['userName'] = $row['userEmail'];
        $name = $row['userEmail'];

        setcookie ("dbMember", $name, time() + 60*60*2, "/"); //last 2 hours

        $login = "true";
        echo $login;
    }
    else
    {
        echo "Username or Password is incorrect. Please try again.";
    }
?>

如果用户信息与数据库匹配,它会将“true”传递回登录页面,然后重新加载索引页面。

<?php
    //This is the main page for the site.
    include("includes/main_modal.php");
?>
<?php
if(isset($_SESSION['userName']) && isset($_COOKIE['dbMember']))
{
    $user = $_SESSION['userName'];
    echo $user;

}
        else {
            echo "goback";
        }
?>

一旦用户进入,它就会运行索引页面但是会话没有启动,我不知道我做错了什么。 main_modal.php在页面的最顶部有session_start();。 先感谢您。

1 个答案:

答案 0 :(得分:1)

在设置会话值之前,您是否使用过session_start();

例如:

<?php
    session_start();

    $q = $_GET["q"];
    $p = $_GET["p"];

    $dbc = mysqli_connect("localhost", "lucy", "abc123", "userDB");
    if(mysqli_connect_error())
    {
        echo "Database cannot be connected", mysqli_connect_error();
        exit();
    }

    $query = "select * from users where (userEmail = '$q'and userPassword = SHA('$p'))";
    $result = mysqli_query($dbc, $query);
    $num = mysqli_num_rows($result);

    if($num != 0)
    {
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

        //set session 
        $_SESSION ['userName'] = $row['userEmail'];
        $name = $row['userEmail'];

        setcookie ("dbMember", $name, time() + 60*60*2, "/"); //last 2 hours

        $login = "true";
        echo $login;
    }
    else
    {
        echo "Username or Password is incorrect. Please try again.";
    }
?>