TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   header(location) does not work? (http://www.talkphp.com/general/2018-header-location-does-not-work.html)

lesP 01-19-2008 09:49 PM

header(location) does not work?
 
This doesnt work:
PHP Code:

<?

header
('Location: index.php?mode=statistikker&mode2=statistikker&action='.$_POST[statistikpost].'&spil='.$_GET[id].'&overordnet='.$_GET[overordnet].'');

?>

I get this error:

Warning: Cannot modify header information - headers already sent by (output started

Andrew 01-19-2008 09:51 PM

With header(), you can't have any output or whitespace outputted to the browser before that function is called. If that's the entire code for the page, try getting rid of the blank lines. If it still doesn't work, put the <? and ?> on the same line as the function.

lesP 01-19-2008 09:54 PM

This file is included in another file.

lesP 01-19-2008 09:55 PM

Then what should I do? I want the script to do like this line.

EDIT: Maybe I could use some JavaScript to send the user to the page?

xenon 01-19-2008 09:56 PM

Then you must send that header before any actual output. If you can't do that, use another redirection method (like a meta tag - which will invalidate your html - or a javascript code).

lesP 01-19-2008 10:00 PM

What would this function look like in Javascript? With all the output. I'm not the best one to JavaScript

ETbyrne 01-19-2008 10:07 PM

Just do this:

PHP Code:

$url 'http://www.mysite.com';

echo 
"<script language=\"javascript\">
location.href=\"
$url\";
</script>"
;
exit; 
// Stops executing page, just stops if JS is disabled 


lesP 01-19-2008 10:12 PM

Well it doesnt quite work. How should I get the URL in?

Village Idiot 01-19-2008 10:32 PM

Dont use javascript, too many people have it disabled. Use this code

Code:

<META HTTP-EQUIV="Refresh"
      CONTENT="5; URL=html-redirect.html">

5 being the number of seconds before refresh (put 0 for instant) and URL being the url to go to

Alan @ CIT 01-19-2008 11:04 PM

What is being output / sent to the browser before the redirect header? If you are doing an instant redirect do you really need to output something first since I'm guessing no-one would see it?

Alan

xenon 01-19-2008 11:09 PM

Quote:

Originally Posted by Village Idiot (Post 9003)
Dont use javascript, too many people have it disabled.

lol...so how's in the '80s? Is it true that they run naked on the streets? :-D No, really now, who turns off Javascript nowadays (and what would be the purpose)?

Javascript redirection method:

Code:

function do_redirect( url )
{
    window.location = url;
}

..then, in your php code, you could do something like so:

PHP Code:

echo '<script type="text/javascript">do_redirect("http://someurl.com/apage.html");</script>'


Village Idiot 01-19-2008 11:12 PM

Quote:

Originally Posted by xenon (Post 9007)
lol...so how's in the '80s? Is it true that they run naked on the streets? :-D No, really now, who turns off Javascript nowadays (and what would be the purpose)?

A small percentage, but it still amounts a a lot of people. It is something you still need to cater to. They do it because security holes have came up, some of them recently. I see it as paranoia, but I'm not the client.

ETbyrne 01-19-2008 11:22 PM

Hello, that's why I included the exit; command so if for some odd reason JS is disabled then it just exits, stops. xenon's method works but so does mine. As for the putting in the URL...

PHP Code:

$url 'http://www.mysite.com'// *Cough*

echo "<script language=\"javascript\">
location.href=\"
$url\";
</script>"
;
exit; 
// Stops executing page, just stops if JS is disabled 


Village Idiot 01-19-2008 11:40 PM

You shouldnt stop execution because they disabled javascript, the site has to work fully for them.

ETbyrne 01-20-2008 03:43 PM

OK, then put in an echo with a link before the exit; command. Just keep in mind that the META method can be disabled too.

Salathe 01-20-2008 04:20 PM

Rather than working around the problem (ie, finding a new way to redirect) why not solve the initial problem and be done with it? Sending out headers should never, ever, come after any content has been sent out. Re-structure your PHP code so that the redirect can happen before anything is outputted and bam, no need to bother with resorting to HTML/JavaScript/ask-them-politely redirects.

Also -- a minor point in the grand scheme of things -- is that the value in the Location header should be a full URL: http://domain.com and all the rest. It will work with just index.php... but just because something works, doesn't make it right. :-)

TlcAndres 01-20-2008 06:44 PM

Quote:

Originally Posted by xenon (Post 9007)
lol...so how's in the '80s? Is it true that they run naked on the streets? :-D No, really now, who turns off Javascript nowadays (and what would be the purpose)?

Javascript redirection method:

Code:

function do_redirect( url )
{
    window.location = url;
}

..then, in your php code, you could do something like so:

PHP Code:

echo '<script type="text/javascript">do_redirect("http://someurl.com/apage.html");</script>'


I go around with javascript disabled o.O

Andrew 01-20-2008 06:53 PM

I sometimes go without JavaScript because I disable it to test sites that use it (to see if they work without it), and forget I turned it off.

ETbyrne 01-20-2008 06:54 PM

Quote:

Originally Posted by Salathe (Post 9072)
Rather than working around the problem (ie, finding a new way to redirect) why not solve the initial problem and be done with it? Sending out headers should never, ever, come after any content has been sent out. Re-structure your PHP code so that the redirect can happen before anything is outputted and bam, no need to bother with resorting to HTML/JavaScript/ask-them-politely redirects.

I agree with everything you said here. ^^


All times are GMT. The time now is 08:44 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0