May 1, 201016 yr So i was wondering u see i dont use ob_start unless its to prevent stuff from being sent before i use header but i was wondering after i use ob_start and the page redirects to a new page should i use ob_flush on the next page to flush the output buffer thanks in advance
May 2, 201016 yr No. That said, you shouldn't actually be outputting anything before a header anyway. A well designed application structure will ensure that this never happens. You should take a look into the MVC architecture if you haven't already
May 3, 201016 yr Author No. That said, you shouldn't actually be outputting anything before a header anyway. A well designed application structure will ensure that this never happens. You should take a look into the MVC architecture if you haven't already Well i am curious how do u avoid this when u need say a header("Location:index.php"); at the bottom of the page but u have already echoed stuff above
May 3, 201016 yr I don't see why this would ever be the case. Do ALL of your logic before your content. You don't have to echo out anything before a header. If you provide some code as an example I'll show how easy it is to change it so that you don't echo before your content is output
May 3, 201016 yr Author I don't see why this would ever be the case. Do ALL of your logic before your content. You don't have to echo out anything before a header. If you provide some code as an example I'll show how easy it is to change it so that you don't echo before your content is output Ok heres some for the login of my blog <?php /* @Check if login form was submitted @Before doing anything else */ if(!isset($_POST['login'])){ /* @Redirect user to login page */ header("Location:login.php"); }else{ /* @Create some main variables @For the login system @Include globals file @Start Session */ session_start(); define("ALLOW_ACCESS",true); require_once ("global.php"); $user = (isset($_POST['user'])) ? $_POST['user'] : FALSE; $pass = (isset($_POST['pass'])) ? $_POST['pass'] : FALSE; /* @Check if User and Password fields are not empty */ if($user && $pass){ /* @Check to see if username and pass are correct */ if($user == $admin_username && $pass == $admin_password){ /* @Start the users session if all is good */ $_SESSION['ADMIN'] = $user; /* @Redirect to index page */ header("Location:index.php"); }else{ /* @Display error message if username or password is not correct */ echo "<p style='color:red;'>Username or password are incorrect!</p> <a href='login.php'>Go back to login page</a> "; } }else{ /* @Redirect to login page */ header("Location:login.php"); } } ?>
May 3, 201016 yr if (!isset($_POST['login'])) { /* @Redirect user to login page */ header("Location:login.php"); die(); } else { /* @Create some main variables @For the login system @Include globals file @Start Session */ session_start(); define("ALLOW_ACCESS", true); require_once ("global.php"); $user = (isset($_POST['user'])) ? $_POST['user'] : false; $pass = (isset($_POST['pass'])) ? $_POST['pass'] : false; /* @Check if User and Password fields are not empty */ if ($user && $pass) { /* @Check to see if username and pass are correct */ if ($user == $admin_username && $pass == $admin_password) { /* @Start the users session if all is good */ $_SESSION['ADMIN'] = $user; /* @Redirect to index page */ header("Location:index.php"); die(); } else { /* @Display error message if username or password is not correct */ $error = "<p style='color:red;'>Username or password are incorrect!</p> <a href='login.php'>Go back to login page</a> "; } } else { /* @Redirect to login page */ header("Location:login.php"); die(); } } if($error) echo $error; Notice how I've output the echo after all logic. Also, can you see that I've put die(); after each of the redirects? This is essential. Setting the header doesn't automatically redirect, it runs all the code after it too, which is why you put die(); after it to stop that from happening. A simpler method is to create a redirect function, for example function go($location) { header('Location: '.$location); die(); } Then just use go('index.php');
May 3, 201016 yr Author if (!isset($_POST['login'])) { /* @Redirect user to login page */ header("Location:login.php"); die(); } else { /* @Create some main variables @For the login system @Include globals file @Start Session */ session_start(); define("ALLOW_ACCESS", true); require_once ("global.php"); $user = (isset($_POST['user'])) ? $_POST['user'] : false; $pass = (isset($_POST['pass'])) ? $_POST['pass'] : false; /* @Check if User and Password fields are not empty */ if ($user && $pass) { /* @Check to see if username and pass are correct */ if ($user == $admin_username && $pass == $admin_password) { /* @Start the users session if all is good */ $_SESSION['ADMIN'] = $user; /* @Redirect to index page */ header("Location:index.php"); die(); } else { /* @Display error message if username or password is not correct */ $error = "<p style='color:red;'>Username or password are incorrect!</p> <a href='login.php'>Go back to login page</a> "; } } else { /* @Redirect to login page */ header("Location:login.php"); die(); } } if($error) echo $error; Notice how I've output the echo after all logic. Also, can you see that I've put die(); after each of the redirects? This is essential. Setting the header doesn't automatically redirect, it runs all the code after it too, which is why you put die(); after it to stop that from happening. A simpler method is to create a redirect function, for example function go($location) { header('Location: '.$location); die(); } Then just use go('index.php'); But aint session_start also considered as output before header
May 3, 201016 yr No. session_start() needs to be run before any output or you will get the same issue as you do with header().
May 4, 201016 yr ob_start has a lot of uses besides the header != fail fix. For one thing, it can speed up the request response cycle, you can use stuff like gzip/deflate to further improve performance. You can also use it to manipulate chunks of output - handy if you're handling view rendering in a home brew MVC application.
May 5, 201016 yr Just an added note: Whack a session_write_close in before redirecting, this will avoid annoying glitches.
Create an account or sign in to comment