2011-09-16 198 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