Jump to content

Solved (thanks red)flush, then redirect


Ninjakreborn

Recommended Posts

A lot of the time's I use redirect quite a bit, to do little things, like if there not logged in, redirect them to the homepage. 
I am having troubles with it, and I also know why, I have a form, if it's successfully it redirects them back to the homepage, or so the client wants.  I originally took the easy way out, they submit it, then message say insert successfully, click here to return home.  Well he comes back and wants a redirect, but I don't know how.

What do I have to do with this flush I have heard about, in order to be able to get a successfully redirection.
Link to comment
Share on other sites

It's giving me a header's already sent error.
I know it's suppose to, because I have a bunch of validation before it, plus my config include.
In lot's of posts I heard people say, if you need something to redirect, after something was outputted, use some combination of obflush and some other stuff, I just don't know how.
Link to comment
Share on other sites

here... use this for every redirect... i'll save LOTS of time and agrivation...
[code]<?
function redirect($filename=".?op=main",$delay="0",$die="0"){
if((!headers_sent())&&($delay=="0")) header('Location: '.$filename);
elseif($delay=="0"){
  echo '<script type="text/javascript">';
  echo 'window.location.href="'.$filename.'";';
  echo '</script>';
  echo '<noscript>';
  echo '<meta http-equiv="refresh" content="0;url='.$filename.'" />';
  echo '<noscript>';
}else echo '<meta http-equiv="refresh" content="'.$delay.';url='.$filename.'" />';
if($die=="0") exit;
}
?>[/code]
Link to comment
Share on other sites

if youre using a 'header: location', it will not be actioned until the script ends. so if you have anything after the redirect line that displays, you'll end up with the error. after your header line, stick an 'exit':

[code]
<?php
header("Location: /somewhere.php");
exit;
?>
[/code]

in an ideal world, you should never do any validation like this after ANY form of content has been written to the screen. all your logic should be done and dusted before you even contemplate using echo or outputting HTML. this way, you can use the code I posted without too many issues and without even needing to use output buffering.

so
[code]
<?php
// ... all your validation stuff goes here
// ... if any errors, redirect code goes here

// ... include your header here
// ... body here
// ... footer here
?>
[/code]
Link to comment
Share on other sites

yes, but the format is like this.
Yours
[code]<?php
// ... all your validation stuff goes here
// ... if any errors, redirect code goes here

// ... include your header here
// ... body here
// ... footer here
?>[/code]
What I thought
[code]<?php
// ... validation
// ... record validation errors
// ... display errors if there are any
// ... if there are not then do database (file) work
// ... if the work fails, return errors
// ... if the work succeeds, he wants it to redirect back to the homepage so they can see
// ... the results for himself.
?>[/code]
Or is there something specific I am overlooking.
Link to comment
Share on other sites

if you want terrible messy code, then sure - put some display work, put some logic, put some more display work, a bit more logic, followed by a mix of everything else. without even seeing your code, i'm guessing it's gonna be quite unmaintainable. one thing i learned is whilst total seperation (MVC type thing) is not totally necessary, keeping some form of neat structure to your code goes a long way. even if i'm not using a template of some form, and not using MVC pattern, i'll always still make a point of doing ALL my logic first, and only then will i display the page.

try it, and see the difference yourself. instead of displaying the validation errors where you have them, store them in a var/array UNTIL it gets to outputting your template/HTML stuff, and THEN display the errors.
Link to comment
Share on other sites

Yes that is how I do if, for example.

at the top of the page of the same form, if it's single page I have something like
indenting off some because it's in the forum though
[code]<?php
if (isset($_POST['submit'])) {
  $errorhandler = "";
  if ($_POST['firstname'] == "") {
      $errorhandler .= "First name was left blank.<br />";
  }
  if ($_POST['lastname'] == "") {
      $errorhandler .= "Last name was left blank.<br />"
  }
  validate_email($_POST['email'], 1);
  if ($_POST['field1'] == "") {
      $errorhandler .= "Field 1 was left blank.<br />";
  }
  // more validation of whatever type or kind is needed like above
  errorhandler($errorhandler, $url); // this is the function I use to display my error messages, if the
  // error handler is set, it displays it, as well as a url (if it's on a different page), to go back and fix
  // errors
  if ($errorhandler == "") {
    // here I do my mysql_real_escape_string or whatever other preperation before database entry
    // db variables (insert, query excetera)
    if (mysql_query($insert)) {
      header("location: redirect"); // example, I redirect here
    }else {
      echo "Problem inserting into database";
    }


  }




}
?>[/code]
probably a few errors there, just an example. That is how, i needed it to resurrect at that point, if it was successfully and if there were no errors.
Link to comment
Share on other sites

then in the case of an error, providing your code terminates and never reaches the line:
[code]
      header("location: redirect"); // example, I redirect here
[/code]
you shouldnt have too many problems. as per my previous post, you need:

[code]
<?php
      header("location: redirect"); // example, I redirect here
      exit;
?>
[/code]
Link to comment
Share on other sites

please note too that the code i showed is not a one off. i dont have a single header redirect that doesnt have an exit following it, and the only reason i ever use ob_start is for collecting a layout/part of a layout in a variable for use while templating.

glad it worked.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.