我有一个页面(index.php)是一个登录页面,所以我需要验证用户并重定向到其他页面但是标题(位置:“welcome.php”);不工作,sql查询没问题,但我只收到消息“登录成功”,页面doest重定向到另一个名为welcome.php 我是PHP的新手,所以任何帮助都很棒!
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico">
<title>Login</title>
<link href="bootstrap.min.css" rel="stylesheet">
<link href="signin.css" rel="stylesheet">
</head>
<body>
<div class="container">
<form class="form-signin" role="form" action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<h2 class="form-signin-heading"><center>Bienvenido!</center></h2>
<input type="text" name="username" class="form-control" placeholder="Username" required="" autofocus="">
<input type="password" name="password" class="form-control" placeholder="Password" required="">
<div class="checkbox">
<label><input type="checkbox" value="remember-me"> Remember me </label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
</form>
</div>
<?php
$link = mysqli_connect("localhost","root","root","testdb") or die ("error".mysqli_error($link));
$username = $_POST['username'];
$password= $_POST['password'];
if (isset($_POST['username'])) {
$sql = "SELECT * FROM testdb.user WHERE username='$username' and password='$password'";
$result = mysqli_query($link,$sql);
if ($result);
{
$num=mysqli_num_rows($resultado);
}
if($num==1)
{
header("Location: welcome.php");
exit();
}else{
header("Location:wrong.php");
}
mysqli_free_result($result);
mysqli_close();
}
?>
答案 0 :(得分:2)
这是因为您在发出重定向之前发送输出。一旦开始打印HTTP消息正文,就无法更改HTTP标头。
// echo "Login Successful"; // remove this line and all other HTML
header("Location: welcome.php");
exit();
基本上,您必须重新构建程序,以便在提交表单时您不会将输出发送到浏览器。
伪代码示例:
if user has submitted the form then
authenticate user
if authentication is successful then
redirect user to welcome.php
else
show login page and error message
else
show login page
答案 1 :(得分:1)
认为这可能有助于robbmj提供的真实答案
创建3个文件夹...
在Views文件夹中,创建一个名为&#34; Login.php&#34;
在php页面中粘贴你的html表单:
<!DOCTYPE html>
<head>
</head>
<body>
<div class="container">
<form class="form-signin" role="form" action="<?=$_SERVER['PHP_SELF']?>/Controllers/Login.php" method="POST">
<h2 class="form-signin-heading"><center>Bienvenido!</center></h2>
<input type="text" name="username" class="form-control" placeholder="Username" required="" autofocus="">
<input type="password" name="password" class="form-control" placeholder="Password" required="">
<div class="checkbox">
<label><input type="checkbox" value="remember-me"> Remember me </label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
</form>
</div>
</body>
</html>
在Models文件夹中,创建一个名为SQLDbContext.php
在该文件中放置代码如下:
class SQLDbContext
{
public $link;
public function Connect()
{
$this->link = mysqli_connect( "localhost", "root", "root", "testdb")
or die ( "error" . mysqli_error( $enlace ) );
}
public function __Destruct()
{
mysql_free_result($result);
mysql_close();
}
}
在Models文件夹中,创建一个名为AuthenticationRepository.php
在该文件中,放置代码如下:
require_once( "SqlDbContext.php" );
class AuthenticationRepository extends SQLDbContext
{
public function __Construct()
{
$this->Connect();
}
public function GetUsersByUsernameAndPassword( $username, $password )
{
$sql = "SELECT * FROM testdb.user WHERE username='$username' and password='$password'";
$result = mysqli_query( $this->link, $sql );
return $result;
}
}
在控制器中创建一个Login.php文件(您会注意到我在“登录”视图中将您的操作更改为/Controllers/Login.php
在该php文件中,将您的逻辑放入登录:
require_once( "../Models/AuthenticationRepository.php" );
$authenticationRepository = new AuthenticationRepository();
$username = $_POST[ "username" ];
$password = $_POST[ "password" ];
$usersInDb = $authenticationRepository->GetUsersByUsernameAndPassword( $username, $password );
$num = mysqli_num_rows( $usersInDb );
if( $num == 1 )
{
header("Location: Views/Welcome.php");
}
else
{
// Set a $_SESSION here and in the Views/Login.php check for that $_SESSION being set
header("Location: Views/Login.php");
}
备注:强>
- 您会注意到在发出标题(...)之前没有任何内容回显到屏幕上
- 你会注意到所有的逻辑都被划分了(错误但它会让你开始)
- 你仍然需要进行SQL注入检查和验证等,但我会留下你做伙伴
通过这一切,您可以避免目前遇到的很多问题......您可以在这里做很多事情来改进这些代码,事实上,上面的代码真的不太热或者,但是它朝着正确的方向迈出了一步......分离出你所有的东西......看看http://www.laravel.com这是一个MVC框架,它可以帮助你不要太过于夸大其词:)< / p>