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

Hi I'm having trouble to setup cookie and run the session through ajax from php.

this is a login page.

<?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();
            }
    }
 }

and this goes to the loginUser php file

<?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.";
    }
?>

if user info matches with the database it will pass 'true' back to login page, then will reload the index page.

<?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";
        }
?>

It runs the index page as soon as user loges in but the session doesn't start and I don't know what I am doing wrong. main_modal.php has session_start(); at the very top of the page. Thank you in advance.

1个回答



在设置会话值之前是否使用 session_start(); </ code>。</ p>
\ n

例如:</ p>

 &lt;?php 
session_start();

$ q = $ _GET [“q”];
$ p = $ _GET [“p”];

$ dbc = mysqli_connect(“localhost”,“lucy”,“abc123”,“userDB”);
if(mysqli_connect_error())
{
echo“ 数据库无法连接“,mysqli_connect_error();
exit();
}

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

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

//设置会话
$ _SESSION ['userName'] = $ row ['userEmail'];
$ name = $ row ['userEmail'];

setcookie( “dbMember”,$ name,time()+ 60 * 60 * 2,“/”); //最后2个小时

$ login =“true”;
echo $ login;
}
其他

\ {

“用户名或密码不正确。请重试。”;
}

?&gt;
</ code> </ pre>
</ div>

展开原文

原文

Did you using session_start(); before you set value of session.

for example:

<?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.";
    }
?>

dreljie602951
dreljie602951 嗨,我只是尝试过它,它的工作原理! 我使用了我以前工作中的代码,它运行正常,所以我没有意识到我必须使用session_start(); 在页面中。 无论如何,非常感谢你!
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问