Click to See Complete Forum and Search --> : Curious on how to do PHP Redirect???


centrationinc
01-16-2008, 06:19 PM
What I have written is a function that basically says:

function logout() {

session_start();

$old_user = $_POST['valid_user'];
unset($_SESSION['valid_user'];
$result_des = session_destroy();

if(!empty($old_user)) {
if($result_des) {
/* this is where I want the redirect section to go */
}
else {
echo 'Could not log you out.'; // then display a link to your homepage
}
}
else {
echo ' You were not logged in so you could not be logged out.';
}
}


Any help would be great. All i need is the section for redirecting that would go inside the comments. Also, if anyone has any ways to improve the script and make it safer or what not please feel free to let me know. Thanks.

NogDog
01-16-2008, 06:26 PM
See the header (http://www.php.net/header)() function (in particular the "Location:" header).

centrationinc
01-16-2008, 06:32 PM
Ok i started reading the page and i noticed one major problem. The header() only works when it comes before any actual output of HTML. This will be after you press a button and you log out. So therefore you will already see some output. This is where i got confused beforehand.

andre4s_y
01-16-2008, 08:32 PM
Maybe http_redirect() function can do..

Or..
Using Output control functions such as : ob_start() function.
The manual says :

The Output Control functions allow you to control when output is sent from the script. This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data.

ss1289
01-17-2008, 01:24 AM
Try to use the http_redirect or maybe javascripts window.location() function like so
echo "<script type=text/javascript>window.location='example.php';</script>";
^^This is what I use.

scragar
01-17-2008, 01:39 AM
better still, use a meta refresh over javascript, not everybody has JS enabled.
<meta http-equiv="refresh" content="2;url=http://google.com/">

RGL
01-17-2008, 07:51 AM
I used:
<?PHP

echo ("<script type='text/javascript' language='JavaScript'>
setTimeout('Redirect()',3000);
function Redirect()
{
location.href = 'index.php';
}
</script>
");
?>

centrationinc
01-17-2008, 10:40 AM
Ok thanks to all... ummmm thats alot of different options..... so ill work my way down the list and Ill let you all know what works. Thanks

paulrogers250
01-17-2008, 11:45 AM
Was also wondering this, thanks

NogDog
01-17-2008, 10:30 PM
If there is some reason in your script for it to do a redirect, then there should normally be no reason to output anything prior to that redirect. Therefore, you should be able to arrange your code logic so that any such call to the PHP header() function comes before any normal output is generated.

If you simply cannot see a way to rearrange your code to do so, then start your script out with a ob_start() (http://www.php.net/ob_start) command so that all output is buffered until the end of the script.