faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL : Questions in search of an Answer

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 5 people (80%) answered Yes
Recently 4 of 5 people (80%) answered Yes

Entry

Hi.. How do i redirect to a page from PHP script as I do in javascript using window.location="URL"

Nov 20th, 2002 08:13
Jaime Shaker, Bimbisar Oraon,


To redirect in php, you would need to use the header function.
http://www.php.net/manual/en/function.header.php

Example:

<?php
    header("Location: http://www.example.com/");
?>

However, this code will only work if zero data has been sent to the 
browser.  If anything has already been sent to the browser (including
white spaces), you will receive a header error.  Let's refer back to the
manual to find a solution.

<manual>
     "In PHP 4, you can use output buffering to get around this problem,
with the overhead of all of your output to the browser being buffered in
the server until you send it. You can do this by calling ob_start() and
ob_end_flush()  in your script, or setting the output_buffering 
configuration directive on in your php.ini or server configuration files." 
</manual>

Example:

<?php
    ob_start();
    if($x == true){ header("Location: http://www.gohere.com/"); }
    else { header("Location: http://www.gothere.com/"); }
    ob_end_flush();
?>

Hope this helps