如何在用户无需登录时直接访问文件时重定向登录页面

时间:2015-08-04 10:15:30

标签: javascript php

我有文件夹结构

login.php
index.php
public/test.txt
public/hello.csv

显示内容当用户转到网址http://myApp/public/test.txt时 登录用户可以在网址http://myApp/public/test.txt中查看内容 我希望在用户没有进入时重定向login.php页面。

1 个答案:

答案 0 :(得分:2)

不可能直接。 你必须改变你的结构:

www/public/login.php
www/public/index.php
www/public/read.php
www/public/.htaccess
www/datas/test.txt
www/datas/hello.csv

www / public目录是你的www-root,url无法访问目录www / datas。

文件read.php使这个:

call like : read.php?data=test.txt
- check if the user is logged, if false redirect login
- check if the file exists in www/datas
- check the format of get data (just a file, not a path)
- change the header Content-tye with the mime-type of the reading file
- send the file content

你的.htaccess有这样的规则:

RewriteRule ^public/(.*)$ read.php?data=$1 [NC,L]

因此,您可以使用网址http://domain.com/public/test.txt显示文件,并且read.php文件会自动检查身份验证。