Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Question about Ob_start

Featured Replies

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 :)

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

  • 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

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

  • 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");


} 

}

?>


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');

  • 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

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.

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.