RewriteRule 正在破坏 $_SESSION

发布于 2024-09-18 06:27:18 字数 1375 浏览 5 评论 0原文

一切都工作正常,直到我添加了 .htaccess 文件。我想做的是将所有用户路由到他们的个人资料页面。因此 www.darudude.com/user1 路由到 www.darudude.com/userinfo.php?user=user1

我的 .htaccess 文件如下:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ userinfo.php?user=$1 [L,QSA]

但是,自从我添加此文件后,它就中断了我的会话。在每个页面上我都有一个初始化会话,并且会话存储引荐来源网址。这是处理重要部分的代码。

if(isset($_SESSION['url'])){
     $this->referrer = $_SESSION['url'];
}else{
     $this->referrer = "/index.php";
}
//this echo is used to debug why this thing isn't working!!
echo "<script>alert('".$this->referrer."');</script>";
/* Set current url */
$this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];

然后我使用这段代码返回到原始页面:

header("Location: ".$session->referrer);

例如,如果没有 .htaccess 文件,如果我通过其中一个页面登录,一切正常,我会被重定向回我登录的页面(即如果我从index.php登录,我会被重定向回index.php;如果我从faq.php登录,我会被重定向回faq.php)。使用 .htaccess 文件,我不断被发送到 /userinfo.php ,导致我认为我的重写规则出了问题,

这就是它应该如何工作: 加载index.php。 $_SESSION['url'] 设置为index.php
登录表单已制定,其操作重定向到 process.php
process.php $session->referrer 是从 $_SESSION['url'] 设置的
确认登录后,页面应使用以下方式重定向: header("Location: ".$session->referrer);

这就是它最初的工作方式,没有任何问题。

但是,创建 .htaccess 后,它似乎将我重定向到 userinfo.php。我想这和我的统治有关系。

有什么想法吗?

Everything was working fine till I added my .htaccess file. What I'm trying to do is route all my users to their profile page. So www.darudude.com/user1 routes to www.darudude.com/userinfo.php?user=user1

My .htaccess file as this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ userinfo.php?user=$1 [L,QSA]

However ever since I added this, it breaks my sessions. On every page I have a initialize a session, and the sessions stores the referrer. This is the piece of code that handles the important part.

if(isset($_SESSION['url'])){
     $this->referrer = $_SESSION['url'];
}else{
     $this->referrer = "/index.php";
}
//this echo is used to debug why this thing isn't working!!
echo "<script>alert('".$this->referrer."');</script>";
/* Set current url */
$this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];

and then I'm returned to the original page using this piece of code:

header("Location: ".$session->referrer);

So for example, without the .htaccess file, if I login through one of the pages everything works and I get redirected back to the page I logged in from (i.e. if I logged in from index.php I get redirected back to index.php; if faq.php, I get redirected back to faq.php). With the .htaccess file I keep getting sent to /userinfo.php leading me to thing its something wrong with my rewriterule

This is how its supposed to work:
index.php loads. the $_SESSION['url'] is set to index.php
a login form is enacted whos action redirects to process.php
process.php the $session->referrer is set from $_SESSION['url']
After the login is confirmed the page should redirect using: header("Location: ".$session->referrer);

This is how it worked originally without any problems.

However, after the .htaccess was created it seems to redirect me to userinfo.php. I think it has something to do with my rule.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习惯成性 2024-09-25 06:27:18

我不确定我是否理解这个问题,但是您使用的重写规则似乎将对 /index.php 的请求转换为对 /userinfo.php?user= 的请求/index.php 这可能不是你想要的。

I'm not sure if I understand the problem, but the rewrite rule you're using seems to turn the request to /index.php into a request to /userinfo.php?user=/index.php which may not be what you want.

别闹i 2024-09-25 06:27:18

这是因为您依赖于 $_SERVER['PHP_SELF'],它不包含查询字符串(URI 的 ?user= 部分)。它以前有效,因为您的页面不依赖查询字符串来唯一标识自己。您可以使用 $_SERVER['REQUEST_URI'] 代替,但请注意您不希望保留查询字符串而现在却保留的情况。

顺便说一句,您的 RewriteRule 正则表达式中的 \?* 的作用与它不存在时完全相同。

It's because you're relying on $_SERVER['PHP_SELF'], which does not include the query string (the ?user= part of the URI). It worked before because your pages didn't rely on query strings to uniquely identify themselves. You can use $_SERVER['REQUEST_URI'] instead, though look out for circumstances where you don't want the query string to be preserved and now it is being.

Incidentally, the \?* in your RewriteRule regex is doing exactly the same as thing as if it weren't there.

ζ澈沫 2024-09-25 06:27:18

您可以尝试使用 AJAX 登录,这样就根本不需要刷新页面。简单的谷歌搜索就会出现大量结果,见下文。即使您没有使用 jQuery(很多教程似乎都希望您使用),使用基本的 Javascript 仍然是可能的,事实上,这就是我在稍后将其转换为使用 jQuery 之前编写 AJAX 登录脚本的方式。

http://www.google.com/search?q=php+ ajax+登录

You could try logging in with AJAX, thus never having to refresh the page at all. A simple Google search will throw up plenty of results, see below. Even if you're not using jQuery (which alot of the tutorials seem to expect you to), it's still possible with basic Javascript, in fact that's how I wrote my AJAX log-in script before converting it to use jQuery later.

http://www.google.com/search?q=php+ajax+log-in

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文