Understanding cache limiter | headers already sent php warning - php

I have a site that is functioning fine, but just by refreshing the main page (index_3.php) my error log populates with two warnings.
[08-Oct-2013 11:36:09] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home2/mysite/public_html/mysubsite/index_3.php:7) in /home2/mysite/public_html/mysubsite/functions.php on line 12
[08-Oct-2013 11:36:09] PHP Warning: session_regenerate_id() [<a href='function.session-regenerate-id'>function.session-regenerate-id</a>]: Cannot regenerate session id - headers already sent in /home2/mysite/public_html/mysubsite/functions.php on line 13
I have researched enough to understand that something is sending the page data before the session begins, but I can't seem to root out the cause. functions.php is taken right from this tutorial site under "Create PHP Functions." http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL
Also loading on index_3.php is get_opwire.php which just places a table. The beginning of get_opwire.php looks like:
<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();
sec_session_start(); is a custom session start located in functions.php
When I try to rearrange the order of the sec_session to the top or shift those around the table breaks. Index_3.php is just the main page, mostly html that has includes for a submit form and get_opwire.php
Would someone be able to help point me to the problem?
Edit: index_3.php (from line 1 through )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<meta name="Generator" content="Xara HTML filter v.6.0.1.335"/>
<meta name="XAR Files" content="index_htm_files/xr_files.txt"/>
<title>index_3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" type="text/css" href="index_htm_files/xr_main.css"/>
<link rel="stylesheet" type="text/css" href="index_htm_files/xr_text.css"/>
<link rel="stylesheet" type="text/css" href="index_htm_files/custom_styles.css"/>
<script type="text/javascript" src="index_htm_files/roe.js"></script>
<!--[if IE]><script type="text/javascript" src="index_htm_files/prs.js"></script><![endif]--><!--[if !IE]>--><script type="text/javascript" src="index_htm_files/prs3.js"></script><!--<![endif]-->
<script type="text/javascript">var xr_nextpage=""; var xr_transition=0; var xr_transitiontime=0;var xr_prevpage="index_2.htm"; var xr_btransition=0; var xr_btransitiontime=500;</script>
<style type="text/css">.xr_pbd {position: absolute; border:none; left: 50%; margin-left: -380px;}</style>
</head>
EDIT 2: somewhere inside index_3.php
<html>
<body>
<div style="width: 480px; height: 175px; overflow: auto;">
<?php include 'get_opwire.php'; ?>
</div>
</body>
</html>

Try removing the closing ?> tag from all your included php files.

Something somewhere before session start and session regenerate id is putting something on the page. If you are sure you're not echoing or printing anything to the page, it's probably a blank space at the top of one of the files.
EDIT 1
Some text editors/word processors save files with extra invisible characters at the top due to encoding. Try copying and pasting the text of the files into new files in a plain text editor or text editor recommended for coding, such as Notepad++.
EDIT 2
So, those were the most likely solutions.... It says
output started at /home2/mysite/public_html/mysubsite/index_3.php:7
which means that it's line 7 of index_3.php where the output starts at. There is probably a space there or maybe some kind of error that causes output to be sent.
If you really can't find that invisible output, you can use buffering with ob_start etc. to catch all output. Put ob_start before your session start and use ob_end_flush later to display the page. But that's not a real fix for the fact that you're sending output without knowing it.
EDIT 3: "No output" includes any and all HTML.
If there's something in any file that is not inside PHP brackets <?php ?> then that counts. Your index_3.php file starts with HTML. That is output. You have to do get_opwire.php first.
EDIT 4
Concerning how get_opwire.php has both page-start stuff and the table printout, it's an example of why it's a good idea to separate your display code from your functionality code. You have three options:
Have separate files. You would have a file like page_start.php that does includes and session_start that you include at the very top of index_3.php, and a file like display_table.php that displays your table that you include where the table goes.
Turn the table into a function. You would wrap the table output inside a function, include get_opwire.php at the very top of index_3.php, then call the function down where you want the table.
Use output buffering. Output buffering catches the stuff printed out so that you can use it later. It would go like this:
top of index_3.php:
ob_start();
include get_opwire.php;
$table = ob_get_contents();
ob_end_clean();
where the table goes:
echo $table;

