2012-11-04 57 views
0

我花了很多天來解決這個問題,我找不到解決方案。 我使用我的ajax表單+ php後端登錄後,用戶重定向頁面顯示「Missing session」消息,如果我嘗試轉儲$ _SESSION,它看起來像是空的。然後,如果我回去,我做同樣的登錄它會正常工作。這發生在不同的瀏覽器(通常當他們有cookie和緩存清除)和在不同的託管服務提供商。

這是我的Ajax代碼:


$(document).ready(function(){ 
$('#loginbtn').click(function(){ 

if($('#username').val() == "" || $('#password').val() == ""){ 
      return false; 
     } 
     else 
     { 

      $.ajax 
      ({ 
       type: 'POST', 
       url: 'ajax_login.php', 
       cache: false, 
       dataType: 'json', 
       data: 
       { 
        username: $('#username').val(), 
        password: $('#password').val() 
       }, 
       success:function(data) 
       { 


       if(data.error === true){ 

alert("Failed to login: "+data.message) 

        } 
        else 
        { 


     setTimeout(function() 
     { 
window.location = 'http://www.mywebsite.com/dashboard.php'; 
     },2000); 

        } 
       }, 
       error:function(XMLHttpRequest,textStatus,errorThrown){ 
      alert("An error occured!"); 

       } 
      }); 
      return false; 
     } 

    });  

    }); 

這是PHP登錄後臺:

<?php 
include "config.php"; // start session and connect to mysql db, also contain functions sanitize(), getip() 

$username = sanitize(htmlspecialchars($_POST['username'],ENT_QUOTES)); 
$pass = sanitize($_POST['password']); 


// FUNCTION TO LOGIN 
$sql = mysql_query("SELECT * FROM members WHERE username = '$username' AND password = '$pass'"); 
$array = mysql_fetch_array($sql); 

if(mysql_num_rows($sql) === 0){ 
$message['error'] = true; 
$message['message'] = "Wrong username or password.";  
echo json_encode($message); 
exit; 
} 


$_SESSION['username'] = ucwords(strtolower($username)); 
$_SESSION['points'] = $array['points']; 
$_SESSION['ip'] = getip(); 
$_SESSION['welcome'] = true; 


$message['error'] = false; 
$message['message'] = "Completato.";  

echo json_encode($message); 
exit; 

最後這dashboard.php檢查會議代碼:

<?php 

include "config.php"; 
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); 

if($_SESSION['username'] == "") { 
header("Location: index.php?nosession"); 
exit; 
} 

編輯:這是config.php裏面的內容

<? 
session_start(); 
date_default_timezone_set("Europe/Rome"); 
$hostname = ""; //hostname 
$data_username = "dbxxxxxxxx"; //database username 
$data_password = "xxxxx"; //database password 
$data_basename = "dbxxxxxxx"; //database name 
$conn = mysql_connect("".$hostname."","".$data_username."","".$data_password.""); 
mysql_select_db("".$data_basename."") or die(mysql_error()); 

function sanitize($text) { // funzione che pulisce le stringe per prevenire exploit; 
if(get_magic_quotes_gpc() == 0) { 
$text = addslashes($text); 
} 
$text = htmlentities($text); 
$text = strip_tags($text); 
$escape = mysql_real_escape_string($text); 
$arraydangerous = array('SELECT *', 'LOAD_FILE', 'DELETE', 'TRUNCATE', '\' OR', '<javascript>', 'src=', '<?', '?>', 'document.cookie', 'http://', 'www.'); 
$text = str_replace($arraydangerous, "", $text); 
return $text; 
} 

function getip() 
{ 
    return filtra($_SERVER['HTTP_CF_CONNECTING_IP']); // I use CloudFlare ,so i must use this way :) 
} 

我該如何解決這個問題?謝謝

+0

不要以純文本格式存儲密碼。請不要使用'mysql_ *'函數來編寫新的代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/q0gwD)。看到[紅色框](http://goo.gl/OWwr2)?相反,您應該瞭解[準備好的語句](http://goo.gl/orrj0)並使用[PDO](http://goo.gl/TD3xh)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](goo.gl/YXyWL)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/b2ATO)。另請參閱[爲什麼我不應該在PHP中使用mysql函數?](http://goo.gl/J5jAo) – DCoder

+0

謝謝,我不小心刪除了md5函數以在發佈代碼時加密它。我已經在考慮使用新的方式改變登錄過程,但無論如何,我仍然爲這個令人討厭的bug而瘋狂:) –

+0

我想'config.php'中有什麼問題。你可以在這裏發佈源代碼嗎? – Mahdi

回答

1

config.phpsession_start();後面添加這行。

session_start(); 

// reset the session, if not logged-in 
if (empty($_SESSION['username'])) { 

    $_SESSION['username'] = null; 
    $_SESSION['points'] = null; 
    $_SESSION['ip'] = null; 
    $_SESSION['welcome'] = null; 
} 

此外,我想這是更好的,你改變dashboard.php到這樣的事情:

<?php 

include "config.php"; 

if($_SESSION['username'] == "") { 
    header("Location: index.php?nosession"); 
    exit; 
} 


if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); 

?> 

我覺得你的問題是,你有你的服務器上的舊會話,而測試代碼。例如,您嘗試登錄,將值添加到會話中,但出於任何原因您收到一些錯誤,並且您看到您未登錄。您忘記了已將一些數據添加到會話中,刷新dashboard.php,你看到嘿,看起來你已經登錄了。然後你可能會認爲你的代碼是瘋狂的,隨機工作或任何其他不相關的原因。 (幾年前,我有一段代碼在當天下雨時正在工作,並且在不下雨的時候沒有工作,幸運的是,我在兩天內解決了這個問題,然後才發瘋)

您也可以清理存儲在服務器上的所有會話,以確保您有一個乾淨的測試,而您正在更改代碼。

我希望這些會有所幫助。

+0

非常感謝您的幫助。不幸的是這仍然發生。我現在用語言解釋:用戶登錄並將其重定向到儀表板,並顯示錯誤(調試$ _SESSION var 100%爲空,不需要舊會話!)。如果他回來了(有時候,如果他只是刷新),它會修復並正確加載$ _SESSION變量。仍然瘋狂:) –

0

我不確定這是這種情況還是什麼(因爲我不知道config.php裏面有什麼),但在我看來,你忘記開始會話,然後在你的「PHP登錄後端「文件!

+0

我發佈了Config.php :)它總是啓動會話,因爲它是第一個包含的文件。 –