此登录用户代码适用于localhost,但不适用于实时服务器。 index.html会通过ajax将变量发布到checklogin.php,response
可以是0
或1
。现在我也没有。密码错误或密码正确0
或1
未被退回。
的index.html
<script type="text/javascript">
function check() {
var one = $('#username').val();
var two = $('#password').val();
if(one == '' || two == ''){
$('#stage').show().html("Please enter your username and password!");
$('#stage').delay(2000).fadeOut();
}
else
$.post("checklogin.php", {
username : one,
password : two
},function(data){
if (data.response==1)
{
window.location = '/client/client_dashboard.php';
}
else if (data.response==0) {
$('#stage').show().html("Your username or password is incorrect!");
}
$('#stage').delay(2000).fadeOut();
},"json");
}
</script>
checklogin.php
<?php
include("connect.php");
$myusername = isset($_POST['username']) ? $_POST['username'] : false;
$mypassword = isset($_POST['password']) ? $_POST['password'] : false;
// username and password sent from form
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM `client` WHERE username='$myusername' AND password='$mypassword'";
$result=mysqli_query($conn, $sql);
// Mysql_num_row is counting table row
$num=mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($num==1){
session_start();
$_SESSION["myusername"] = $myusername;
$response = 1;
}
else {
$response = 0;
}
$display = array('response' => $response);
echo json_encode($display);
?>
我做了一些调试来测试mysql连接并选择带有固定数据的用户名和密码:
<?php
include("connect.php");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* check if server is alive */
if (mysqli_ping($conn)) {
printf ("Our connection is ok!\n");
$sql="SELECT * FROM `client` WHERE username='sami' AND password='123'";
$result=mysqli_query($conn, $sql);
// Mysql_num_row is counting table row
$num=mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($num==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_start();
$_SESSION["myusername"] = $myusername;
echo "got data";
}
else {
echo "no data";
}
} else {
printf ("Error: %s\n", mysqli_error($link));
}
/* close connection */
mysqli_close($link);
?>
服务器给我这个输出:
Our connection is ok! got data
我尝试使用chrome进行调试:
更多细节:
所以我决定删除mysql用户名&amp;密码选择看看发生了什么和奇怪我设法登录。我猜这些数据确实已发布。
$myusername = isset($_POST['username']) ? $_POST['username'] : false;
$mypassword = isset($_POST['password']) ? $_POST['password'] : false;
if($myusername != ''){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_start();
$_SESSION["myusername"] = $myusername;
$response = 1;
}
else {
$response = 0;
}
$display = array('response' => $response);
echo json_encode($display);
这可能是什么问题?