Redirecting a page automatically in PHP

I want to redirect a page automatically in PHP


  include "base.php"; 
  $_SESSION = array(); session_destroy();
<meta http-equiv="refresh" content="=0;URL=index.php" />

Where base.php calls the database and starts the session:

  $dbhost = "localhost";
  $dbname = "login";
  $dbuser = "root";
  $dbpass = "";
  mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
  mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());  

When pressing logout, I am not getting back to index.php.


This should work, you had an extra = before 0:

<meta http-equiv="refresh" content="0;URL=index.php" />


As far as I know, HTML, JavaScript and PHP provide their own way of page / header redirection. Here are three examples, showing how to redirect to

# JavaScript:
<script type="text/javascript">
    window.location = "";
<meta http-equiv="refresh" content="0; URL=''"/> 

Note The 0 in content="0;, is a value for seconds. It tells the browser how many seconds it should wait before starting the redirect.

# PHP:


Note A PHP header() must be Always be placed before outputting anything to the browser; even a single empty space. Otherwise, it will cause the infamous "header already sent" errors.

you can put this on your PHP code:


Note that as per all headers, this must be placed before any output (even whitespace).

Meta refresh syntax is slightly wrong

<meta http-equiv="refresh" content="0;URL='<?php echo $_SERVER['HTTP_HOST']; ?>/index.php'">

More details here

The cleaner way is to send a http redirect header

More details here


header( 'HTTP/1.1 301 Moved Permanently');
header( 'Location: ' . $_SERVER['HTTP_HOST']  . '/index.php' );

Concerning absolute URIs in redirects W3C says

14.30 Location

The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource. For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource. The field value consists of a single absolute URI.

   Location       = "Location" ":" absoluteURI

An example is:



Need Your Help

Rails devise error "NoMethodError ... merge"

ruby-on-rails ruby-on-rails-4 devise rubygems

I was trying to make a User model with devise (so far in my application I have not had a problem with making Models or adding attributes). I run

In AWK, is it possible to specify "ranges" of fields?


In AWK, is it possible to specify "ranges" of fields?