FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How To Use the Header() function





wangbin218200
I'm a raw beginner.

I put together, from two other scripts, a script to email the input from a form and to write it to a mysql database at the same time.

The original email scripts had three or more little 'if' routines to catch errors, upon which there would be a jump to a new page and an exit. The jump via this header() function.

When I stuck the two together I started to get this 'can't modify header information...' error.

Reading up on it I found it is because the script has already made some output before the header() func, being 'print' statements on the success of the sql write.

I commented out the print statements and everything is fine.

But it raises the question for me about how to do this thing - jump to another page out of an 'if' ?

It seems to me to be quite reasonable to write a script that does all sorts of things and prints out a sort of 'progress report' as it is going and then, eventually, decides to leap to another page because of something it has encountered, good or bad.

But header() won't do this, it seems.

So what will?

Or has that methodology got to be abandoned?

Does this also mean that you can't use header() twice even when you use it correctly? That is: you use header() before any output, then make the output and therefore can't use header() again?

regards
rohan2kool
you can use the header function as many times as you want BEFORE ur script gives any output.

next, well.. the way u said.. there's no way to do it, other than using the HTML meta object to refresh the current page.
kv
If you want to just do "something else" while in middle of printing something (like running onother script which outputs some other content), you can use include() instead of header(). header() always does a server side http redirection and sends fresh headers, not just change the script which is being executed. So, you can't do it if you have already sent headers. This is not a php feature, but is a webserver feature.

Quote:
Does this also mean that you can't use header() twice even when you use it correctly? That is: you use header() before any output, then make the output and therefore can't use header() again?


As long as you have not output anything, you can redirect multiple times.
Enom
The bad thing is that almost all web hosts block this php feature.
("Headers Already Sent" - I hate this message)
JayBee
Enom wrote:
The bad thing is that almost all web hosts block this php feature.
I don't think so. Every web host I have ever use didn't block this feature.
JayBee
there couldn't be any echo before calling header()
problem is also new line at the begining of file like
Code:

<?php
header("location: http://frihost.com");
?>

this could cause a problem --^

Code:
<?php
header("location: http://frihost.com");
?>

this not --^

You can use PHP output buffering
------------------------------------------
surround your code by:
at the begining of file place
Code:
<?php ob_start(); ?>

than place code here
for example - this will not cause any error (but without ob_start() it will cause an "headers allready sent" error)
Code:
<?php
echo "redirection example";
header("location: http://frihost.com");
?>

and at the end place this
Code:
<?php
$data = ob_get_contents();
ob_end_clean();
echo $data;
?>
hexkid
wangbin218200 wrote:
It seems to me to be quite reasonable to write a script that does all sorts of things and prints out a sort of 'progress report' as it is going and then, eventually, decides to leap to another page because of something it has encountered, good or bad.

But header() won't do this, it seems.

So what will?


Trying to do that really does not make much sense, sorry Wink

Imagine there's a script just like the following code and you access it.
Code:
<?php
// the $ok variable will be set to false if anything is not ok
$ok = true;

if (isset($_POST['username'])) {
  ### the user typed a password (possibly blank)
  // do_something();
} else {
  ### No username! Let's tell the user that
  echo 'You must supply a username.';
  $ok = false;
}

if (isset($_POST['password'])) {
  ### the user typed a password (possibly blank)
  // do_something();
} else {
  ### No password! Let's tell the user that
  echo 'You must supply a password.';
  $ok = false;
}

if (!$ok) {
  ### there was something wrong, so we'll just redirect
  header('Location: index.php'); // should be full URL
}
?>


Now, if all worked as you expected, when the browser sees the header to redirect it will open a new page. This usually happens 0.00004 seconds (just made that number up) after the errors have been displayed, and the user will not have time to even notice them.
What i usually do is use session variables.
Code:
<?php
session_start();
// ...
// the $ok variable will be set to false if anything is not ok
$ok = true;

if (isset($_POST['username'])) {
  ### the user typed a password (possibly blank)
  // do_something();
} else {
  ### No username! Let's tell the user that
  $_SESSION['error'][] = 'You must supply a username.';
  $ok = false;
}

if (isset($_POST['password'])) {
  ### the user typed a password (possibly blank)
  // do_something();
} else {
  ### No password! Let's tell the user that
  $_SESSION['error'][] = 'You must supply a password.';
  $ok = false;
}

if (!$ok) {
  ### there was something wrong, so we'll just redirect
  header('Location: index.php'); // should be full URL
}
?>
Code:
<?php // index.php
session_start();
if (isset($_SESSION['error'])) {
  foreach ($_SESSION['error'] as $msg) {
    echo $msg;
  }
  unset($_SESSION['error']);
}

// rest of index.php
?>


HTH
rohan2kool
Enom wrote:
The bad thing is that almost all web hosts block this php feature.
("Headers Already Sent" - I hate this message)


if u mean the php feature of allowing output before headers, then it's not a php feature, it's a CGI and HTTP specification.

if u mean the feature of headers, that's something if disabled no web server can ever work.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.