问题
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.
回答1:
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
回答2:
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).
来源:https://stackoverflow.com/questions/10056233/jquery-load-and-php-session-start