Php 用户名和密码字段工作不正常

Php 用户名和密码字段工作不正常,php,html,laravel,authentication,post,Php,Html,Laravel,Authentication,Post,我试图创建一个简单的登录页面,如果用户名和密码都等于“admin”,那么我想重定向到另一个名为“admin_page.php”的页面。我有下面的代码,但出于某种原因,无论我在用户名和密码字段中输入什么,登录都可以工作,即使其中有空的。 谁能告诉我为什么会这样? 这是我的密码: <body> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $user = $_POST['username']; $pass =

我试图创建一个简单的登录页面,如果用户名和密码都等于“admin”,那么我想重定向到另一个名为“admin_page.php”的页面。我有下面的代码,但出于某种原因,无论我在用户名和密码字段中输入什么,登录都可以工作,即使其中有空的。 谁能告诉我为什么会这样? 这是我的密码:

<body>

<?php


 if ($_SERVER["REQUEST_METHOD"] == "POST") {

  $user = $_POST['username'];
  $pass = $_POST['psw'];
  if (($user === "admin") && ($pass ==="admin")) {
           header("Location: admin_page.php");

  } else {
  echo("error ! please enter correct data");
}

  echo $user;

}

?>

<div class="bg">

    <div class="a">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype='multipart/form-data'>
{{ csrf_field() }}
    <input type="text" placeholder="Username" name="username">
      <input type="Password" placeholder="Password" name="psw">
      <button type="submit" name="submit">Login</button>
  </form>
</div>

</div>
</body>
index.php

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

问题在于检查请求参数的位置

你的观点

<body>



<div class="bg">
     @if(count($errors) > 0)
         @foreach($errors as $error)
             <p>{{$error}}</p>
         @endforeach
     @endif
    <div class="a">
    <form method="post" action="/" enctype='multipart/form-data'>
{{ csrf_field() }}
    <input type="text" placeholder="Username" name="username">
    <input type="Password" placeholder="Password" name="psw">
    <button type="submit" name="submit">Login</button>
</form>
</div>

</div>
</body>

但这不是解决问题的方法,它只是解决了你目前的问题。我建议您考虑使用laravel的内置身份验证

除此之外,您不需要在表单标记上添加
enctype='multipart/form data'
,除非您使用它上载文件。无论如何,代码本身应该可以工作,并且只有在凭据匹配时才重定向您。您是否进行过调试以检查$\u POST中实际接收到的值,以及代码实际采用的路径?现在还不清楚web.php与这个btw有什么关系。您也在使用某种框架吗?如果是这样的话,那么它可能已经提供了用于身份验证和正确重定向到视图的内置机制。@ADyson,即使我删除了标题(位置…)并按下登录按钮,我仍然可以访问admin_page.php!!我不知道为什么会发生这样的事,所以现在很清楚你在用拉威尔。所以,正如我之前所说的,停止尝试创建自己的登录页面。使用现成的功能。你现在的尝试至少在一定程度上是失败的,因为你没有正确地坚持MVC框架,并试图进行其他类型的重定向。这不是它在那种框架下的工作方式。使用框架提供的工具。@MaxMuster实际上已经修改了您的编辑,但尚未完全批准,并对其进行了修改,以删除一些不必要的标记,并将其添加回HTML中,该标记仍然相关:-)。因此,是的,您首先到达了那里,但是在我检查并改进之前,您的更改对任何人都不可见。为什么我在“return view('admin');”行上出现以下错误?ParseError语法错误,意外的“return”(t_return)?我有一个拼写错误,缺少if end括号。我在inokay中添加了它,所以在那里我会检查用户名和密码是否与admin相同?我更新了我的答案以检查输入并返回错误。我不明白为什么登录仍然使用任何输入。谢谢你的回答,任何真正帮助我理解的概念
<body>



<div class="bg">
     @if(count($errors) > 0)
         @foreach($errors as $error)
             <p>{{$error}}</p>
         @endforeach
     @endif
    <div class="a">
    <form method="post" action="/" enctype='multipart/form-data'>
{{ csrf_field() }}
    <input type="text" placeholder="Username" name="username">
    <input type="Password" placeholder="Password" name="psw">
    <button type="submit" name="submit">Login</button>
</form>
</div>

</div>
</body>
 Route::post('/', function (Request $request) {
   $errors = [];
   if($request->has('username') && $request->has('psw')){
        if($request->input('username') === 'admin' && $request->input('psw') === 'admin'){
          return redirect('/admin_page');
        }

        else{$errors[] = "Invalid login attempt";}

   }


   return view('admin', ['errors' => $errors]);
});