2010-03-16 132 views
1

我已经创建了登录页面,它向php页面发送ajax请求以进行登录验证。 在这个PHP页面我创建会话,并发送响应按登录验证。 如果用户通过身份验证,我将它从我发送ajax的java脚本重定向到主页。但在那个主页上我不能得到那个会话对象......为什么? u能告诉我的解决方案来检索主页上那届ajax登录问题

+0

解决方案是在调试。如果没有ajax,请尝试制作。检查cookie和会话ID的 – 2010-03-16 05:41:25

+0

是否在主页中使用了session_start()。 – 2010-03-16 05:54:01

+0

帮助我们来帮助你:提供一些代码! – 2010-03-16 08:16:31

回答

1

我不知道我做了正确的方式,但它为我这种方式(我希望我没有忘记任何事情):

这里的什么我做的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'