Warning: session_start(): Cannot send session cache limiter - headers already sent - php

I am getting an error which is very common.
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sabarspinmatic/public_html/testing/index.php:1) in /home/sabarspinmatic/public_html/testing/index.php on line 1
But the strange this is I have only one php file with only 1 line of session_start().
PHP Code is:
<? session_start() ?>
Could anyone know why I am getting this error. I checked the source code and its showing <br />. I don't know why its showing this tag.
URL to check the error is: http://www.sabarspinmatic.com/testing/
//PART 2
I am just using single file index.php without use of .htaccess and only l line of code. I tried by putting php at start of tag but it doesn't work.

First, I would suggest you to use proper starting tag <?php, just in sake of code readability and portability - not all web servers are configured to support the shorter version <?.
Then make sure there is not even a whitespace before the <?php session_start() .... Also some editors (like Windows Notepad) tend to insert some invisible garbage at the very beginning of the file - so I suggest you to open the file in a HEXADECIMAL mode to see if there are really no characters before the <?php .... There could be for example the invisible UTF-8 BOM character.
Last, but not least, use a semicolon ; at the end of the command - no matter it is the only command in the code block, it is simply a best practice.

There me 2 reasons for the ocurrance of the above issue.
1) May in the including files session_start() is already written.
2) May be u are using the session_start() tag in middle of the code.It should be written in the first line of page.

Firstly short tags have been deprecated by php
use
<?php ?>
everywhere instead of
<? ?>
Also is this your entire page? Do you have a space or a line before very first text in the file

From my experience,removing all spaces in your code solves the problem.Especially if you have a white space before
This will cause the error:
..whitespace..<?php
Removing the whitespace solves the problem

I had the same problem... It was because of the Byte Order Mark (BOM) EFBBBF at the start of the file. Use any hex editor to remove these chars from the file and it should work.

Related

To use ?> or php?>

the tech guy at one of my clients raised to me I wasn't using <?php everywhere and would need to change that because of security reasons. Fair point, will do my good sir! However, he also asked if I could change ?> to php?> as the closing tags and that seemed a bit odd to me. I have never seen that, nor any mention of it anywhere (Google gave me a total of 0 relevant results). And even my code editor implies that that is faulty code.
Can anyone enlighten me if it even exists and if so what benefits it offers above ?>
Thanks in advance.
Given:
<?php
error_reporting(E_ALL);
echo "echo ";
php?>
The output is:
echo
Notice: Use of undefined constant php - assumed 'php' in /tmp/test.php on line 4
In php?>, the php part is not a component of the "end of PHP code" marker. It is just a constant that you haven't defined just like in junk?>.
The PHP tags docs clearly says that <?php is an opening tag and ?> is closing tag.
You can use short opening tag <? but you need to enable short_open_tag in your php.ini file first.
However in some cases it's good to omit the closing tag.
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

Wordpress – HTML document does not start at line one

I've been struggling with this problem for a long time now, but I cannot really find the solution. The problem is that < !DOCTYPE html etc... does not start at the first line, but leaves four blank lines before it starts.
All my files (header.php, index.php etc) have no line breaks before they start.
Anyone with any similar problems/experiences out there? It would have been of huge help!
See here for reference: view-source:http://2famous.tv/
Thank you
This is most often not caused by leading but by trailing whitespace. Lots of old PHP code still closes down code at the end, which then all too often has a stray newline:
<?php
// Lot of source code
?> <----- and a newline here which is the culprit!
To avoid this issue, never close files with ?> - PHP doesn't need it and will just stop parsing at EOF, thus implicitly avoid this 'garbage' in the output.
As for finding the files causing it - good luck, I'd start with combing any custom extensions for this and just removing all ?> markers that you can find.
As an alternative, you can probably 'fix' it by adding a single ob_start() call to your index.php, and then in the template containing the doctype executing ob_end_clean() - this puts all intermediate output in the output buffers, and then trashes it.

Laravel displays an empty line before the Doctype

<!DOCTYPE html>
This is the code.
How can I fix that?
I tested the HTML/CSS/JavaScript before integrating the code with Laravel.
Make sure your PHP files don't have the closing tags (?>). They might add whitespace to your HTML.
For more info, see the PHP docs:
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
You might also want to take a look at this post: Why would one omit the close tag?
I know this is few years late but for other people.
change the page encoding to UTF-8 without BOM and it will be solved.

