PHP redirecting

Status
Not open for further replies.

mil1243

Beta member
Messages
5
Is there any code in PHP that shows a page for a specific time i.e 10 seconds
and automaticlly redirect the page to the other URL ?!
 
You don't need PHP to do it. This is a valid HTML tag I've used before:

<META HTTP-EQUIV="Refresh" CONTENT="10; URL=main.html">

This should be put in the <HEAD> tag of the page you want showing for 10 seconds or so. The "10" means the page will be redirected to the page in the URL field after 10 seconds ("main.html" here).

If you still want to use PHP, you can redirect the user to another site using

header("Location: somelink.php");

...but only if there are no HTML tags prior to this line in your code. To do it after a certain amount of time, it looks similar to the code inside the first tag up there:

header('Refresh: 10; URL=http://www.example.com');

Hope this helps.
 
You can use php for other stuff and insert javascript into the page for the redirect.

For instance this is a page I did that redirects after showing the results of a contact form:

PHP:
<?php
bunch of php code here
<script Language="JavaScript">
  <!--

       var URL   = "http://www.mysite.com"
       
       var speed = 3000

       function reload(){

       location = URL

       }

       setTimeout("reload()",speed);

  //-->
</script>

<p><strong>Redirecting in 3 seconds <br />
Click <a href="http://www.mysite.com">here</a> if you would not like to wait </strong></p>
<p> </p>

<?php 
}
?>

var URL is the link you want to redirect to
var speed is how long it will take (ms, 3000=3 secs, 5000=5 secs, etc.)
 
If you use
Code:
<?php

header('Location: blahblah.com');

?>
in mid-HTML, you'll get a "Headers already sent" error.

To fix that, you'll have to use output buffering.

Example:
Code:
<?php
ob_start();
header('Location: blahblah.com');
ob_end_flush();
?>

This will buffer the PHP and execute any PHP scripts between the ob_start() and ob_end_flush() functions before it does anything else.

It's best to put ob_start(); as the very first piece of code on your page, and put ob_end_flush(); at the very end, therefore buffering (executing) the PHP before anything else.
 
Status
Not open for further replies.
Back
Top Bottom