【问题标题】:htaccess rewrite with a cookie which is equal to a php script that returns a variablehtaccess 使用 cookie 重写,该 cookie 等于返回变量的 php 脚本
【发布时间】:2015-03-18 17:28:12
【问题描述】:

我的根文件下有一个文件夹,我正在尝试使用 cookie 限制对该文件夹的访问

我想要的是根据 cookie 值重写重定向,

例如在我的 htaccess 中我有这个

 RewriteEngine On
 // check if cookie is set and is equal to script.php
 RewriteCond %{HTTP_COOKIE} ^(.*)mycookie=[script.php]  [NC] 
 // allow access if above condition is met [ which i cant figure out how to implement]
 RewriteRule ^ http://example.com/folder/index.html [NC,L]
 // or if the conditions above are not met rewrite somewhere else

现在这是我想要的工作方式

当用户点击此 url..http://example/contents/folder/index.html 时,服务器应检查 mycookie 是否设置并等于 script.php 文件返回的变量,然后如果满足条件让他们进入或重写到另一个 url

script.php 返回数据库中的变量 每次用户登录并更新数据库中的行字段时都会生成此变量,以防止重复使用 cookie 变量。这意味着一个人将无法通过设置自己的变量或在其他地方复制和重用它们来操纵 cookie。

script.php 中的所有内容都经过处理,我的问题在于 htaccess 文件。 请帮忙

【问题讨论】:

  • 这只是一个奇怪的设置。您应该只将 url 重定向到 script.php,而不是返回值,而是将其与 mycookie 值进行比较并相应地重定向。将 PHP 逻辑溢出到 htaccess 中毫无意义!
  • 感谢@RaviThapliyal,那么我该如何重写上述条件以检查 script.php 是否在验证后返回并重写到另一个 url ..
  • 我的意思是在 PHP 中比较和重定向两者。这里不需要 htaccess!
  • 从我的想象中 - 文件夹在我的路由路径中,index.html 文件加载了一堆 swf 文件,除非我在目录文件夹中,否则我无法在浏览器中输出它[它不会加载 swf来自外部的电影],所以我试图在这里实现的是让 url 可见,但根据请求使用 apache 进行验证

标签: php apache .htaccess mod-rewrite cookies


【解决方案1】:

我在 cmets 中的建议是,您的 .htaccess 将简单地拦截请求并将其转发到 script.php 文件。然后您的脚本负责验证 cookie 以及最终显示的内容。

RewriteRule ^folder/index\.html$ /folder/script.php [NC,L]

现在,在你的 php 中,相应地验证 cookieincluderedirect

<?php
    if (isset($_COOKIE["myCookie"]) && verifyCookie($_COOKIE["myCookie"])) {
        include 'index.html';
    } else {
        header ("Location: http://domain.com/some/other/page.php");
    }
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多