简体   繁体   中英

Only allow access to page if user is loged in php

I have a PHP login page with the following code

<?php
   $msg = '';

   if (isset($_POST['login']) && !empty($_POST['username'])
      && !empty($_POST['password'])) {

      if ($_POST['username'] == 'username' && $_POST['password'] == 'password') {
        $_SESSION['valid'] = true;
        $_SESSION['timeout'] = time();
        $_SESSION['username'] = 'username';
        echo 'You have entered valid user name and password';
      }
      else {
        $msg = 'Wrong username or password';
      }
   }
?>

When you enter the correct login it will redirect you to another.php file that should only be accessed if you have logged in with the correct username and password. What is the best way to prevent people who have not logged in with the username and password from accessing this.

You can use a user type or role method to allow a user to access a certain page

Try this link https://codewithawa.com/posts/admin-and-user-login-in-php-and-mysql-database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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