我花了很多天來解決這個問題,我找不到解決方案。 我使用我的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 :)
}
我該如何解決這個問題?謝謝
不要以純文本格式存儲密碼。請不要使用'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
謝謝,我不小心刪除了md5函數以在發佈代碼時加密它。我已經在考慮使用新的方式改變登錄過程,但無論如何,我仍然爲這個令人討厭的bug而瘋狂:) –
我想'config.php'中有什麼問題。你可以在這裏發佈源代碼嗎? – Mahdi