Page redirecting???
Moderator: General Moderators
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Why do you need to redirect after you output HTML? Anyways add a call to ob_start() at the top of your script
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
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.
~~~~~~~~~
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.
~~~~~~~~~
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?No0b wrote:And I put i after still theres that error.
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..
@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]);
?>- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
yes it was, this was before you posted your script, that you thought people were going to hack into.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?!
i suggest you read about the header() function and find out what it does.
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
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.
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.