Forum Moderators: phranque
I have the following rule that checks whether the visitor is authenticated:
RewriteRule ^controlpanel/(.*)$ controlpanel/checkLogin.php
After I am done authenticating the visitor, I use the header("Location: ...") command to proceed with the original request (REQUEST_URI). However, I end up in a loop.
What's the solution?
Also, would someone advise NOT to do authentication this way? My script checks for time between requests and other stuff, so that's why it's all done in 1 convenient location.
Thanks.
Next, if the URL you send with your Location header matches that RewriteRule pattern, it will loop externally (redirection-authentication loop).
To fix the first problem:
RewriteCond $1 !^checkLogin\.php$
RewriteRule ^controlpanel/(.*)$ controlpanel/checkLogin.php [L]
Jim
Am I dreaming or there is also a way to execute another redirect based on the result of the first redirect?
Let's say I do all my magic in PHP and then instead of redirecting the visitor using the "header" command, I let .htaccess do that? If PHP returns "true", then redirect to A, if "false", then to B.
by the way, the rule doesn't seem to work.
==============
in PHP I have the following:
header("Location: ".$_SERVER['REQUEST_URI']);
in .htaccess:
Options +FollowSymlinks
RewriteEngine On
# Control Panel rules
RewriteCond $1 !^checkLogin\.php$
RewriteRule ^controlpanel/(.*)$ controlpanel/checkLogin.php [L]
==============
it goes into a loop (in IE) and FireFox gives an error saying that a redirect won't work.
Secondly, you'll need Live HTTP Headers for Firefox to see what the server response codes and redirect URLs are, to work out where the redirect loop is coming from.
Your authentication script will need to set a client-side cookie that mod_rewrite can test, so that this rewrite->redirect loop can be avoided.
Jim
Let's say I authenticate the user and create a cookie; mod_rewrite will check for that cookie and if it doesn't exist, redirect to my PHP script. However, I also want to check time between requests and if its > N, then have PHP authenticate the user.
Basically, I am looking for a code that will:
1) redirect everything to a single PHP script
2) allow that PHP script to redirect to the original request without meeting a loop
Thanks.
Jim
One other reason why I want to redirect all requests through my script is because I checked if the visitor has JS enabled, and also if the current request came from the same IP as the previous. Basically, I check for a lot of things in my PHP script and I don't really understand how I can do this with a cookie. All the information is stored in the SESSION array.
If you don't know how to write and read cookies, the read the Netscape document I mentioned, and then search our PHP forum (and others) for cookie-handling code. That should give you a very good start on adding the necessary feature.
Jim
Perhaps I am not making myself clear: I have _1_ script that does it all. It checks for authentication, for time between requests, for JavaScript, for IP (previous request/current request), and bunch of other things. Reason? Because any of these parameters may change between requests.
So, that is the reason why I want _all_ requests to be redirected to that script. Once the check is done and everything is OK, the script will redirect to the location originally requested. However, this is where I get stuck: mod_rewrite starts looping.
If I understood you correctly, I will have to create a cookie EVERY time the request is redirected to my script, correct? So, let's say, the visitor makes 20 requests a minute, hence 20 times I will have to set a cookie? What about performance hit? 100 users x 20 requests/minute = 2000 set/read cookie operations.
You have stated that you want *ALL requests rewritten to your script" and that's what you mod_rewrite code does. If this results in a loop, it's because your script is continually "redirecting to the originally-requested URL" and causing a new HTTP request, which then gets rewritten back to the script. So as long as you rewrite *ALL* HTTP requests to your script, then it is up to the script to quit "redirecting to the originally-requested URL" when it no longer needs to do so -- and that is the function of the proposed cookie.
I'm sorry if it's not clear, but I can't say it any differently. It'll be up to you to understand what I wrote, even if I didn't write it well.
Jim