我一直在尝试验证用户是否在访问某个html文件时登录.
例如,如果用户没有登录并且他正在访问something.html我想将他重定向到index.php并拒绝访问something.html直到他登录.
我一直在使用会话来跟踪登录的用户.感谢您的帮助.
在该文件夹中创建.htaccess
AddType application/x-httpd-php .html
Run Code Online (Sandbox Code Playgroud)
然后将此添加到每个html:
<?php
session_start();
if(!isset($_SESSION['logined']) || $_SESSION['logined'] === FALSE){
header("Location: login.php");
die();
}
?>
Then follows your original HTML content
<html> ......
Run Code Online (Sandbox Code Playgroud)
来自fred-ii的想法:
您也可以将其限制为单个文件而不是整个文件夹.
这样,请在.htaccess中使用它
<Files yourhtmlFilname.html>
AddType application/x-httpd-php .html
</Files>
Run Code Online (Sandbox Code Playgroud)
或特定的多个文件:
<FilesMatch "^(file_one|file_two|file_three)\.html$">
AddType application/x-httpd-php .html
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)
我刚刚测试过,该文件夹下的其他html文件将不会以PHP身份运行.