开发者

RewriteRule is breaking $_SESSION

开发者 https://www.devze.com 2023-01-15 10:28 出处:网络
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/user1routes to www.darudude.com/userinfo.php?user=us

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 开发者_如何学Cis 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?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号