Related

Strange Warning with header - Location in php "Cannot modify header information" [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
Warning: Cannot modify header information - headers already sent by
(output started at
C:\wamp\www\ecommerce\base\classes\baseconfig.php:23) in
C:\wamp\www\ecommerce\base\classes\adminHelper.php on line 6
I know the root cause for the above warning. But iam not able to reslove it. My problem is different from other questions and many down-voted and closed topics.
The warning page is : index.php, is the only page that im dealing
with displaying and Setting data. this warning is shown when i called header('Location') on helper adminHelper.php
Im using a class coreConfig.php which sets and
gets the data for the index page.
And some helper classes too.
with the index page im using the same form , Same functions , same routines for setting data in a form. only data changes with the passed argument.
and this page works perfectly with all other set of data, except for one.showing the above warning. i cannot figure it out why this happens.
im formatting the output by using this function
public $mainTabLength = "\t";
public $tabLength = "\t\t";
public function printFormat($text, $class) {
if($this->useDivToPrint) {
echo $this->mainTabLength . "<div class=\"$class\">" . $this->tabLength . "$text\n";
echo $this->mainTabLength . "</div>\n";
}
else{
echo $text;
}
}
I dont want to use buffering of the codes too .. awaiting comments .
Note : I had formatted my php code by removing any white-spaces from the IDE
Update :
The problem was solved by inspecting the buffer output.As expected my code was setting some header information already before im calling header('Location'). But i was sure that i was not doing such a mistake, because my all other piece of data is working fine. But i was surprised to see the output of buffer for working set of data and the one that caused the warning :
form 1: adding new category (working fine header('Location'))
even i can see the following header information already sent :
<!DOCTYPE HTML>
<html>
<head>
<title>E-Commerce</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link href="../css/style.css" media="screen" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="../images/favicon.png"/>
<script type="text/javascript" language="javascript" src="../jses/jquery-1.11.3.min.js"></script>
<script type="text/javascript" language="javascript" src="../jses/scripts.js"></script>
<script type="text/javascript" language="javascript" src="../jses/datePicker.js"></script>
form 2: adding new product (not working header('Location') causing warning)
i can see the same header information already sent :
<!DOCTYPE HTML>
<html>
<head>
<title>E-Commerce</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link href="../css/style.css" media="screen" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="../images/favicon.png"/>
<script type="text/javascript" language="javascript" src="../jses/jquery-1.11.3.min.js"></script>
<script type="text/javascript" language="javascript" src="../jses/scripts.js"></script>
<script type="text/javascript" language="javascript" src="../jses/datePicker.js"></script>
This was the root cause that i was not able to detect a problem with my code.
why it worked for my form1 even though it already sent some header information already !!
Like I said in the comments, the issue is that you are having some output being sent before the call to header which is a no-no. This could include:
output from any included files, inlcuding all below and regardless of depth of included file (included file that includes another file that includes another file...etc).
a newline/space before or after <?php or ?> tags.
echo'ing anything.
even html output from outside of php tags.
a php error or warning.
These, and more, will all output something and will be considered "output before call to header". Because some of it can be hard to see, you might not even see it when viewing a page.
Your options are to either turn on output buffering or find some way to not display output during this request.
If you want to see what is being output (might help find the issue), you can do something like this for testing:
in the first line of the first script being requested (likely index.php, whatever file is requested by the browser), add a call to ob_start() right after <?php. If your file doesn't start with <?php then that is your problem right there.
on the line right before the call to header where you are getting your error, add something like file_put_contents("output.txt", ob_get_contents());.
This will get whatever output there was and write to to a text file. The file will likely run without errors this time (you did just turn on buffering) but it should still write any output. Also remember, it might just be a space or newline written. So if you open the file and see nothing, make sure to check if some non-display character was written.
Now it is just a matter of leaving on buffering or finding and removing that extra space.

PHP does some things twice when this meta tag is in the file when browsed by Firefox

I am wondering why php does certain things twice, instead of once, when a certain meta tag is in the html portion of the file and the file is browsed by Firefox.
The code is like this:
<? /*...normal php code, including writing record to MySQL...*/
send('dan#example.com',$subject,$body);
?>
<!DOCTYPE html><html>
<!--PROBLEM on next line-->
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head>
<title><?= $thisPage?></title>
<link href="<?= $cssURL?>css.freedom-and-purpose.css" rel="stylesheet" type="text/css" media="screen, projection" />
<?
include $dataPath . 'data.php';
?>
The result is TWO records written the database and TWO emails sent, whenever the page is called by FIREFOX. IE and Chrome not producing the problem.
There is a lot of other code in the program, but the reason I showed the portion above is that removing the line that starts with
<META...
solves the problem.
That meta tag is in there because one of the packages I run included it in their code sample.
So, what is that meta tag causes php to double do on DB-writes? And same thing on sending email?
Chances are this is actually a request for favicon.ico being caught by your main PHP file. Putting an empty file in favicon.ico or preventing your PHP from handling that URL should do the trick
enter code hereI would suggest you go through your code in some details and check its formatting.
Phil mentioned the meta tag about which I agree with. His suggestion of <meta charset="utf-8"> would be my preference.
Secondly the line sending the email looks odd. Single quotes aren't an option in php for data replacement, so the line send('dan#example.com','$subject','$body); would result in an email with the subject "$subject" and body just "$body".
Additionally send('dan#example.com','$subject','$body); appears to be missing a quote after $body.
I would advise you to move away from short php tags for opening and closing chunks of php <? ?> and get in the habit of <?php for clarity and to ensure the server you're using processes the code correctly.
Finally, I hope include $dataPath . 'data.php'; adds a </head> and a <body> to the html, as you're currently missing those too.

seo effect - include file with <html> tag inside

Maybe its a stupied question but i didnt fild any answer for it,
If i have file with the html tag head tag&meta.. and I include him in all my web page, its effect the seo of the site?
I have file named "start_html.php" that have this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="bla bla.">
<meta name="keywords" content="bla bla">
<meta name="author" content="bla">
<title>bla bla</title>
<!-- CSS: -->
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<!-- JavaScript -->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
I start every page with this line: <?php include('start_html.php'); ?>
When i check for code error on w3c validator its says that i dont have those tags so the page is not coded good.
If the code from that file is not being displayed in your web page then obviously your PHP code is incorrect and that file is not being included. You need to make sure you have error reporting on and displaying all errors as this will catch this for you.
See this StackOverflow question for how to enable error reporting.
You should have these meta and title tags on all your pages, so including them from PHP is certainly not a bad idea.
However, if the W3 validator tells you these tags aren't there, you should check your output. Perhaps start by 'show source code' in your browser, and see if the tags appear there.
If you try to send your source file for validation, where you have:
<?php include('start_html.php'); ?>
Of course you will get the expected result - no tags, because the source file must be parsed and handled by PHP.
You can give a working link for validation, or copy output in your browser after execution, save the file and send it.

Random whitespace in a PHP page (the client received page, but not the client-received source-code), but only when I use an include()

Ok, I am having the weirdest problem in history. I am making a website that works perfectly in HTML, but is having some REALLY odd behaviour when rendered from PHP - despite having the EXACT SAME client source code (I literally went through it character by character).
At first I thought I'd messed up something in my 'functions.php' file that I'm including, but I don't get any errors, and when I copy&paste the contents of that file into the place where the include('functions.php'); line is, the problem disappears.
Here's my code (with some HTML removed, this is all of the PHP):
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
include 'functions.php';
$footer = file_get_contents('footer.txt');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Removed</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<link type="text/css" rel="stylesheet" href="css/jquery-ui-custom.css" />
<link type="text/css" rel="stylesheet" href="css/main.css" />
</head>
<body>
<div id="header">
<div id="headerContent">
<div id="logo">
</div>
<?php
echo trim(GetNav());
?>
</div>
</div>
<div id="content">
<?php
$NumberOfNewsItems = 2;
echo trim(GetNews($NumberOfNewsItems));
?>
</div>
<?php
echo trim($footer);
?>
</body>
</html>
The functions GetNav and GetNews grab info from the database so I'm not going to show the code from them, plus, as I mentioned earlier, if I don't include the file but instead copy it's contents to the place I make the include call then most of the whitespace disappears (there's still some where the 'echo $footer' call is.
Basically, when you look at the source code, everything looks fine. Where stuff gets incredibly strange is when you hit F12 to look at the dev tools and the elements tab shows a whole bunch of extra whitespace (surrounded by double quotes) immediately after the tag (which impacts the look of the site) as well as between the footer and content Divs (which again, impacts the look of the site).
Also in this view, all of the tags from the are below the first set of white-space and the tag is empty (eg: <head></head>).
The source code looks fine (and if I copy and paste the source code into a HTML file it works flawlessly) and I have to admit that this has me tearing my hair out.
Please help me Obi-Wan-Kenobi, you're my only hope (yes, I love Star Wars, although I wish they'd made more than three movies).
P.S. This might be mega-obvious, but I'm a .Net developer doing this for a family member in my spare time (also it's fun to learn new languages, even ones overly fond of the $ sign), so apologies in advance if I'm the world's biggest newb.
EDIT: What I see in the dev tools is this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
"
"
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Removed</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<link type="text/css" rel="stylesheet" href="css/jquery-ui-custom.css" />
<link type="text/css" rel="stylesheet" href="css/main.css" />
Etc.
EDIT2: Just to be clear, I don't care what the dev tools show, it's just that I'm getting whitespace showing where those quotes are on the actual website, other than that everything is perfect.
EDIT3: Also, the whitespace cannot be clicked on with the element selector and selecting the whitespace in the element tab does not highlight anything on the website. Deleting it in the element tab however DOES fix the website (until it is reloaded obviously), but obviously doesn't help me with my problem.
Try removing the ?> in your functions.php file and no blank rows after the code. And is the functions.php in the same directory as the main file?
And just a tip, include a footer.php instead of file_get_content :-)
I wasn't able to fix this in a way that satisfies me, but here's the workaround that I implemented:
I removed the include from the header entirely and broke up functions.php into one file per function and just included them where they needed to be called.
This alleviated my problem, although it's not ideal. Glad I don't have to use PHP on a daily basis :).
I had the same issue, on the console the source of the html looked like :
By checking the encoding some files were encoded with utf-8 with bom and some with big5. By saving those file to utf-8 ( without bom) solved the issue for me.
I have used Sublime Text 2 with the EncodingHelper from the package manager to see the current file encoding.
Hope it helps.

