我花了很多天来解决这个问题,但我找不到解决方案。 在我使用我的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 :(得分:1)
在 config.php 中,在session_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,你看,嘿,似乎你已经登录了。然后你可能认为你的代码是疯狂的,随机工作或任何其他无关的原因。 (几年前,我有一个代码在当天下雨时工作,并且在没有下雨的时候没有工作。幸运的是,我在2天内解决了这个问题,然后变得疯狂!)
您还可以清理存储在服务器上的所有会话,以确保在更改代码时进行干净的测试。
我希望这些会有所帮助。
答案 1 :(得分:0)
我不确定是这种情况还是什么(因为我不知道config.php
里面有什么内容),但在我看来你忘了在你的“你使用它之前开始会话” PHP登录后端“文件!