【发布时间】:2015-08-21 17:24:50
【问题描述】:
是否可以从提交的 url 中获取查询字符串,将其存储在会话变量中,然后在启动 php 之前重定向到另一个 php 页面?
我正在寻找 .htaccess (Mod-rewrite) 中的解决方案。
用户输入网址
/home.php?id=fred
然后在 .htaccess 中 id=fred 被剥离并放入 $_SESSION['id'] = 'fred'
和之前被发送到/index.php
我曾尝试在 PHP 中使用以下内容,但这似乎失败了
home.php
<?php
$qs = $_SERVER['QUERY_STRING'];
parse_str($qs, $gets);
// there are reasons for not just moving the $_GET values direct to $_SESSION
sanitise_gets($gets); // filters the values for valid values into $_SESSION
// redirect user to real 'home' page
$url = 'http://'.$_SERVER['SERVER_NAME'].'/index.php';
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Location: ".$url,true,303);
exit();
function sanitise_gets((array) $gets) {
...
}
?>
另一个问题是 PHP 内部的重定向似乎只显示 index.php/?id=fred ! 用户永远不可能添加书签或返回 home.php
我想这个问题真的可以归结为:
你能写到 .htacces 中的会话 cookie 吗?
【问题讨论】:
-
使用奥卡姆剃刀哲学,你为什么要这样做?似乎很麻烦,没有什么原因,可能有更简单的方法。
-
id=敏感信息
-
@Aedix Rhinedale 此外,我从未赞同奥卡姆(或任何人)的哲学,即指示其他人应该做什么。我对找到问题的解决方案更感兴趣。
-
我只是在询问更多场景细节(没有敏感信息)以尝试以更简单的方式抽象您的问题,以便您可以更快地解决它。这就是哲学,方法越简单越好。不过没关系。
-
@Aedix Rhinedale 对不起,但它没有遇到那种方式......更像是为什么要打扰。对我来说,简单的解决方案是 .htaccess 中的一两行代码
标签: php .htaccess mod-rewrite