Javascript 当用户不登录直接访问文件时,如何重定向登录页面

Javascript 当用户不登录直接访问文件时,如何重定向登录页面,javascript,php,Javascript,Php,我有文件夹结构 login.php index.php public/test.txt public/hello.csv 当用户转到url时显示内容 登录用户可以看到url中的内容 我想在用户未登录时重定向login.php页面。不可能直接登录。 您必须像这样更改您的结构: www/public/login.php www/public/index.php www/public/read.php www/public/.htaccess www/datas/test.txt www/datas

我有文件夹结构

login.php
index.php
public/test.txt
public/hello.csv
当用户转到url时显示内容 登录用户可以看到url中的内容 我想在用户未登录时重定向
login.php
页面。

不可能直接登录。 您必须像这样更改您的结构:

www/public/login.php
www/public/index.php
www/public/read.php
www/public/.htaccess
www/datas/test.txt
www/datas/hello.csv
RewriteRule ^public/(.*)$ read.php?data=$1 [NC,L]
目录www/public是您的www根目录,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有如下规则:

www/public/login.php
www/public/index.php
www/public/read.php
www/public/.htaccess
www/datas/test.txt
www/datas/hello.csv
RewriteRule ^public/(.*)$ read.php?data=$1 [NC,L]

因此,您可以显示带有url的文件,read.php文件会自动检查身份验证。

@AjeshVC既然存在asp.net问题,这怎么可能是重复的?