jQuery .load and PHP session start - php

I am currently working on a game in PHP and jQuery and at some point I will need to use the .load() from jQuery to load a PHP page into a div. That page will load some player information based on their login information, account id, etcetera, stored into an array inside $_SESSION["arrayname"].
It works perfectly on all sites, except those where jQuery load() is use. On easyphp, I got no errors, but on my web host server im getting this:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /fr/game/map.php:1) in fr/game/map.php on line 6
Here is my code:
<?php
require_once("../../connectionRO.inc.php");
//used to check if we see the array in session, and i dont when i load it with jquery
if(!ISSET($_SESSION["comptejeu"]))
{
require_once('../../classInfoCompte.php');
session_start();
require_once('../../lg.php');
require_once("./glg.php");
}
$ic = new infoCompte;
$ic = $_SESSION["comptejeu"];
?>
I understand that at this point a lot of stuff is already output to the page before that is loaded. Could anyone point me to a better way to retrieve the information that I need from that array to build my object from my class?
Thank you.

Since you are getting this error as output started at /fr/game/map.php:1 (note: line 1) I will place money on you having whitespace or a BOM before the opening <?php tag in /fr/game/map.php.
Make sure the opening < is the first character in the file. If you file is UTF-8, make sure it is UTF-8 without BOM, or convert it to ASCII.
Wrong:
<?php
Right:
<?php

Either there is some white space in your PHP file, there is some whitespace in classInfoCompute.php or there is a function/method call that triggers output in either this or the classInfoCompute file. Sessions depend on cookies, and cookies depend on HTTP headers. Once content has been output, the headers are already sent and you can't send a new one.
There are plenty of questions and answers on Stack Overflow that cover this issue, but it boils down to either sweeping the problem under the carpet (with output buffering), or fixing it properly (by finding what's causing content to be output before your call to session_start and either removing it or moving it to later in the script, or moving session_start to earlier in the script).
If your script mixes HTML and PHP then make sure that the session_start occurs before the first bit of HTML. Otherwise, you'll have to hunt around for white space. If the file is saved as UTF-8 then your text editor might have attached an unnecessary Byte Order Mark (BOM) to the start of the file. Make sure that your file is being saved without a BOM, because it will be treated as content and trigger output to the client (and put a weird character at the start of the output).

Related

"session_start(): Cannot send session cache limiter" warning, using ajax

I am having this error in PHP:
session_start(): Cannot send session cache limiter - headers already
sent
I know I have to put session_start() even before the html <head>, what I am doing. The issue here is that I am using id containers for ajax generated content.
For example, I have a button in the homepage.php which updates some calculate.php file in a divResults, and this calculate.php begins with session_start(). However, this warning is appearing inside divResults.
How could I solve it. Is it possible to simply ignore the warning. How?
(I don't have this problem in XAMPP, only using an external hosting provider)
Thanks
Based on your problem following errors may possible.
you write session_start(); in somewhere middle or end of the .php file so you need to put session_start();at top of .php file just after <?php.
May be some empty lines are there in your .php file at the beginning or end, because of that also this error will arise. remove those empty line.

PHP session error in some pages [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I have written <?php session_start(); ?> above everything in all pages. Some pages are rendering fine but I am getting this error in other pages. I have checked and matched each page code and code is fine. If I remove <?php session_start(); ?> then page renders fine but I need to use session.
Cannot send session cache limiter - headers already sent (output started at /home/) in ...
One thing to note is: It runs fine on my local server.
I would bet it's one of two things:
Make sure there is no output, including newlines, in your file before you call session_start. PHP must send HTTP headers first (before the message body, i.e. page content), so trying to send a header after you've already sent page content will give you that error.
You should only call session_start only once per page. If you're using include in a file after you've called session_start, make sure you're not calling session_start again in the included file. This would cause the "headers already sent" error.
you have some output (maybe an whitespace) in one of your included files.
maybe your <?php is not the very first of your file somewhere.
You could also try to use output buffering (http://php.net/manual/es/function.ob-start.php). I'd first check to see whether you are not sending any output by mistake (as #steven suggests), but still, it might be a good idea for you to buffer your outputs.
Since you are including multiple files, and you seem to have a session_start() in multiple files, I bet the error is thrown in the second file.
Check all files for the session_start(), and for whitespace before any of these are called.
This is a very typical BOM header problem. I suspect that your editor stored a UTF-8 BOM header with 3 bytes  at the begin of the file. The editor will hide them, so if you are not sure if your file contains these characters, you can either use a non interpreting editor (hex editor), or this wonderful online W3C checker.

session_start() throwing error

I am currently getting the following error using php:
Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /home/paramireze/madisonh3.com/calendar.php:1)
in /home/paramireze/madisonh3.com/includes/common.php on line 5
The first line of every file is include common.php, and the first line of code in common.php is 'if(!isset($_SESSION)) {session_start();}`.
This error only occurs on calendar.php and news.php (you can see the error if you visit http://www.madisonh3.com/calendar.php). All my files are the same, which includes a common.php. After that, I will write the html tag and include the header from there.
I've read other discussions regarding session_start and all say to make sure you do not output any html before session_start. Also, if I am doing something wrong, why is it only happening to two out of my 10 files?
There is something outputting data BEFORE your session_start() command. As the session cookie is set to the HTTP header it must precede any HTML output.
The error Cannot send session cookie - headers already sent by (output started at /home/paramireze/madisonh3.com/calendar.php:1) in /home/paramireze/madisonh3.com/includes/common.php on line 5 indicates something is outputted before.
So look into your code and find what could be echoing data before your session_start().
You should care that your editor does not store the utf-8 BOM header, this header is sometimes stored at the begin of the file with 3 bytes .
The editor will hide them, so if you are not sure if your file contains these characters, you can either use a non interpreting editor (hex editor), or this wonderful online W3C checker. The BOM header is treated as output by PHP, and this can cause nasty Cannot modify header information - headers already sent errors.
Checking your URL shows, that there is indeed such a BOM header. Have a look at the settings of your editor (IDE).

How do I carry over a php session from one page to another via Javascript?

I have a form (form.php) that uses the following JS code in the header:
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$.post('test.php', function(data) {
$('#test').html(data);
});
});
});
</script>
A have a link in form.php that once clicked I want a drop down to appear (without refreshing the page) e.g. "Display drop down". The code in the drop down is managed in test.php.
Test.php pulls data in from another service. In order to do get this data I use data that is kept in the session e.g. $_SESSION['data_that_is_sent_to_another_service'].
I start the session in form.php but in order for the test.php to get the information from the other service I need to start a session at the start of test.php.
The code works, but I then get a warning saying:
Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /..my directory.../test.php:1) in /...my directory.../hrdeals.php on line 4
How can I get this warning not to appear (without turning off warnings in PHP)?
Any help much appreciated.
Gregor
PS - the JS might not be correct, but it's more the SESSION issue that I can solve
session_start() must be called before outputing anything to the browser. Check test.php and make sure there is no output.
Javascript and PHP Sessions have nothing to do with one another. Javascript lives in its little bubble (client side rendering/processing). PHP lives in its bubble (server side processing/output).
The error you get is (briefly) - headers already sent.
This refers to http headers (not the head tag where you may have some javascript). You can see the whole list here: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
This error occurs when you start rendering web page stuff before outputting a header. All headers must be completed first.
Check that all echos and prints from php are occurring after session_start() occurs. One tricky place that printing creeps in is when you end php tags and have anything there - even when it's at the end of a page. You don't need ending php tags at the end of your pages and even a space following one will cause this kind of problem. The safest thing is to just remove all ending php tags at the bottom of your php files.
Another tricky thing is that this kind of error is also caused by a "Byte Order Mark" being output by your code editor. You can try opening, fixing and saving the page in just a simple plain-text editor and see if that fixes the problem.

