The header('Location') code

Please would someone explain the function of the header(‘Location: .’); code in the section below.

Many thanks,


  if (isset($_POST['categories']))
    foreach ($_POST['categories'] as $category)
      $categoryid = mysqli_real_escape_string($link, $category);
      $sql = "INSERT INTO jokecategory SET
      if (!mysqli_query($link, $sql))
        $error = 'Error inserting joke into selected category.';
        include 'error.html.php';

  header('Location: .');

Thanks to all who contributed. Especially Immerse.

Note that if you have any PHP errors appear before it gets to the header() statement, the redirect won’t actually happen. This is because the headers have already been sent to the browser in order to display the error messages.

I think ‘Location: .’ will tell the browser to reload the current location.

Normally (at least in my limited experience) it tells you what page to land on after execution of the script. E.g.

header (‘Location: thankyou/index.php’);

will send the user to /thankyou/index.php after the execution of the script.

I’m not sure if header('Location: .'); will do anything, though.

It should do but I would tend to specify where to go rather than leave it to chance or a server setting

Just for reference, the RFC(2616) states you should use an absolute address(URI) for the Location header, not a relative one.



I use ./ or …/

I imagine the “.” is like in directory searching, one means “this directory” and two means “parent directory”. So “Location: .” would mean go to this directory and then whatever the directory’s default “index” page is would be the one that’s served.

It is the most effective way to reload the default page in the current directory.

In the PHP Live course and in his books on PHP Kevin recommends a structure of one page per directory and the use of that statement whenever the page needs to be reloaded.