简体   繁体   English

如何使只有登录PHP的页面才能访问

[英]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. 我是PHP的新手,正在尝试为任务分配一个原型。 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. 一旦针对数据库检查了用户名和密码,如果它们匹配,则将用户带到welcome.php页面。 How can I make the welcome.php only viewable if a user has successfully logged in? 如果用户成功登录,如何才能使welcome.php可见?

<?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登录脚本使用会话和会话变量,该脚本已经包含有关此主题的许多有用信息。

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

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