我的登录脚本一切正常。我在我的div中获得正确的响应(id = login_reply)并且会话开始。但每当我刷新页面时,login_reply就会消失。我怎么能保持login_reply?谢谢!
这是php:
if (isset($_POST['username'], $_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string(md5($_POST['password']));
$check = mysql_query("SELECT * FROM `userbase` WHERE `user_name` = '$username'");
if (mysql_num_rows($check) > 0)
{
while ($row = mysql_fetch_assoc($check))
{
$user_id = $row['user_id'];
$user_name = $row['user_name'];
$user_email = $row['user_email'];
$user_password = $row['user_password'];
if ($password == $user_password)
{
$_SESSION['user_id'] = $user_id;
if (isset($_SESSION['user_id']) && $_SESSION['user_id'] != '')
echo "Welcome back '$user_name'!";
else
{
echo 'no';
}
}
else
echo 'no';
}
}
else
echo 'no';
}
这是jQuery
$(document).ready(function()
{
$('#login').click(function()
{
var username = $('#username').val();
var password = $('#password').val();
$.ajax(
{
type: 'POST',
url: 'php/login.php',
data: 'username=' +username + '&password=' + password,
success: function(data)
{
if (data != 'no')
{
$('#logform').slideUp(function()
{
$('#login_reply').html(data).css('color', 'white');
});
}
else
$('#login_reply').html('Invalid username or password').css('color', 'red');
}
});
return false;
});
});
答案 0 :(得分:3)
问题是JS只是客户端脚本语言 - 它只在客户端浏览器上处理。
如果您想使用AJAX登录,好的,但您必须将登录用户的值存储到会话(或cookie)中。然后在每个页面加载您将检查该会话或cookie是否已设置和存在,如果它们存在,您将在登录后写下与jQuery相对应的HTML ...
换句话说,这就是你要做的事情:
$_SESSION['username']
(例如)$('#login_reply')
$_SESSION['username']
希望这会有所帮助......
EDIT1 :您还应该以客户端浏览器上没有JS工作(禁用)的方式实现登录功能,因此应该对普通的POST进行计数......
EDIT2 :我还想指出一些常见的编程错误......
以下是我的评论添加了我的评论:
if (isset($_POST['username'], $_POST['password']))
{ // <-- This is a .NET style of writing the brackets that I don't like much and I guess PHP don't like .NET either :-)
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string(md5($_POST['password']));
$check = mysql_query("SELECT * FROM `userbase` WHERE `user_name` = '$username'"); // <-- mysql_* method calls should be replaced by PDO or at least mysqli
// Also the query could be improved
if (mysql_num_rows($check) > 0)
{
while ($row = mysql_fetch_assoc($check)) // <-- why calling while when You expect ONLY one user to be found?
{
$user_id = $row['user_id'];
$user_name = $row['user_name'];
$user_email = $row['user_email'];
$user_password = $row['user_password']; // <-- What are these last 4 lines good for? This is useless...
if ($password == $user_password) // <-- this condition SHOULD be within the SQL query...
{
$_SESSION['user_id'] = $user_id;
if (isset($_SESSION['user_id']) && $_SESSION['user_id'] != '') // <-- this condition is useless as You have just set the session variable...
// ALSO, if You use brackets with else it is good to use brackets also with if
echo "Welcome back '$user_name'!";
else
{
echo 'no';
}
}
else
echo 'no';
}
}
else
echo 'no';
}
这是我的重写代码(仍然使用mysql_ *,对不起):
if (isset($_POST['username'], $_POST['password'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string(md5($_POST['password']));
$check = mysql_query("SELECT * FROM `userbase` WHERE `user_name` = '{$username}' AND `user_password` = '{$password}' LIMIT 1"); // <-- We check whether a user with given username AND password exists and we ONLY want to return ONE record if found...
if ($check !== false) {
$row = mysql_fetch_assoc($check);
$_SESSION['user_id'] = $row['user_id'];
echo "Welcome back '{$row['user_name']}'!";
} else {
echo 'no';
}
} else
echo 'no';
}
答案 1 :(得分:2)
最简单的解决方案是使用cookies:
在PHP方面,您声明并删除cookie:
<?php
if (!isset($_COOKIE['new_one']) ) {
setcookie('new_one', "ole", 0, "/");
echo "logged in";
}
else {
setcookie('new_one', null);
echo "logged out";
}
?>
在jQuery客户端:
$(document).ready(function() {
if ($.cookie("new_one") === "ole") {
$('#login_reply').html("Show msg only when the server sets the cookie.");
}
});
答案 2 :(得分:1)
您需要将登录回复数据存储在服务器上或者会话中; javascript是一种客户端语言,因此在刷新之间不会保留所获得的任何值。
我建议您存储结果,然后在使用php生成页面时检查它是否存在;如果是,请填写数据..等等。