【问题标题】:Restrict PHP page from specific user限制特定用户的 PHP 页面
【发布时间】:2018-07-21 22:13:40
【问题描述】:

假设我有一个名为 users 的数据库,它有 3 个用户:

  uid | a_id | username | password
  1   |  1   | William  | 123
  2   |  2   | Joe      |321

a_id指的是“access id”,意思是1是admin,2是guest。

我希望 a_id 为 2 的用户无法看到或点击 examplepage1.php

这是我用于登录的代码:

 <?php
 try {
 $db = new PDO('mysql:host=localhost;dbname=login', "root", "");
 } catch (PDOException $e) {
 echo $e->getMessage();
 }

    $uid = $_POST['username1'];
    $pwd = $_POST['password1'];

    $sql = "SELECT * FROM `users` WHERE  `username` = :username1 AND `password` = :password1";
   $statement = $db->prepare($sql);
    $userData = [
'username1'=>$uid,
'password1'=>$pwd,

    ];

   $statement->execute($userData);

   if($statement->rowCount() > 0){
   session_start();
   $_SESSION['admin']= $uid;
   $_SESSION['logged'] = true;

   header('Location: indextemplate.php');
   exit();
 }

   elseif ($uid!=$idvariable&$pwd!=$idvarible){
   header('Location: loginform.php?error=empty2');
  exit();
 }
 ?>

在每一个页面的开头我都会放:

<?php
  session_start();
   if(!isset($_SESSION['admin'])){
   header('location:index1.php');
   }
   else
   { 
 *my website here*
  }
 ?>

【问题讨论】:

标签: php mysql


【解决方案1】:

在您使用用户名和密码登录时存储您的a_id。并检查

<?php
  session_start();
   if(isset($_SESSION['a_id']) && $_SESSION['a_id'] == 1){
   header('location:index1.php');
   }
   else
   { 
 *my website here*
  }
 ?>

【讨论】:

    猜你喜欢
    • 2016-07-02
    • 2010-11-01
    • 2011-11-25
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多