Page 2 of 2

Posted: Thu Feb 09, 2006 5:37 pm
by nickman013
did you try removing the whitespace, like jshpro2 said?

Posted: Thu Feb 09, 2006 5:37 pm
by josh
Why do you need to redirect after you output HTML? Anyways add a call to ob_start() at the top of your script

Posted: Thu Feb 09, 2006 5:40 pm
by feyd
No0b:
Read the link I posted near the first reply.

jshpro2:
ob_start() is a band-aid. I really don't like seeing it recommended to resolve the header warning.

Fix the problem, don't hide it.

Posted: Thu Feb 09, 2006 5:44 pm
by No0b
daedalus do I put the header after or before the html?

Posted: Thu Feb 09, 2006 5:55 pm
by nickman013
Noob, listen to feyd, it is your solution.

Posted: Thu Feb 09, 2006 5:56 pm
by No0b
ok so the code

<?
sleep(10);
header('Location: http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\').'/show_login.php);
?>

didn't work. And I put i after still theres that error.

If anyone reads this please help me.

Posted: Thu Feb 09, 2006 5:58 pm
by josh
Your header should be the first thing on the page, before any output...

Also a call to exit() should immediately follow it so your script doesn't continue executing.. a common mistake for example is on delete.php to check if the user is an admin and if not redirect, some people will just assume since the user got redirected when in reality under certain conditions delete.php could carry out it's work.

Feyd: I would argue that ob_start() is a viable solution in some cases, let's say for example you want to redirect on a condition that depends on some data being selected from the database, and you need to output the data you selected if the user was not redirected. you have 3 options

1 Select from the database first of all and decide whether or not to redirect the script, then select it again for outputting

2 Select it once and build your output in a variable, then at the end of the script either redirect or output that variable

3 Use a call to ob_start() and just make sure to exit() after your header redirect


Options 2 & 3 would be achieving the same result and neither one is "bad" so to speak. Option 1 is bad optimization.


~~~~~~~~~
No0b wrote:And I put i after still theres that error.
Again, please make sure there is no output before that, do you have any whitespace before your <?php tag? Are there any echo() calls? Are you outputting anything at all? Why do you have a call to sleep() in there?

Posted: Thu Feb 09, 2006 6:07 pm
by feyd
Under proper programming, no output should be generated until all page logic has been processed. I have never needed to use ob_start() in any of my work. It was only used once, for separate things (a form of error handling)..

Posted: Thu Feb 09, 2006 6:26 pm
by No0b
Ok well the sleep() function was nicks idea.

So the header() function can't be called be after any output. So then how the heck does the devnetwork.net do it?!

Posted: Thu Feb 09, 2006 6:33 pm
by josh
devnetwork does not, if you are talking about after you make a post, that is a meta redirect.


@Feyd, but isn't output buffering just one method of making sure there is no output (when used properly)

ex..

Code: Select all

<?
ob_start();
if (1) {
?>output<?php
}
$msg[0] = ob_get_clean();

if (1) {
$msg[1] = 'output';
}

var_dump($msg[0]==$msg[1]);
?>

Posted: Thu Feb 09, 2006 7:03 pm
by nickman013
No0b wrote:Ok well the sleep() function was nicks idea.

So the header() function can't be called be after any output. So then how the heck does the devnetwork.net do it?!
yes it was, this was before you posted your script, that you thought people were going to hack into.

i suggest you read about the header() function and find out what it does.

Posted: Thu Feb 09, 2006 7:23 pm
by No0b
Ok so I figured it out thanks to your guys help. It's the meta tag not the header. Thanks everyone. See you on anouther post. :D

Posted: Thu Feb 09, 2006 7:25 pm
by josh
nickman, why did you refer him to sleep(), what relevance does that have to the issue?

Posted: Thu Feb 09, 2006 8:52 pm
by nickman013
he told me he didnt want it to redirect automatically so i told him to sleep the header.

Posted: Fri Feb 17, 2006 12:46 pm
by owltech
There are 3 kinds of redirects in CGI:

full - HTTP://www.xyz.com/pages/blah.php
absolute - /pages/blah.php
relative - ../pages/blah.php

The absolute stays on the server, and is very fast, as you might expect. That is, the redirect never goes back to the browser on the client's box, and is called "internal". The full always goes all the way back to the browser as a response header (here, browser, take this page and...). The relative always goes back to the browser, but depends on page relative context.

PERL scripts as targets of the redirect seem to work fine with all forms of redirects, but PHP apparently prefers full redirects. I am not sure why. It could be that the CGI does not send the target back thru the PHP resolution engine before sending it to the browser. Someone else can enlighten me about that.

So,

header ('http://www.xyc.com/pages/blah.php');

will work fine for PHP but not

header ('pages/blah.php');

or

header ('../pages/blah.php');

This WILL work, however:

print ("Location: /cgi-bin/blah.pl");

Note that if you are redirecting from a PERL script, you must not execute:

print ("Content-type: text/html") or any other content-type statement. Otherwise the browser will consider your redirect as data, and not command.

The world of redirect is tricky.