[英]Login index form is not working with login.php if i use header() function
我是php的初學者。 我正在嘗試制作一個登錄表單(index1.php)。 如果我使用表單標記中的“ action”屬性將其重定向到login.php,則效果很好。 但是我也想在相同的索引形式上進行一些驗證。因此,我使用標頭函數將其重定向到“ login.php”,而不是在“ action”屬性中。
無論我是否輸入正確的用戶名和密碼,它都會在執行login.php的最后一個錯誤時給我錯誤變量'$ username'和'$ password'的錯誤(//該用戶不存在)。 請幫助我了解我的代碼有什么問題以及如何使它與login.php一起運行?
這是index1.php的代碼
<!DOCTYPE HTML>
<html>
<head><br>
<style>
.error {color: #FF0000;}
</style>
</head>
<body style="font-family: verdana, sans-serif;";>
<hr />
<div style="width: 77%; height: 300px; padding: 30px; border: 5px solid #e3e3e3; background-color: #F8F8F8 ; color: #000; margin: 100px;" align="center">
<br>
<?php
$username=$password="";
$Err="";
if($_SERVER['REQUEST_METHOD']== "POST")
{
$valid = true;
if ((empty($_POST["username"])) || (empty($_POST["password"])))
{
$Err = "Please Enter your username and password";
$valid = false;
}
else
{
$username = test_input($_POST["username"]);
$password = test_input($_POST["password"]);
}
if($valid)
{
header("location:login.php");
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<span class="error"><?php echo $Err; ?></span>
<h2>Login</h2>
<form action="menubar.php?page=index1" method="post">
<font size="3">Username:<input type="text" name="username" style="padding: 4px;"/><p>
Password: <input type="password" name="password" style="padding: 4px;"/><p>
<input type="submit" value="Login" style="padding: 4px; width: 5em; height: 1.9em;"/></font>
</form>
<h5> No Account?<a href="menubar.php?page=register">Register!</a></h5>
</div>
</body>
</html>
這是login.php的代碼。
<?php
session_start();
$username=$_POST["username"];
$password=$_POST["password"];
$message[]="Fill up both username and password";
$connect=mysql_connect("localhost","root","") or die("Couldnt connect to Database");
mysql_select_db("login") or die("Couldnt find Database");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if($numrows!==0)//username exists in database
{
while($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if($username==$dbusername&&$password==$dbpassword)//valid username and password
{
$_SESSION['username'] = $username;
header("Location: menubar.php?page=home");
}
else//incorrect password or username
die("incorrect password or username");
}
else//that user doesnt exit
die("that user doesnt exit");//
?>
好吧,現在我改用“ sqli”
這是更新的login.php代碼
<?php
session_start();
$username=$_POST["username"];
$password=$_POST["password"];
$message[]="Fill up both username and password";
$connect=mysqli_connect("localhost","root","") or die("Couldnt connect to Database");
mysqli_select_db($connect,'login') or die("Couldnt find Database");
$query = mysqli_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysqli_num_rows($query);
if($numrows!==0)//username exists in database
{
while($row = mysqli_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if($username==$dbusername&&$password==$dbpassword)//valid username and password
{
$_SESSION['username'] = $username;
header("Location: menubar.php?page=home");
}
else//incorrect password or username
die("incorrect password or username");
}
else//that user doesnt exit
die("that user doesnt exit");//
?>
現在更新后,我沒有收到無效變量的錯誤。 但是它仍然沒有正確進行。無論我是否輸入正確的用戶名和密碼,它都會執行最里面的內容,即(//有效的用戶名和密碼)
謝謝....
所以你的代碼將是這樣的
<?php
$username=$password="";
$Err="";
if($_SERVER['REQUEST_METHOD']== "POST")
{
$valid = true;
if ((empty($_POST["username"])) || (empty($_POST["password"])))
{
$Err = "Please Enter your username and password";
$valid = false;
}
else
{
$username = test_input($_POST["username"]);
$password = test_input($_POST["password"]);
}
if($valid)
{
include("login.php");
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE HTML>
<html>
<head><br>
<style>
.error {color: #FF0000;}
</style>
</head>
<body style="font-family: verdana, sans-serif;";>
<hr />
<div style="width: 77%; height: 300px; padding: 30px; border: 5px solid #e3e3e3; background-color: #F8F8F8 ; color: #000; margin: 100px;" align="center">
<br>
<span class="error"><?php echo $Err; ?></span>
<h2>Login</h2>
<form action="menubar.php?page=index1" method="post">
<font size="3">Username:<input type="text" name="username" style="padding: 4px;"/><p>
Password: <input type="password" name="password" style="padding: 4px;"/><p>
<input type="submit" value="Login" style="padding: 4px; width: 5em; height: 1.9em;"/></font>
</form>
<h5> No Account?<a href="menubar.php?page=register">Register!</a></h5>
</div>
</body>
</html>
請務必注意,在設置標頭之前,您不應該向客戶端發送任何輸出-
header("Location: menubar.php?page=home");//or login.php
因此,在發送任何數據之前,請驗證您的數據並設置標題,如果無效數據則發送正常數據。
更新:以下是完整的經過測試的代碼
index1.php
<?php session_start();?>
<!DOCTYPE HTML>
<html>
<head><br>
<style>
.error {color: #FF0000;}
</style>
</head>
<body style="font-family: verdana, sans-serif;";>
<hr />
<div style="width: 77%; height: 300px; padding: 30px; border: 5px solid #e3e3e3; background-color: #F8F8F8 ; color: #000; margin: 100px;" align="center">
<br>
<?php if(isset($_SESSION['message'])){ echo "<span class='error'>".$_SESSION['message']."</span>"; $_SESSION['message']=null;}?>
<h2>Login</h2>
<form action="login.php" method="post">
<font size="3">Username:<input type="text" name="username" style="padding: 4px;"/><p>
Password: <input type="password" name="password" style="padding: 4px;"/><p>
<input type="submit" value="Login" style="padding: 4px; width: 5em; height: 1.9em;"/></font>
</form>
<h5> No Account?<a href="menubar.php?page=register">Register!</a></h5>
</div>
</body>
</html>
的login.php
<?php
session_start();
$username=$password="";
$Err="";
if($_SERVER['REQUEST_METHOD']== "POST")
{
$valid = true;
if ((empty($_POST["username"])) || (empty($_POST["password"])))
{
$Err = "Please Enter your username and password";
$valid = false;
}
else
{
$username = test_input($_POST["username"]);
$password = test_input($_POST["password"]);
}
if($valid) {
$valid = false;//reset $valid value to validate for Database test
//$username=$_POST["username"];
//$password=$_POST["password"];
//$message[]="Fill up both username and password";
$connect=mysqli_connect("localhost","root","") or die("Couldnt connect to Database");
mysqli_select_db($connect,'login') or die("Couldnt find Database");
//Note: mysqi_query() returns results of query
$result = mysqli_query($connect, "SELECT * FROM users WHERE username='$username'");//update here pass the $connect parameter
$numrows = mysqli_num_rows($result);//change here
if($numrows!==0)//username exists in database
{
$dbusername = null;//move it up for visibility after while block
$dbpassword = null;
while($row = mysqli_fetch_assoc($result))//Note here pass the result
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if($username == $dbusername&&$password==$dbpassword)//valid username and password
{
$_SESSION['username'] = $username;
header("Location: menubar.php?page=home");//the landing page
die();
}
else{
//incorrect password or username
$Err = "incorrect password or username";
$valid = false;
}
}
else {
//that user doesnt exit
$Err = "that user doesnt exit";
$valid = false;
}
//header("location:index.php");
}
if(!$valid) {
$_SESSION['message'] = $Err;//Put the message in $_SESSION variable
header("location:index1.php");
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
menubar.php
<?php
session_start();
$page=isset($_GET["page"])?$_GET["page"]:"";
if(isset($page)) {
header("location: ".$page.".php");
} else {
header("location: 404.php");
}
?>
您代碼中的問題是在login.php
,從未設置$_POST["username"]
和$_POST["password"]
,所以最好的辦法是不使用header
而只使用require_once
<!DOCTYPE HTML>
<html>
<head><br>
<style>
.error {color: #FF0000;}
</style>
</head>
<body style="font-family: verdana, sans-serif;";>
<hr />
<div style="width: 77%; height: 300px; padding: 30px; border: 5px solid #e3e3e3; background-color: #F8F8F8 ; color: #000; margin: 100px;" align="center">
<br>
<?php
$username=$password="";
$Err="";
if($_SERVER['REQUEST_METHOD']== "POST"){
$valid = true;
if ((empty($_POST["username"])) || (empty($_POST["password"]))){
$Err = "Please Enter your username and password";
$valid = false;
}else{
$username = test_input($_POST["username"]);
$password = test_input($_POST["password"]);
}
if($valid)
{
require_once('login.php');
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<span class="error"><?php echo $Err; ?></span>
<h2>Login</h2>
<form action="menubar.php?page=index1" method="post">
<font size="3">Username:<input type="text" name="username" style="padding: 4px;"/><p>
Password: <input type="password" name="password" style="padding: 4px;"/><p>
<input type="submit" value="Login" style="padding: 4px; width: 5em; height: 1.9em;"/></font>
</form>
<h5> No Account?<a href="menubar.php?page=register">Register!</a></h5>
</div>
</body>
</html>
那是因為您有一個輸出:
?>
<?php
導致空白行輸出。
在發送任何實際輸出之前,必須通過常規HTML標記,文件中的空白行或從PHP中調用header()
合並所有PHP代碼,並確保文件開頭沒有空格。
同樣在header('location: index.php');
添加exit();
如果您還有其他腳本,請在下面。
還要將您的重定向標頭移到最后一個if之后。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.