[英]nothing happens after check login script 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. 当我执行“ checklogin.php”脚本时(由登录表单调用时)-没有任何反应。 I stay on index.php, and I still see the form (which should have been removed by some jquery). 我停留在index.php上,但仍然看到该表单(应该已经被某些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
There aren't any errors - but I'm wondering if all those "GET" requests are correct. 没有任何错误-但我想知道所有这些“ GET”请求是否正确。 Should any of them be "POST"? 其中任何一个都应该是“ POST”吗?
Here is my code: 这是我的代码:
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 模板/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>
UPDATE 更新
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?) 这是尝试登录后的源代码。我将尝试说明为什么我认为从未设置$ _SESSION ['username'](但是为什么?)
<!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>
Notice how the entire page is in the division...but there is also and empty "showuser" div at the bottom. 注意整个页面在分区中的方式...但是在底部还有一个空的“ showuser” div。 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']. 我认为,当表单调用checklogin.php时-出现问题,并且用户永远不会保存在$ _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. 因此,...在login_success.php被调用时-$ _SESSION ['username']设置为null ...并且index.php(全部)加载到$ .post请求上...因为当发生这种情况根据login_success.php,$ _ SESSION ['username']为空。 Essentially...login_success.php becomes index.php somewhere along the way. 本质上... login_success.php在此过程中的某处变为index.php。 Enlighten me! 开导我!
Figured it out, thanks to @fattomhk for the comment. 弄清楚了,感谢@fattomhk的评论。 I had to put the session_destroy() line in checklogin.php into the else block...like so: 我不得不将checklogin.php中的session_destroy()行放入else块中,就像这样:
...
else {
echo "Wrong Username or Password";
session_destroy();
}
?>
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). 在运行checklogin.php脚本后,会话已被破坏-因此从未设置会话用户名...并且login_success.php在加载页面之前检查用户名...如果没有用户名-它将加载index.php (这就是为什么登录后页面没有更改的原因)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.