dougua9328 2014-01-24 19:33
浏览 43
已采纳

网站重定向,如果没有登录

Here is my login form. aka index.php

<form class="form-3" action="login.php?log=ok" method="post" >
   <input type="text" name="username" id="login" placeholder="Username">
   <input type="password" name="password" id="password" placeholder="Password"> 
   <input type="submit" name="submit" value="Submit">                     
</form>

And here is my login checker. aka login.php

<?php
require_once 'classes/Personel.php';
$personel = new Personel();
$personel->setUsername($_POST['username']);
$personel->setPassword($_POST['password']);
$personel->login();

header("Location: index.php");


// REDIRECT
session_start();
if (strcasecmp($personel->getRole(), "LTO") == 0   ) {
    $_SESSION['role'] = "LTO";
    $_SESSION['personel'] = $personel;
    header("Location: LTO");
}else if(strcasecmp($personel->getRole(), "LTFRB") == 0){
     $_SESSION['role'] = "LTFRB";
    $_SESSION['personel'] = $personel;
    header("Location: LTFRB");
}else if(strcasecmp($personel->getRole(), "LGU") == 0){
     $_SESSION['role'] = "LGU";
    $_SESSION['personel'] = $personel;
    header("Location: LGU");
}else if(strcasecmp($personel->getRole(), "ADMIN") == 0){
     $_SESSION['role'] = "ADMIN";
    $_SESSION['personel'] = $personel;
    header("Location: admin");
}
?>

now when i try to access any accounts from them i can easily open its index page and other pages even i'am not login. How can i prohibit that? and how can i avoid the url rewriting?

eg. the index page of admin

try to open my link the correct account is admin-admin also try a wrong one..

Big Thanks in advance.

  • 写回答

3条回答 默认 最新

  • duanqiao2006 2014-01-24 19:40
    关注

    First thing, you need to set the redirect to index.php in proper place, with some condition.

    I get your problem, but are you checking the session on each and everypage?

    You need to implement a check at the beginning of each page that whether the session is properly set or not. Else redirect back to index.php.

    You need to implement this code before each of your pages:

    session_start();
    if(isset($_SESSION['role'])){
       if($_SESSION['role'] != "ADMIN") { //change the "ADMIN" to your unique role per page
          echo "Access denied";
          exit();
       }
    else {
       header("Location: index.php");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch