2011-09-16 207 views
7

我有一个通过AJAX登录的代码,然后将数据传递给.php文件以根据SQL进行检查。对于测试,用户名和密码是我 - 我,但即使这从检查它回来没有登录我,似乎会议没有被设置。没有设置PHP会话

log.php

<script type="text/javascript"> 
$(document).ready(function() 
{ 
$("#login_form").submit(function() 
{ 
    //remove all the class add the messagebox classes and start fading 
       $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000); 
    //check the username exists or not from ajax 
    $.post("ejaxlog.php",{ username:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data) 
    { 
     if($.trim(data)=='yes') //if correct login detail 
     { 
     $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox 
     { 
      //add message and change the class of the box and start fading 
      $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1, 
      function() 
      { 
      //redirect to secure page 
      document.location='http://www.google.com'; 
      }); 

     }); 
     } 
     else 
     { 
     $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox 
     { 
      //add message and change the class of the box and start fading 
      $(this).html('Your login detail sucks...').addClass('messageboxerror').fadeTo(900,1); 
     });  
     } 

    }); 
    return false; //not to post the form physically 
}); 
//now call the ajax also focus move from 
$("#password").blur(function() 
{ 
    $("#login_form").trigger('submit'); 
}); 
    }); 
    </script> 

    <link type="text/css" href="formboxes.css" rel="stylesheet"> 

    </head> 

    <body> 
    <? 
    echo $_SESSION['u_name'];?> 
    <form method="post" action="" id="login_form"> 
<div align="center"> 

<div > 
    User Name : <input name="username" type="text" id="username" value="" maxlength="20" /> 

</div> 
<div style="margin-top:5px" > 
    Password : 
    &nbsp;&nbsp; 
<input name="password" type="password" id="password" value="" maxlength="20" /> 

</div> 
<div class="buttondiv"> 
<input name="Submit" type="submit" id="submit" value="Login" style="margin-left:-10px; height:23px" /> <span id="msgbox" style="display:none"></span> 

</div> 

    </div> 
</form> 

这再通过下一级代码检查MySQL的,是它成功的话回声出“是”这是我在我的HTTP头看到(所以必须是正确的),但它不根据log.php文件中的指定将我重定向到“eindex.php”。

<?php 
session_start(); 
require("../mcfrdb.php"); 
// Included database once using the require method 
?> 

    <?php 


$username = mysql_real_escape_string(stripslashes($_POST['username'])); 
$pass = mysql_real_escape_string(stripslashes($_POST['password'])); 


$result = mysql_query("SELECT user, pass FROM mcfruse WHERE user='$username'")or die(mysql_error()); 
$row=mysql_fetch_array($result); 

if(mysql_num_rows($result)>0) 
{ 

    if(strcmp($row['pass'],$pass)==0) 
    { 
      echo "yes"; 
      $_SESSION['name']=$username; 
    } 
    else 
      echo "no"; 
} 
else 
    echo "no"; 

我的HTML时使用萤火说

所以是被echo'ed这意味着它传递正确的PWD,它验证,但它仍然表示,“登录细节吸”和犯规重定向我eindex.php

eindex.php

<?php 
session_start(); 
if(!isset($_SESSION['name'])) 
header("Location:../index.php"); 
//if logout then destroy the session and redirect the user 
if(isset($_GET['logout'])) 
{ 
session_destroy(); 
header("Location:../index.php"); 
} 

require("../mcfrdb.php"); 
?> 

我已经检查了几次代码,但没有找到任何东西。所有答复和任何帮助表示赞赏。 非常感谢。

编辑:甚至添加在MD5 pwd哈希(从此代码中省略)我仍然得到“是”,当我回显用户名和哈希,他们是机器人匹配,会话不是仍然设置,但不重定向我。在如果($。TRIM(数据)==“是”)

+0

你的['session_start()'](http://php.net/manual/en/function.session-start.php)在哪里? – feeela

+1

好的错误信息bro –

+0

在那里,jsut在最上面,它从代码中省略了,我想我应该把它粘在; D – ben

回答

1

ejaxlog.php回报yes的顶部?你确定?尝试添加alert(data)console.log(data)以查看它返回的内容。似乎你的脚本不会返回yes或不只是yes(可能会出现一些错误?)。显然,你没有被重定向,因为你的JavaScript收到的字符串不合适,所以它不会重定向你。 再次尝试记录/提醒脚本返回的数据。或者安装诸如FireFug for FireFox之类的东西来查看JavaScript所做的所有请求。

+0

ahhhh,没人会明白的。 (数据)=“是”返回是坏的,因为它试图返回所有的HTML废话,而不是只是“是”它是搭售返回没想到用FB甚至我有我它安装,现在工作完美。谢谢 – ben

7

您通过AJAX访问任何页面是单独的请求,你将需要调用session_start()为了使会话超全局填充。

0

使用session_start()你ejaxlog.php页面

+0

我在那里,只是从代码中省略....现在加入 – ben