Jump to content

Warning: Cannot modify header information


moosey_man1988
Go to solution Solved by cyberRobot,

Recommended Posts

Hi Guys

 

I've across an issue With my project im working on and I cannot solve it :(

 

When accessing my reports page I get this:

 

Warning: Cannot modify header information - headers already sent by (output started at /var/www/virtual/crmtech.co.uk/minicrm/htdocs/header.php:104) in

 

I have eliminated the issues down to my javascript scripts in the header :| 

 

but I just cannot work out why it would be causing the issue.

 

here is the header.php

<?php
require ('secure/session.php');
require ('Functions/functions.php');

error_reporting(E_ALL);
ini_set('display_errors', 1);

?>

<!DOCTYPE html>
<html>
		<head>
	
	<!--favicon-->
	<link rel="icon" href="<?php echo url();?>Images/favicon.ico" type="image/x-icon" />
	
	<!--Style Sheets-->
	<link rel="stylesheet" type="text/css" href="<?php echo url();?>bootstrap/css/style.css">
	<link rel="stylesheet" type="text/css" href="<?php echo url();?>bootstrap/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="<?php echo url();?>bootstrap/css/custom.css">
	<!--Script Links-->
	<script src="<?php echo url();?>bootstrap/js/jquery/jquery.js"></script>
	<script src="<?php echo url();?>bootstrap/js/bootstrap.min.js"></script>
	<script src="<?php echo url();?>bootstrap/js/typeahead/typeahead.js"></script>
	<script src="<?php echo url();?>bootstrap/js/bootstrap-datepicker/bootstrap-datepicker.js"></script>
	
<!-- For Displaying Modals-->
<script type="text/javascript">
	$(".btn-group").find(':input:checked').parent('.btn').addClass('active');
</script>

<!--This is the datepicker -->	
<script type="text/javascript">
	$('.datepicker').datepicker({
    format: 'mm/dd/yyyy',
    startDate: '-3d'
	})
</script>

<!--lets use the better to typeahead for search forms -->
<script type="text/javascript">
	$(function (){
		$("#typeaheadCust").typeahead({
			source: function(customer, process) {
				$.ajax({
					url: '<?php echo url();?>Functions/Getter.php',
					type: 'POST',
					data: 'customer=' + customer,
					dataType: 'JSON',
					async: true,
					success: function(data) {
						process(data);
					}
					
				});	
			}
		});
	});

</script>

and here is the my reporting/main.php page

<?php 

//this is going to be the main section for reporting

include('../header.php');

//lets check the user is authorised to be here!

if ($userLevel > 3){
	
echo "you are authorised to be here!";	
} else {
	
echo "you are not allowed here naughty!";
//sleep(10);
echo "lets send you here ". SERVER_PATH;
header('Location: ../index.php');
	
}


?>

here is the page result, sorry i had to delete the server path :D

 

post-178672-0-96233000-1449145285_thumb.png

 

Any help would be massively appreciated Thanks!

Edited by moosey_man1988
Link to comment
Share on other sites

this is a very common error. if you had searched for it (there's even a thread from this time yesterday with the same error - http://forums.phpfreaks.com/topic/299626-cannot-modify-header-information/ ) you would have found what it means.

 

YOU CANNOT SEND ANYTHING* TO THE BROWSER BEFORE YOU USE A HEADER(), SESSION_START(), OR SETCOOKE() STATEMENT. the http protocol REQUIRES that any 'control' headers be sent to the browser before you send anything else to the browser.

 

* - anything includes a BOM (Byte Order Mark) character that your editor saved at the start of your file, any characters outside of <?php ?> tags, even if those characters are white-space characters that you cannot see or are not rendered for display by the browser, the <doctype tag and any other html/javascrpt/css markup are CHARACTERS being sent to the browser, anything your php code echos, prints, var_dump()s, or print_r()s, or anything that php outputs, such as a php error message.

 

your php header() statement must be located in your code before you start sending the html/javascript/css to the browser. your page control logic that determines what to do on the page, or even if you are going to stay on the page, should be near the start of your code. you should only produce and send html/javascript/css to the browser after you have determined if you are even going to stay on the page.

 

edit: see the following post for a suggested page layout that will help make your code fool proof - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095

 

your page access level check/header() redirect would be part of item #3 in that page layout. all your code for producing and outputting the web page would fall under items 5-8 in that list.

Edited by mac_gyver
Link to comment
Share on other sites

  • Solution

As mac_gyver mentioned, nothing can be outputted to the screen before calling header(). Since you're just redirecting the user, the header() call can be moved to the top of your script.

<?php 
//lets check the user is authorised to be here!
if ($userLevel > 3){
    echo "you are authorised to be here!"; 
} else {
    header('Location: ../index.php');
    exit;
}
 
 
//this is going to be the main section for reporting
 
include('../header.php');
?>
 
Note that I added the "exit" statement after the header() call to prevent the script from doing any further processing.
 
If you want to display a message before the page is redirects, you could look into using header() and "refresh". An example can be found here:
Edited by cyberRobot
Link to comment
Share on other sites

Wow I cant believe how simple that is! haha

 

I even wrote this a work around

function usercheck($userlevel){

	if ($userlevel > 3){
	
echo "you are authorised to be here!";	
} else {
$url = url(). "/dashboard.php";
	
die('<script type="text/javascript">window.location.href="' . $url . '?error=you are not Authorised to be here";</script>');
	
}
	
}

and then just called that function :)

 

but i think it would be a wiser option to put the command before the header :)

 

Thanks guys, sometimes it just takes a different pair of eyes to look at something differently or properly in this case :)

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.