Page redirecting???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

did you try removing the whitespace, like jshpro2 said?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post by No0b »

daedalus do I put the header after or before the html?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Noob, listen to feyd, it is your solution.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)..
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post 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?!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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]);
?>
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

nickman, why did you refer him to sleep(), what relevance does that have to the issue?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

he told me he didnt want it to redirect automatically so i told him to sleep the header.
owltech
Forum Newbie
Posts: 1
Joined: Fri Feb 17, 2006 12:23 pm
Location: South Carolina

Post 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.
Post Reply