Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting a page automatically in PHP

Tags:

html

http

php

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.

like image 745
Javeria Habib Avatar asked Dec 25 '12 14:12

Javeria Habib


2 Answers

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.

like image 71
samayo Avatar answered Oct 25 '22 02:10

samayo


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

like image 16
cristi _b Avatar answered Oct 25 '22 02:10

cristi _b