Php 帖子在我的托管服务中不起作用
我有以下获取用户名和密码的代码:Php 帖子在我的托管服务中不起作用,php,Php,我有以下获取用户名和密码的代码: <form action="checklogin.php" method="post"> <table> <TR><TD>Username: </TD><TD> <input type="text" name="username" required="required" class="ipt"/> </
<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
}
?>
//在checklogin.php中:
我检查了和数据库的连接是否正常。
我如何追踪问题
在添加一些报告代码后,我收到了以下警告:
如我在评论中所述: “我猜您首先需要连接,然后将DB连接传递给mysql_real_escape_string(),或者只使用mysqli_u进行所有操作,或者PDO。” 因此,从错误消息的外观来看,服务器的设置很可能需要将DB连接作为参数传递: 将DB连接传递给您的功能,并将当前连接代码修改为:
$conn = mysql_connect("localhost", "xxxxxx","xxxxxxx");
mysql_select_db("xxxxxxx", $conn) or die(mysql_error($conn));
手册中的示例:
$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$conn) {
die('Not connected : ' . mysql_error());
}
然后:
但是,使用mysqli\uucode>或PDO并使用预先准备好的语句会更好,因为mysql\ucode>函数已被弃用,并将从未来的PHP版本中删除
还要确保您的HTML表单不在PHP之上,因为您可能会在标题之前输出。首先放置
,然后放置PHP的其余部分,然后放置HTML,或者使用两个单独的文件
另外,您提到在同一个文件中同时使用HTML表单和PHP/MySQL
您需要使用有条件的!empty()
用于输入
,或者,它们更安全
参考资料:
- MySQLi连接:
脚注:
添加退出代码>在每个标题之后。否则,您的代码可能需要继续执行
即:
在浏览器中查看源代码。我相信您会看到正在打印的javascript。也许您甚至会注意到语法错误
将导致javascript语法错误,因为您的$username
需要被引用,因为它是一个字符串-打印“警报”(“.$username”)代码>“已检查与数据库的连接是否正常。”-这是<代码>mysql
?-<代码>mysqli
?-PDO?plus,HTML表单和PHP/SQL是否在同一个文件中?echo$username代码>上面说什么?非常感谢。它工作得很好。@Alireza非常欢迎你。
$username = mysql_real_escape_string($_POST['username'], $conn);
$password = mysql_real_escape_string($_POST['password'], $conn);
header("location: home.php");
exit;