Can not output image into src tags

I have the following:
$imageurl = "<img class='item_thumb'
src='getimagethumbnail.php?imagename=".urlencode($product_image)."&from=".$prodimagedir."'
min-width='150' min-height='150' border='0' class='item_thumb'>";
Which creates the following html:
<img class="item_thumb" border="0" min-height="150" min-width="150"
src="getimagethumbnail.php?imagename=productsmall_1248886833bloggingbok.jpg&
from=products/"/>
However, the image does not show up. I point my browser to that src link and it gives me a bunch of unreadable text which I am assuming is the image meaning that the script getimagethumbnail is working fine. (I am guessing).
But as I said, the image is not appearing at all. What is wrong and what steps can I take to determine the problem?
Just to add, when I point my browser to that src link: It also gives me:
Warning: Cannot modify header information - headers already sent by
(output started at /home/dji/public_html/getimagethumbnail.php:35) in
/home/dji/public_html/includes/functions.php on line 4953
I am assume this is because of the output?? This script was working fine and I have made no changes to it as far as I am aware!
Thanks
You are trying to send the header('Content-Type') command, after outputting whitespace/characters.
You need to make sure that the header command is before anything is printed on the page.
This will work:
header('Content-Type: ....');
readfile('file.png');
This won't
readfile('file.png');
header('Content-Type: ....');
This is because the header command tells the browser what to look for in the content. All of the headers must be sent before any content because that is how the connections works. The browser can't be told what to expect after the content has already been sent.
Open Connection With Server -> Get Headers -> Get Content -> Close Connection
One of the big reasons behind this is encoding. As the content comes through, the browser has to decode it properly. If you send a header in the middle of the page telling the browser that the encoding type is a, when it was processing it like b, things can get really confusing.
So, in order to send the headers properly, you must put the header command before any output.
That error is caused when you print stuff to the output and then attempt to use the header() method. You shouldn't be outputting anything until after you do what you need with header(). Nothing should precede this, not even white-space.
You already have produced some output (on line 35) before setting the header for the image type. This might simply be a whitespace between php tags, or something you forgot to remove.
Your getimagethumbnail.php script is not generating a valid image; it's including text in it (the warning message you quote), which prevents browsers from rendering it. Judging by the error text, I'd guess this is due to changes made either to getimagethumbnail.php or functions.php.
Fundamentally, the problem is that functions.php is attempting to call header() after output has already been sent to the browser, which just plain won't work. You need to check both files and make sure that any calls to header() come before anything else that sends data to the browser.
You may want to turn off the display_errors setting, as any code which generates any warning or error for any reason will cause the problem you're seeing if the warning/error occurs before your header() calls. (Just make sure you have error logging on, so you can still see what's going wrong!)

Categories