Php include annoying top margin

I don't know why every time I try to include my header using PHP’s include there's a top margin. I checked it using Firebug and it says there's a 22px offset margin on the top. Is anybody experiencing this problem? I think it's a CSS propiety: top: 22px. But nothing can change it even if I write h1 style="top: 0px; margin-top: 0px;". I think it's a php-CSS mystery that will never be solved.
edit: The only way to get rid of that top margin offset or whatever it is, is to add the follow properties to the H1: top: 0px;
position: absolute;
Will those properties generate more problems in the future?
is there a better way to solve this top margin-offset problem?
edit2: I think there's a problem with the encoding. Is there a conflict between the encoding of the included file (header.html) and the index file?
My index goes like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
<div id="page-wrap">
<?php include_once("header2.html"); ?>
</div>
</body>
</html>
With this CSS:
* {
padding: 0px;
margin: 0px;
}
My header.html (the one that’s being included):
<h1>Header 2</h1>
And that’s the output:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
<div id="page-wrap">
<h1>Header 2</h1> </div>
</body>
</html>
God its so simple that I really dont know where the top margin is coming from (in all browsers).
It only happens when I use php includes.
The source code looks the same as when I dont use php include.
This is pretty weird, but I copied & pasted your output HTML into Notepad++, and there was a strange character just prior to the h1. When pasting it into Notepad, the h in the h1 was subscripted.
Therefore, it looks to me like you may have an erroneous (or unexpected for PHP) character in your included HTML (or in the PHP including it). My suspicion? The dreaded UTF-8 BOM. Try to change the encoding of your included HTML file to eliminate the BOM (which I've always had problems with when dealing with PHP).
Edit: Yep. Just verified with a hex editor. There's a byte-order mark at the beginning of your included HTML. I don't know which editor you use, but you should have the option to change your text encoding settings.
See this for more info about PHP & the BOM.
You ought to post your generated HTML, like by copying what you see when you select "View Source" from your web browser. Oftentimes, I've seen mysterious blank lines in PHP-generated HTML because PHP is configured to output errors and warnings. Even if this is not caused by that, we can more-easily diagnose the problem if we see the outputted HTML.
I experienced the same problem...so what I did to fix it was to add an html opening comment at the beggining of the first file and an html closing comment at the beginning of the second file. This encloses the BOM (or whatever is appearing in between the files) inside a comment so it's not outputted in html.
For example:
#header.php
<?php ?>
<html>
<!--
#body.php
<?php ?>
-->
<body>
...
I tried to fix the problem converting between encoding formats in notepad++ to no avail. So this is only a temporary fix til I find a better solution to the problem.
well, without knowing what's in your php include, it's hard to tell, but make sure there's no CSS inside of it. also make sure that CSS you're loading is getting found and loaded. i debug by changing the background color or something else visual so u can be sure it's loading.
i'd bet anything that it has nothing to do with the php include it's self. like previously mentioned, there may be some css in the included file that is doing it, but i've never seen php anything like what you are describing.
i would look at the doctype declaration [temporarily remove it to see what happens], and remove the current html declaration tag and replace it with a plain <html> tag [again just temporarily, to debug]
A lot of times an extra new line will be at the end of a PHP script file you are including after the ?> tag. This will cause an extra character to be included in the generated output. You can remedy this problem by not having a closing ?> tag at the very end of your file. PHP knows to "assume" that it's closed.
Do you have the option to give us a link? I just tried locally a couple of things and you may get this "unwanted" new line for apparently "no reason" at all between your content and the "doctype" declaration, if there is a nonprintable character. Try to delete all characters after the ">" and take a look at it with a hexeditor to make certain of it. Please provide us more info.
Thats very interesting, because I just used your markup and your css. I also put it into an php file and included a html file. But there is no margin. Nowhere.
So make sure your browser is able to find the css file with
* { margin: 0; padding: 0 }
And I suggest you to use a reset sheet like this one - http://meyerweb.com/eric/tools/css/reset/index.html.
p.s. if you own some webspace, put it online and send us a link.
Dreamweaver drives me nuts with it's automatic BOM additions. Here's the fix:
http://www.adobe.com/support/documentation/en/dreamweaver/mx2004/dwusing_errata/dwusing_errata2.html
I am using notepad and I selected from the menu -> Encoding->Encoding with UTF-8 without BOM and it worked just fine. This should be done for every included file. I think this is the solution to your problem. Have a nice evening.
I almost killed my keyobard trying to solve to problem.
Then I tried PSPad editor, change explicitly .inc or .php to UTF-8 and IT WORKED !!
I had one time this problem and here is the way to fix it - its really problem in charset.I have spent 10 hours to find it. In my situation I change at all php files(all php files, all tpl(if you have) files) charset from UTF-8 to UTF-8 without bom and save it and try then. After that you can also make at .htaccess string "AddDefaultCharset UTF-8".

Categories