Jump to content

header('location ');


canadabeeau

Recommended Posts

Hi, I have a PHP function that is called in another PHP page.

That is index.php calls func_my();

 

func_my() works like this:

	
function func_my(){
$method = $_SESSION['data'];
unset($_SESSION['data']);
list($one, $two) = split("#", $method, 2);
if($one == "0"){
echo "Goodday";
} else {
header('location: http://www.google.com.au');
};
};

 

Except the header redirect is not rdirecting to google, it just loads the page with no echo becuase it !=0 and does nothing,anyone can you please help with this ssue.

 

Thankyou in advance

Link to comment
Share on other sites

UPDATE:

I did as asked above remove the two ";" and adde4d exit;

However I forgot to add that when I run this function it does not load anything below it (continue the oage. For example after the function I have text and if I run the function so it does the redirect not only does it not redirect it does no load the text below, so it is kind of stopping. More help would be greatly appreciated

Link to comment
Share on other sites

To you have php tags opening and closing above it with html?

Turn on error_reporting(E_ALL); in the top of the script. Does it say, Cannot set headers, headers already sent?

if so you cannot put a header in a place where you already have outputted html. Thats why you should separate your html from business logic.

Link to comment
Share on other sites

Warning: Cannot modify header information - headers already sent by (output started at C:\inetpub\wwwroot\_inc\php\nav.php:27) in C:\inetpub\wwwroot\apps\my\functions.php on line 47

 

What do I do to fix this, I need to redirect if the value in not 0, how can I solve this, what does this error actually mean for me?

Link to comment
Share on other sites

This requires to understand how the html page is sent. The first part of a page is called the headers, which is sent first, then after that you can buffer out the html. So logically you cannot set a header, when the header for the page has already been sent.

That whats PHP does when you open can close php tags with html, once you close the PHP tag and put html, the header is sent. After that point, you cannot set any more thigns that modify the headers, like cookies, sessions, and header().

 

 

You might want to installed LiveHTTPheaders for Firefox, if you use firefox, then you can what a header looks like.

Link to comment
Share on other sites

Well yeah if you have whitespace before the first <?php tag, then php will send the headers right away. It will assume you started the page with html.

 

Either you will have to restruture your page, maby do the that bit of logic in the first php tag,  or a cheap hack might be to use ob_start(); which stop sending of the header, until it hits ob_end_flush();

 

Then when you hit that header() redirect, use ob_end_clean() before it (Discards the html) then just send the header and exit;

 

This an example:

 

<?php
ob_start();
$test = '1';
?>

<p>Hello world foo bar</p>

<?php

if($test == '0'){

    echo "And goodday";
} else {

   ob_end_clean();
   header("Location: http://google.com");
   exit;


}
ob_end_flush();
?>

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.