Reputation: 163
if( isset($_COOKIE['user']) && !empty($_COOKIE['user']) ){
header("Location: ./");
}
This is my code for login page. What I want to achieve is whenever someone is logged in [if a cookie named user
exists], the user should be redirected to the homepage. I am using mod_rewrite to write URLs.
The problem I am facing is that whenever the cookie exists, and I go to localhost/project/login/
, it shows ERR_TOO_MANY_REDIRECTS:
However when I use localhost/project/login.php
it works fine.
Upvotes: 0
Views: 527
Reputation: 45914
header("Location: ./");
./
is a relative URL. Consequently if you send back Location: ./
in the HTTP response then the browser will interpret this as relative to whatever is currently displayed in the browser.
So...
When requesting localhost/project/login/
(note the trailing slash), the browser will redirect back to localhost/project/login/
(the same URL - redirect loop).
When requesting localhost/project/login.php
, the browser will redirect to localhost/project/
(your home page I assume).
To always redirect back to the homepage (/project/
) from any URL-path depth then you would need to at least specify a root-relative URL in the Location
header. For example:
header('Location: /project/');
(Or, you mess around calculating the current path depth from the request in order to construct a relative URL-path. But if you are going to do that then you might as well calculate the absolute URL of the homepage - or have this stored - which is arguably preferable.)
Upvotes: 1