我一直试图用PHP和MySQL创建登录代码。当我单击登录按钮时,即使用户名和密码正确,它也不会转到下一页:它只是刷新login.php页面。我不确定我的代码中存在什么问题。
此相同的代码适用于本地服务器,但在互联网上则不然。
<?php
session_start();
include './db_connect.php';
$error = " " ;
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password =md5($_POST['password']);
if((!isset($username)) || ($username=="") || (!$username == " ") || (!isset($password)) || ($password=="") || (!$password == " ")){
$error = "Please enter the details";
}
else{
$username = $_POST['username'];
$password =md5($_POST['password']);
$query = mysql_query("SELECT * FROM admin WHERE admin_name = '".$username."' and password = '".$password."'");
$row = mysql_fetch_array($query);
$username_db = $row['admin_name'];
$password_db = $row['password'];
if(($username == $username_db) && ($password ==$password_db) )
{
header ("Location:admin_hotel_details.php");
$_SESSION['username']=$username_db;
}
else{
$error = "Username and password do not match";
}
}
}
?>
这是admin.php(我的表格代码)
<form name="" action="admin.php" method="post">
<style type="text/css">
label{width:130px;}
</style>
<table cellpadding="4" style="margin-left:10px;">
<tr>
<td colspan="2"><span style="color:#F00;">
<?php
if(isset($_GET['msg'])){
$msg = $_GET['msg'];
$error = "Successfully logged out";
}
if(isset($error)){echo $error;}
?>
</span></td>
</tr>
<tr>
<td style="width:150px;"><label for="username">Username: <span style="color:#F00;">*</span>:</label></td>
<td style="width:100px;">
<input type="username" name="username" id="username" onkeyup="javascript:this.value=this.value.toUpperCase();" style="width:205px" />
</td>
</tr>
<tr>
<td style="width:100px;"><label for="password">Password:<span style="color:#F00;">*</span>:</label></td>
<td style="width:100px;"><input type="password" name="password" onkeyup="javascript:this.value=this.value.toUpperCase();" id="password" style="width:205px;" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
<td style="width:100px;">
<input type="submit" name="submit" id="submit" value="Login"/>
<input type="reset" name="reset" id="reset" value="Reset"/>
</td>
</tr>
</table>
</form>
我也试过了不同的login.php
<?php
include("./db_connect.php");
session_start();
$error = " " ;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Username and password sent from form in HTML
$myusername = $_POST['username'];
$mypassword = md5($_POST['password']);
$sql = "SELECT * FROM admin WHERE admin_name = '".$myusername."' and password ='".$mypassword."'";
$result = mysql_query($sql) or die(mysql_error($sql));
$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if ($count == 1) {
$_SESSION['login_user'] = $myusername;
header("location:admin_hotel_details.php");
} else {
$error = "Your username or password is invalid";
}
}
?>
这给了我同样的问题(在标题处)。
答案 0 :(得分:0)
在这里,我尝试根据我发布的评论稍微清理一下你的代码。看看这是否适合你。
<?php
session_start();
include './db_connect.php';
$error = " " ;
if(isset($_POST['submit']) && isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password =md5($_POST['password']);
if($username=="" || $password==""){
$error = "Please enter the details";
}else{
$result = mysql_query("SELECT * FROM admin WHERE admin_name = '".$username."' and password = '".$password."'");
if(mysql_num_rows($result)>0){
$_SESSION['username']=$username;
header("Location:admin_hotel_details.php");
}else{
$error = "Username and password do not match";
}
}
}
?>
答案 1 :(得分:0)
尝试错误地使用脚本。
header("location: admin_hotel_details.php");
exit();
}else {
//Login failed
echo "<script>alert('INVALID USERNAME OR PASSWORD!');
window.location='index.php';
</script>";