检查登录脚本php jquery后没有任何反应 [英] nothing happens after check login script php jquery
问题描述
我正在尝试纠正一个简单的登录脚本.当我执行"checklogin.php"脚本时(由登录表单调用时)-没有任何反应.我停留在index.php上,但仍然看到该表单(应该已经被某些jquery删除了).如果我打开萤火虫,则会看到以下内容:
I am trying to right a simple login script. When I execute the "checklogin.php" script (upon being called by the login form) - nothing happens. I stay on index.php, and I still see the form (which should have been removed by some jquery). If I open firebug I see this:
POST http://localhost/~Eamon/checklogin.php 200 OK 2ms jquery.js (line 8724)
GET http://localhost/~Eamon/templates/login_success.php 302 Found 1ms jquery.js (line 8724)
GET http://localhost/~Eamon/index.php 200 OK 1ms
GET http://localhost/~Eamon/reqscripts/jquery.js?_=1371173056318 200 OK 1ms jquery.js (line 8724)
GET http://localhost/~Eamon/js/application.js?_=1371173056319 200 OK 0
没有任何错误-但我想知道所有这些"GET"请求是否正确.其中任何一个都应该是"POST"吗?
There aren't any errors - but I'm wondering if all those "GET" requests are correct. Should any of them be "POST"?
这是我的代码:
index.php
index.php
<!DOCTYPE html>
<html>
<head>
<title>it IT</title>
<script src="reqscripts/jquery.js"></script>
<script src="js/application.js"></script>
</head>
<body>
<form id="login" method="post" action="checklogin.php">
<h1>Member Login</h1>
<p>Username:<input name="myusername" type="text" id="myusername"></p>
<p>Password:<input name="mypassword" type="password" id="mypassword"></p>
<input type="submit" name="Submit" value="Login">
</form>
</body>
</html>
checklogin.php
checklogin.php
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="bonjour3"; // Mysql password
$db_name="itit"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "$username", "$password", "$db_name")or die("cannot connect");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql = $mysqli->prepare("SELECT * FROM $tbl_name WHERE username=? and password=?");
$sql->bind_param('ss',$myusername,$mypassword);
$sql->execute();
$sql->store_result();//apply to prepare statement
$numRows = $sql->num_rows;
if($numRows === 1){
$_SESSION['username'] = $myusername;
}
else {
echo "Wrong Username or Password";
}
session_destroy();
?>
js/application.js
js/application.js
$(document).ready(function() {
$("#login").submit(function(e) {
e.preventDefault();
$.post("checklogin.php", $(this).serialize(), function(){
$("#showuser").load("templates/login_success.php");
$("#login").remove();
});
});
});
templates/login_success.php
templates/login_success.php
<?php
session_start();
if($_SESSION['username'] === null){
header("location: ../index.php");
}
?>
<!DOCTYPE html>
<html>
<body>
<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<a href = "logout.php">Log out</a>
</body>
</html>
更新
这是尝试登录后的源代码.我将尝试说明为什么我认为从未设置$ _SESSION ['username'](但是为什么?)
Here is the source code from after an attempted log in. I will try to show why I think that $_SESSION['username'] is never being set (but why?)
<!DOCTYPE html>
<html>
<head>
<body>
<div id="showuser">
<title>it IT</title>
<script src="reqscripts/jquery.js">
<script src="js/application.js">
<form id="login" action="checklogin.php" method="post">
<h1>Member Login</h1>
<p>
Username: <input id="myusername" type="text" name="myusername">
</p>
<p>
Password: <input id="mypassword" type="password" name="mypassword">
</p>
<input type="submit" value="Login" name="Submit">
</form>
<form id="register" action="checkreglogin.php" method="post">
<h1>Member Registration</h1>
<p>
Username: <input id="rmyusername" type="text" name="rmyusername">
</p>
<p>
Password: <input id="rmypassword" type="password" name="rmypassword">
</p>
<p>
Email: <input id="myemail" type="text" name="myemail">
</p>
<input type="submit" value="Register" name="Submit">
</form>
<div id="showuser"></div>
</div>
</body>
</html>
请注意整个页面在分区中的状态...但是在底部也有一个空的"showuser" div.这使我感到困惑.我认为,当表单调用checklogin.php时-出现问题,并且用户永远不会保存在$ _SESSION ['username']中.因此,...在login_success.php被调用时-$ _SESSION ['username']设置为null ...并且index.php(全部)加载到$ .post请求上...因为当发生这种情况根据login_success.php,$ _ SESSION ['username']为空.本质上... login_success.php在此过程中的某个地方变成了index.php.启发我!
Notice how the entire page is in the division...but there is also and empty "showuser" div at the bottom. This confuses me. I think that, when checklogin.php is called by the form - something goes wrong, and the user never gets saved in $_SESSION['username']. Therefore...when login_success.php gets called - $_SESSION['username'] is set to null...and index.php (all of it) loads on $.post request...because that is what would happen when $_SESSION['username'] is null according to login_success.php. Essentially...login_success.php becomes index.php somewhere along the way. Enlighten me!
推荐答案
弄清楚了,感谢@fattomhk的评论.我不得不将checklogin.php中的session_destroy()行放入else块中……就像这样:
Figured it out, thanks to @fattomhk for the comment. I had to put the session_destroy() line in checklogin.php into the else block...like so:
...
else {
echo "Wrong Username or Password";
session_destroy();
}
?>
运行checklogin.php脚本后会话被破坏-因此从未设置会话用户名...并且login_success.php在加载页面之前检查用户名...如果没有用户名-它将加载index.php(这就是为什么登录后页面没有更改的原因.)
The session was getting destroyed after the checklogin.php script ran - thus the session username was never being set...and login_success.php checks for a username before loading the page...if there is no username - it loads index.php (which is why the page wasn't changing upon log in).
这篇关于检查登录脚本php jquery后没有任何反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!