11

I want to redirect a page automatically in PHP

Logout.php:

<?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:

<?php
  session_start();  
  $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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Javeria Habib
  • 547
  • 2
  • 7
  • 17
  • 3
    [**Heads up about the `mysql_*` functions**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – PeeHaa Dec 25 '12 at 15:04

5 Answers5

34

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 http://google.com

# JavaScript:

<script type="text/javascript">
    window.location = "http://google.com";
</script>

# HTML:

<meta http-equiv="refresh" content="0; URL='http://google.com'"/> 

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:

<?php

header('Location: http://www.google.com');

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.

Community
  • 1
  • 1
samayo
  • 16,163
  • 12
  • 91
  • 106
16

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

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

Linky https://en.wikipedia.org/wiki/Meta_refresh

Ry-
  • 218,210
  • 55
  • 464
  • 476
cristi _b
  • 1,783
  • 2
  • 28
  • 43
7

you can put this on your PHP code:

header('Location:index.php');

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

Nick
  • 6,316
  • 2
  • 29
  • 47
gamehelp16
  • 1,101
  • 1
  • 7
  • 22
3

Meta refresh syntax is slightly wrong

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

More details here http://en.wikipedia.org/wiki/Meta_refresh

The cleaner way is to send a http redirect header

More details here http://en.wikipedia.org/wiki/HTTP_301

logout.php

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

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:

   Location: http://www.w3.org/pub/WWW/People.html

Source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77
  • Agreed meta not a good approach for redirection, by the way in your point of view (OFF TOPIC) is it good to stop caching the content with meta tags or it should be good with another approach? – soft genic Dec 25 '12 at 14:55
  • @soft genic I prefer headers because this can be read in HEAD requests and by servers like a proxy, not just by the retrieving browser. Also the overhead is slightly bigger which doesn't really make a difference tho (HTML can be gzip compressed, headers can't) – Michel Feldheim Dec 25 '12 at 15:02
  • @Pee Haa, thanks for the reminder. I've modified the code above – Michel Feldheim Dec 25 '12 at 15:07
1

In the case you need to redirect the web page with a PHP variable include you can do this: where $user[0] is PHP variable. In that way, the next web page user.php can get the value of the variable.

header('Location:./user.php?u_id='.$user[0]);

or

header("Location:./user.php?u_id=$user[0]");
Lucho
  • 221
  • 1
  • 6