Jump to content

redirecting with headers never works


viperjts10

Recommended Posts

I've always never understood the use of headers. All I know is that it has to be the first thing output using php, and there must be no white space etc..

 

But how can that be if I use the header in an 'if' statement or whatnot. Here's what I'm trying to do...I'm using a simple 'if' statement to see if the user logged out:

 

		if($session->is_logged_in())
	{
		/* Kill session variables */
		$session->logout();
		//$_SESSION = array(); // reset session array
		redirect_to('index.php');
		//echo "<meta http-equiv=\"Refresh\" content=\"0;url=index.php\">";
	}

 

My redirect function is below:

 

	function redirect_to($location = NULL) {
	if ($location != NULL) 
	{
		header("Location: {$location}");
		exit;
	}
}

 

How can I possibly use a header without having any whitespace before hand if I need to test a condition first?

 

Link to comment
Share on other sites

function redirect( $url ){
if (! headers_sent( ) ){

header( "Location: ".$url );
exit( 0 );
}
echo "<script language=Javascript>document.location.href='".$url."';</script>";
exit( 0 );
} 

redirect('http://wizecho.com'); //redirect to any url

Link to comment
Share on other sites

Hi  viperjts10,

I ran your code and the redirection worked for me.

 

The only thing i added was a class to create the session object with methods is_logged_in() and logout() both of which returned true.

 

I'm not sure why you are having problems but it must be something in the code preceeding the snippets you posted.

 

The full code I used is below.

 

All the best,

Fergal

 

<?php

class session
{
    function is_logged_in()
    {
        return true;
    }
    
    function logout()
    {
        return true;
    }
}

$session = new session();

if($session->is_logged_in()) {
        /* Kill session variables */
        $session->logout();
        //$_SESSION = array(); // reset session array
        redirect_to('index.php');
        //echo "<meta http-equiv=\"Refresh\" content=\"0;url=index.php\">";
}

function redirect_to($location = NULL) {
    if ($location != NULL) 
    {
            header("Location: {$location}");
            exit;
    }
}

Link to comment
Share on other sites

It's nothing to do with the server. When you send output to the browser (even white-space) the response headers (your redirects, cookies, etc.) are sent first. This means once output has been sent, you're unable to set anymore response headers.

 

I don't know if this is just from copying the code into the post, but I can see an empty line above your PHP tag. quite possibly this is the cause of your problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.