简体   繁体   中英

How to Make The Page Only Accessible Once Logged in PHP

I am new to PHP and trying to put together a protype for an assignment. I am using the following login script together with a simple form fill for username and password. Once it checks the username and password against the database, if they match it takes the user to the welcome.php page. How can I make the welcome.php only viewable if a user has successfully logged in?

<?php
include("web_config.php");

 session_start();

  if($_SERVER["REQUEST_METHOD"] == "POST") {
  // username and password sent from form 

  $myusername = mysqli_real_escape_string($mysqli,$_POST['username']);
  $mypassword = mysqli_real_escape_string($mysqli,$_POST['password']); 

  $sql = "SELECT UserAccountID FROM UserAccounts WHERE Username = '$myusername' and Password = '$mypassword'";
  $result = mysqli_query($mysqli,$sql);
  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
  $active = $row['active'];

  $count = mysqli_num_rows($result);

  // If result matched $myusername and $mypassword, table row must be 1 row

  if($count == 1) {
   echo "User logged in."; header("location: welcome.php");
   } else {


         $error = "Your Login Name or Password is invalid";
      }
   }
 ?>
  <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php    echo $error; ?></div>

您可能想看看在PHP登录脚本使用会话和会话变量,该脚本已经包含有关此主题的许多有用信息。

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