Php ajax的登录问题
我创建了一个登录页面,它将ajax请求发送到php页面进行登录验证。 在那个php页面上,我正在创建会话,并根据登录验证发送响应。Php ajax的登录问题,php,javascript,jquery,ajax,login-attempts,Php,Javascript,Jquery,Ajax,Login Attempts,我创建了一个登录页面,它将ajax请求发送到php页面进行登录验证。 在那个php页面上,我正在创建会话,并根据登录验证发送响应。 若用户经过身份验证,我将从发送ajax的java脚本将其重定向到主页。但在那个主页上,我无法获取会话对象。。。为什么?你能告诉我在主页上检索该会话的解决方案吗?我不确定我的方法是否正确,但这种方法对我有效(我希望我没有忘记任何事情): 下面是my login.php的功能: header('Content-type: text/json'); // here :
若用户经过身份验证,我将从发送ajax的java脚本将其重定向到主页。但在那个主页上,我无法获取会话对象。。。为什么?你能告诉我在主页上检索该会话的解决方案吗?我不确定我的方法是否正确,但这种方法对我有效(我希望我没有忘记任何事情): 下面是my login.php的功能:
header('Content-type: text/json');
// here : import files I need
session_start();
// here : load some parameters into session variables
// here : init mysql connection
// here : get user and password from $_POST and check them against the database
// If the user can't connect, return an error to the client
if ( ! $ok )
{
echo '{ "ok": "N" }';
exit;
}
$_SESSION['user'] = $user;
echo '{ "ok": "O" }';
?>
然后,当我访问另一个php文件时,它是如何开始的:
header('Content-type: text/json');
// again, required files go here
session_start();
if ( ! isset($_SESSION['user'] )) {
echo '{ "ok": "N" }';
exit;
}
$user=$_SESSION['user'];
....
每次ajax调用我都会检查结果是否告诉我用户未连接,如果未连接,则返回登录页面
$.ajax({
type: "POST",
url: "myphp.php",
dataType: "json",
data: somedatatopost,
success: function(pRep){
if (!pRep) {
alert("No response from the server.");
return;
}
if (pRep.ok=="N") {
window.location = '/index.html';
return;
}
// here is where I handle a successful response
}
});
对于登录ajax调用,我有:
[....]
// here is where I handle a successful response
window.location='mynewpage.html'
我不确定我是不是用正确的方法做的,但这种方法对我很有效(我希望我没有忘记任何事情): 下面是my login.php的功能:
header('Content-type: text/json');
// here : import files I need
session_start();
// here : load some parameters into session variables
// here : init mysql connection
// here : get user and password from $_POST and check them against the database
// If the user can't connect, return an error to the client
if ( ! $ok )
{
echo '{ "ok": "N" }';
exit;
}
$_SESSION['user'] = $user;
echo '{ "ok": "O" }';
?>
然后,当我访问另一个php文件时,它是如何开始的:
header('Content-type: text/json');
// again, required files go here
session_start();
if ( ! isset($_SESSION['user'] )) {
echo '{ "ok": "N" }';
exit;
}
$user=$_SESSION['user'];
....
每次ajax调用我都会检查结果是否告诉我用户未连接,如果未连接,则返回登录页面
$.ajax({
type: "POST",
url: "myphp.php",
dataType: "json",
data: somedatatopost,
success: function(pRep){
if (!pRep) {
alert("No response from the server.");
return;
}
if (pRep.ok=="N") {
window.location = '/index.html';
return;
}
// here is where I handle a successful response
}
});
对于登录ajax调用,我有:
[....]
// here is where I handle a successful response
window.location='mynewpage.html'
解决方案正在调试中。首先尝试在没有ajax的情况下创建if。检查Cookie和会话id是否已在主页中使用会话_start()。帮助我们帮助您:提供一些代码!解决方案正在调试中。首先尝试在没有ajax的情况下创建if。检查Cookie和会话id是否已在主页中使用会话_start()。帮助我们帮助您:提供一些代码!