4

Where do you use the command header()?

I have the following code at handlers/handle_login.php. The user has gone to the site from index.php which is the starting place.

 if(!$logged_in){
     header("Location: index.php");                                                                          
     die("You are not logged_in");
 }

If if-clause is true, I get a 404 error, since the header puts me to to handlers/index.php, instead of index.php.

2
  • Does your code make any other output before the header() call? if so, describe it.
    – fixmycode
    Commented Aug 9, 2009 at 2:18
  • From my tests, PHP does not appear to alter the value of the Location header before it gets sent to the client. The browser is either interpreting the current context to be handlers/, or your environment has some awesome Location header translation that I totally need, because I am sick of devising ways to dynamically create absolute URIs.
    – Zenexer
    Commented Jun 1, 2013 at 6:43

3 Answers 3

7

While I agree with nilamo and earl, I hope I can give a bigger picture:

Using relative paths can have very strange effects depending on where the browser 'thinks' it is in your site hierarchy. For example, assume the site has an index file '/index.php' but is configured to accept module and action in the URI path. You may very well have a url that looks like:

http://www.yoursite.com/forms/contact/

From this situation, returning a header like:

header("Location: index.php");

may very well cause the browser to try to request

http://www.yoursite.com/forms/contact/index.php

which is obviously not what you want. For this reason, it's generally better to use '/index.php' as recommended above, or even better use the fully qualified URL when possible.

Hope this helps.

1
  • 8
    I would only add that the HTTP/1.1 RFC explicitly states that the URL given in header Location has to be absolute, and not relative : "The field value consists of a single absolute URI" (see w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30 ) ; even if browsers generally aggre to work with relative URLs, you should use absolute ones Commented Aug 9, 2009 at 12:31
4

Set the location to the complete URL of the index.php, not just the filename. According to php.net, this is the right way to do it, don't use relative paths. Here is an example:

 if(!$logged_in){
     header("Location: http://exampledomain.com/index.php");
     die("You are not logged_in");
 }
2
  • I am debugging my codes offline so I cannot use such URLs. However, I am not sure if the url like http://localhost/index.php works similarly as your url. Commented Aug 9, 2009 at 2:40
  • You can try to use the $_SERVER['SERVER_NAME'] global variable.
    – Phoexo
    Commented Aug 9, 2009 at 3:28
1

Try using '/':

if(!$logged_in){
     header("Location: /index.php");                                                                          
     die("You are not logged_in");
 }

Without a slash, it is assumed that you're referring to something in the current directory. By sticking that slash at the front, you're explicitly referring to the file at the root of the site. Since the page is 'index.php', you could just as easily use "header('Location: /')".

1
  • 1
    Downvoted because HTTP spec says the location header must be an absolute URL. If you use relative, it may or may not work. Commented Nov 3, 2011 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.