Problem with whitespace before doctype

I got some strange problem where i have 2 copies of same website in 2 different folders. In one of those 2 copies has whitespace before doctype declaration so i have problems to work with facebook as document is formated incorrectly.
Before html there is some php calculations, but there is no echo statements or something.
What could be cause for 2 identique websites under same server in different folders, one having this issue?
I'm almost positively certain that you have some trailing whitespace at the end of a PHP include. Check there first.
Example:
<?php
// header file
include 'other_file.php';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- etc. -->
Then
<?php
// other_file.php
// connects to database, or something,
// but notice the whitespace after the closing tag
?>
... pretend this text isn't here
I added the note at the end to get stackoverflow's markdown editor to show the additional lines after the closing ?> tag. PHP automatically truncates one newline character after the tag in includes, but if you have more than one they will be interpreted as blank space before your doctype declaration.
Easiest fix? Remove the closing tag ?> from all of your includes. This is a valid technique, and could be the fastest way for you to get up and running.
You don't say which browser you're having problems with. IE used to check the first few letters of the page for a doctype and, if it isn't one (such as whitespace), it would go into quirks mode.
This is especially common if you are using includes. Check over all your includes or other php files carefully that come before your output or headers.
<?
echo "php here";
?>
This will cause a problem if it's in an includes
that comes before your headers or doctypes.
Remove any non php here, file should end with "?>"
not whitespace, linefeeds or characters.
As for why? Unknown, on one production server this issue raises it's head constantly for me (CentOs 5) but on my dev machine (latest Fedora) it doesn't happen and I have no issues.
Honestly there's probably something I could track down to find out why, but since proper usage says "no extra spaces or lines" I just do that and don't worry too much about the "why is it handled differently on my servers" all that much. (Kind of a cop-out I know)
I ran into this where a line break was above the Doctype. I tried a few things and then ended up just placing the doctype in the php.
<?php
echo '<!DOCTYPE html>';
... other php code/includes/etc ...
?>
<html>
... other html elements ...
Not sure if that's the right way to go about it, but it worked for me. If you're using a more extensive doctype, you'll need to escape special characters.
I just spent a day solving this problem where my libraries worked fine on static pages but under a dynamic system with a master script at the start (apache mod_write rewriting all to master script) the browser flipped into backward compat mode (js: alert(document.compatMode()) ) and distorted my styling. All my include files were ended properly and no echos, etc.. The problem? A blank line above the opening <?php of the master script.......
You may not believe but I have a MAGIC way to solve this problem:
Upload your documents (which are included before <!DOCTYPE) on your host using File Manager of CPanel, then choose the document and Click "Edit" to see the source code of the document. Then press Ctrl+Home then press Delete!
You see that nothing will be deleted but that hidden white space!!
Save your document and you will notice that everything is OK now.
I do this for all of my projects
In my case this works fine:
$ brew update
$ brew install coreutils
$ cd directoryWithSymfony2Project
$ for file in $(find ./ -name '*.twig' -or -name '*.php'); do sed `echo -e 's/[\xC2\xA0]//g'` $file > temp; echo 'Processing ' $file;rm $file; mv temp $file; done
I had the same issue, and the culprit was a whitespace at the top of one of my included PHP files, before <?php:
<?php //Line above this line was the problem
function gggl_vertical_cards(){
ob_start();
?>
//...
do you use session_start() or header() anywhere? then (without outbutbuffering) no whitespace or other character is allowed to send to the client before this functions.

What is this PHP warning, "Cannot modify header information"?

I'm a newbie for PHP5 and In my php page I'm getting this error when try to redirect to another page
Warning: Cannot modify header information - headers already sent by (output started at <path to my php file>:<line number>) in <path to my php file> on line <line number>
in my php file I have several includes and each one doesnt have any space before of after tag
This is because you must first set your headers and then add any output.
If you did not echo/print anything make sure you did not have warnings or notices (the count as output too if you have error reporting on).
As a good practice, if you can, put the header calls at the top of your script.
You could also take a look into output buffering if you need to generate output before headers.
Maybe your php file has a unicode signature (BOM) which adds a signature at the beginning of your file.
open your php file with a plain text editor like notepad and see if there is something at the beginning of your file. if so, remove them.
The problem could be an opening <? tag with some spaces just before the "<" like shown here "_<". These spaces count as output and can prevent headers from being set.

Categories