Link to home
Start Free TrialLog in
Avatar of Bhupendra Rathore
Bhupendra Rathore

asked on

Warning: Cannot modify header information - headers already sent by (output started at /home/w3org/public_html/age/index.php:79) in /home/w3org/public_html/age/index.php on line 133

How can i solve this error :S

below is my index.php

and the line of the function 79 and 133 is-
index.php
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

The error message is a bit misleading, actually your output already begins in line 1, as your php script is a mix of HTML and php and any HTML parts of such mixed scripts are already output.

A header requires to be sent first, no output is allowed in advance.

What you could do is use output buffering, start your script with a php section even before the doctype:
<?php ob_start(); ?><!DOCTYPE...

Open in new window


Then in case you decide you want to send a Location header instead of the page, you do
.... else{
      ob_end_clean(); // clear the output buffer and don't output it
	 header('Location: http://www.runningprofiles.com/error.php');
                                                exit();
			}

Open in new window

And at the end of the script, for the normal case of no Location header output the buffer with
<?php ob_end_flush(); ?>

Open in new window

Bye, Olaf.
As an alternative to doing ob_start() which is not really what you want to be doing here you should be doing all your routing logic at the top of your page. In other words
<?php
if(isset($_POST['submit'])) {
	$day=isset($_POST['day']) ? $_POST['day'] : false;
	$month=isset($_POST['month']) ? $_POST['month'] : false;
	$year=isset($_POST['year']) ? $_POST['year'] : false;

	$age = (date("md", date("u", mktime(0, 0, 0, $day, $month, $year))) > date("md") ? ((date("Y")-$year)-1):(date("Y")-$year));
	if($age > 21) {
		header('Location: http://www.runningprofiles.com/error.php');
	}
	// Now you can put the rest of your logic here.
?>

Open in new window



What you have done is put your routing in the middle of your output which is not good practice. Routing happens before you start rendering the view.
Avatar of Buckminster Garcia
Buckminster Garcia

Turn on output buffering
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.