我的登录脚本有问题,我实现了ajax,所以如果有一个无效的登录,消息将显示在同一个页面上,但是当用户输入正确的信息时,我会有一些问题,我必须刷新页面,然后它说欢迎用户,但是当用户输入错误信息时,它会立即显示无效的登录消息。
index.php
<?php
if (!isset($_SESSION['uid'])) {
echo "<p>Welcome to our homepage. you can be part of our awesome community by signing up.</p>";
}else{
echo "<p>You are now logged in and able to post comments in our site</p>";
}
?>
<?php
if (!isset($_SESSION['uid'])) {
echo "<form action='login_parse.php' method='post' style='display: inline-block'>
Username: <input type='text' name='username' id='username' />
Password: <input type='password' name='password' id='password' />
<input id='submit-button' type = 'button' value='login' id='submit' />";
echo "</form>";
echo "<form action='signup.php' method='post' style='display: inline-block'>";
echo " ";
echo "<input type='submit' name='submit' value='Signup'>";
echo "</form>";
} else {
echo "<form style='display: inline-block'>";
echo "<p>You are logged in as ".$_SESSION['username']."";
echo "</form>";
echo "<form action='logout_parse.php' method='post' style='display: inline-block'>";
echo " ";
echo "<input type='submit' value='logout'>";
echo "</form>";
echo "<form action='profile.php' method='post' style='display: inline-block'>";
echo " ";
echo "<input type='submit' value='profile'>";
echo "</form>";
}
?>
<script src="ajax.js"></script>
<script>
var HTTP = loadXMLDoc();
var submitEvent = document.getElementById("submit-button").onclick = function(){
hello2(HTTP);
};
</script>ajax.js
function hello2(){
var username = encodeURIComponent(document.getElementById('username').value);
var password = encodeURIComponent(document.getElementById('password').value);
var url = "login.php?username="+username+"&password="+password;
HTTP.onreadystatechange=function()
{
if (HTTP.readyState==4 && HTTP.status==200)
{
document.getElementById("ack1").innerHTML=HTTP.responseText;
}
};
HTTP.open("POST", url ,true);
HTTP.send();
}login.php
<?php
session_start();
include_once("connect.php");
if (isset($_REQUEST['username'])) {
$username = mysql_real_escape_string( $_GET["username"]);
$password = mysql_real_escape_string( md5 ($_GET["password"]));
$sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 1) {
$row = mysql_fetch_assoc($res);
$_SESSION['uid'] = $row['id'];
$_SESSION['username'] = $row['username'];
header("location: index.php");
exit();
} else{
echo "INVALID login information.";
exit();
}
}
?>为了解决这个问题,我尝试使用login.php中的header函数刷新页面,但是仍然没有发生任何事情,我必须手动刷新页面,然后加载欢迎用户。我是不是做错什么了。
发布于 2015-04-07 12:37:11
使用ajax调用文件时,不能使用header /服务器端重定向。
相反,如果登录处理正确,则必须检查ajax调用的成功函数中的结果,并使用javascript重定向。
https://stackoverflow.com/questions/29491709
复制相似问题