简体   繁体   English

有条件的重定向apache2

[英]conditional redirect apache2

I am setting up a webpage to host files for some users. 我正在设置一个网页来为某些用户托管文件。 Right now if you go to the directory blah/folder you will be presented with apaches default folder view. 现在,如果转到目录blah / folder,将显示默认的默认文件夹视图。 This folder view is ideal, however I would like to prevent people from going straight to the folders in the server directory. 此文件夹视图是理想的,但是我想防止人们直接进入服务器目录中的文件夹。

Right now I have a login page which starts a session and if the users logs in correctly will direct them to their top directory and set a session variable. 现在,我有一个登录页面,该页面启动会话,如果用户正确登录,将把他们定向到其顶层目录并设置会话变量。

I would like to have a way that whenever someone attempts to access a file or folder on my server, it checks for this variable and if it is not present it directs them back to the login page and if it is present shows them the default folder view. 我希望有一种方法,每当有人尝试访问服务器上的文件或文件夹时,它都会检查此变量,如果不存在该变量,则会将其引导回登录页面,如果存在,则将其显示为默认文件夹视图。

I was thinking I could use .htaccess to redirect to a php file that checks the session status, but I don't know how I would keep this from running in a loop. 我以为我可以使用.htaccess重定向到检查会话状态的php文件,但是我不知道如何避免使其循环运行。

Let's say you have a members page that you want only to be accessed when a user is logged in and a login page that can be accessed by anyone. 假设您有一个仅在用户登录时才需要访问的成员页面,以及一个任何人都可以访问的登录页面。

You can use $_SESSION[ ] to do this 您可以使用$ _SESSION []执行此操作

Create a header page that is included in both files and use session_start() 创建两个文件中都包含的标题页,并使用session_start()

Login page should look something like this 登录页面应如下所示

 <?php
  require_once 'header.php';
  if(isset($_POST['user']&&$_POST['pass'])
  {
    //validate the values and check if user login credentials are right
   $_SESSION['loggedin']='true';
  }
 ?>

Members page should have something like this 会员页面应该有这样的内容

<?php
 require_once 'header.php';
 if($_SESSION['loggedin']=='true')
 {
  echo'you can access this page member';
 }
 else{
 echo'Sending you back Mr';
 header('Location: http://somesite.com/login.php');
 }
?>

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

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