我创建了一个 AJAX 登录系统,在该系统中我通过 ajax $.post 请求将数据发送到 login.php,并且成功运行。但我想在 login.php 中设置 cookie 我它不起作用为什么我的代码如下 Login.php 用于 ajax 请求
<?php
include ($_SERVER['DOCUMENT_ROOT'] . '/datastore/sqlconnect.php');
$email = $_POST['email'];
$password = CryptoLib::g_encode($_POST['password']);
$query = mysqli_query($con, "SELECT f_name, password from `tablename` WHERE email='$email'");
$data = mysqli_fetch_array($query);
$pass = $data['password'];
if($password == $pass)
{
$cookiedata = CryptoLib::g_encode($data['f_name']);
setcookie('client',$cookiedata);
echo 'success';
}
else
{
echo 'failed';
}
?>
我的登录页面
<script type="text/javascript">
$(document).ready( function() {
$("#sidebarform").submit( function() {
$("#loginbtn").attr("disabled", "disabled");
$("#loginbtn").attr("value", "Logging in...");
event.preventDefault();
$.post('ajaxhandlers/login.php',
$("#sidebarform").serialize(),function( result ) {
if( result == 'success' ) {
$("#msg").html("Logged in Successfully");
window.location="index.php";
}
else {
$("#msg").html("Invalid Username or Password");
$("#loginbtn").attr("value", "Login");
$("#loginbtn").removeAttr("disabled");
}
});
});
});
</script>