简体   繁体   English

PHP 如何成功登录重定向到无法访问的页面?

[英]PHP how do I make a successful login redirect to a page that is otherwise inaccessible?

I'm making a website for a local sportsclub and I want to make an admin page to edit things like planned activities etc. The plan is to have a url (www.example.com/login) where the admin has to login.我正在为当地的体育俱乐部制作一个网站,我想制作一个管理页面来编辑诸如计划活动等内容。计划是有一个 url (www.example.com/login),管理员必须在其中登录。 After a successful login the admin should be redirected to a new page (www.example.com/dashboard).成功登录后,管理员应该被重定向到一个新页面(www.example.com/dashboard)。 I have this login form in php but the problem is that you can still access the dashboard without being logged in by just typing the URL.我在 php 中有这个登录表单,但问题是您仍然可以通过输入 URL 来访问仪表板而无需登录。 This is my code这是我的代码

<!DOCTYPE html>
<?php
if(isset($_POST["submit"])){
$user=$_POST['username'];
$pass = $_POST['password'];
if($user=="admin" && $pass=="admin"){
  echo("username and password matched")
}else{echo("error! please enter correct credentials");}

}
?>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <form action="" method="POST">
    <table align="center">
      <tr>
        <td>username:</td>
        <td><input type="text" name="username" placeholder="Gebruikersnaam"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="password" placeholder="Wachtwoord"></td>

      </tr>
      <tr>
        <td></td>
        <td><input type="submit" name="submit" value="submit"></td>
      </tr>
    </table>
  </form>
</body>
</html>

So what I'm missing and what I don't know how to do is to make the form redirect to the dashboard after a successful login and to make the dashboard page forbidden if you're not logged in.所以我缺少的是我不知道如何做的是在成功登录后使表单重定向到仪表板,并在您未登录时禁用仪表板页面。

Thanks for reading!谢谢阅读!

Use session_start() and $_SESSION global variable.使用session_start()$_SESSION全局变量。

After successful login set a value there:成功登录后在那里设置一个值:

$_SESSION['signed_in'] = true;

and in the code of your pages check if the variable is set:并在您的页面代码中检查是否设置了变量:

if (!isset($_SESSION['signed_in']) || $_SESSION['signed_in'] !== true) {
   header('Location: /your_login_page.php');
   exit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM