我有获取用户名和密码的代码:
<form action="checklogin.php" method="post">
<table>
<TR><TD>Username: </TD><TD> <input type="text" name="username" required="required" class="ipt"/> </TD></TR>
<TR><TD>Password: </TD><TD> <input type="password" name="password" required="required" class="ipt" /> </TD></TR>
<TR><TD></TD><TD ><input type="submit" value="Login"/> </TD></TR>
</table>
</form>
使用XAMP在本地计算机上正常工作。但是,当我将文件传输到真正的严重,checklogin.php无法接收用户名,我在checklogin.php中检查$ username是空的:
//in checklogin.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
mysql_connect("localhost", "xxxxxx","xxxxxxx") or die(mysql_error()); //Connect to server
mysql_select_db("xxxxxxx") or die("Cannot connect to database"); //Connect to database
$query = mysql_query("SELECT * from users WHERE username='$username'"); //Query the users table if there are matching rows equal to $username
$exists = mysql_num_rows($query); //Checks if username exists
$table_users = "";
$table_password = "";
if($exists > 0) //IF there are no returning rows or no existing username
{
while($row = mysql_fetch_assoc($query)) //display all rows from query
{
$table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
$table_password = $row['password']; // the first password row is passed on to $table_users, and so on until the query is finished
}
if(($username == $table_users) && ($password == $table_password)) // checks if there are any matching fields
{
if($password == $table_password)
{
$_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
header("location: home.php"); // redirects the user to the authenticated home page
}
}
else
{
Print '<script>alert("Incorrect Password!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
}
else
{
Print '<script>alert("Incorrect Username!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
?>
我检查了与数据库的连接是否正常。 我该如何追踪问题
答案 0 :(得分:3)
正如我在评论中所述:
“我的猜测是你首先需要连接,然后将数据库连接传递给mysql_real_escape_string(),或者只使用mysqli_用于所有内容,或PDO。”
因此,根据您的错误消息的外观,您的服务器设置很可能需要将DB连接作为参数传递:
将数据库连接传递给您的功能,并将当前的连接代码修改为:
$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$conn) {
die('Not connected : ' . mysql_error());
}
手册中的示例:
$username = mysql_real_escape_string($_POST['username'], $conn);
$password = mysql_real_escape_string($_POST['password'], $conn);
然后:
mysqli_
但是,使用mysql_
或PDO并使用预准备语句可以更好地解决这个问题,因为<?php session_start(); ?>
函数已被弃用,将从未来的PHP版本中删除。
确保您的HTML表单不在PHP之上,因为您可能在标题之前输出。首先放置!empty()
,然后放置PHP的其余部分,然后放置HTML,或者使用两个单独的文件。
另外,您提到您在同一文件中同时使用HTML表单和PHP / MySQL。
您需要使用条件exit;
作为输入。
Use mysqli
with prepared statements或PDO with prepared statements,他们更安全。
参考(S):
脚注:
在每个标题后添加header("location: home.php");
exit;
。否则,您的代码可能希望继续执行。
即:
loadform