开发者

PHP session_start() fails to create session

开发者 https://www.devze.com 2023-01-21 12:56 出处:网络
These are my first 5 lines in my index.php: <?php session_start(); if (!isset($_SESSION[\'cart\'])) $_SESSION[\'cart\']=\'\';

These are my first 5 lines in my index.php:

  <?php
session_start();
if (!isset($_SESSION['cart'])) $_SESSION['cart']='';
$page = $_GET['page'];
?>

and so on.. I'm looking at the sessions trough the firefox->firebug->firecookie plugin and a session i开发者_如何学运维s not created.(I am sure of it because the cart that worked yesterday doesnt work today.) Any ideas why this might happen and how to fix it ?

I found this when enabling errors: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/controll/public_html/metalkonsult.com/index.php:1) in /home/controll/public_html/metalkonsult.com/index.php on line 2

I explored further - something IS sent to the browser but I dont know where its coming from. I did a var_dump(headers_list()); on the first line and this is what I get:

array(2) { [0]=> string(23) "X-Powered-By: PHP/5.2.6" [1]=> string(23) "Content-type: text/html" }

How do I turn this off ? What's sending it ?

I set session.auto_start = 1 in php.ini in the website folder sessions work now.. dont know what caused the problems but problem is temporarily fixed


If the white space I see before your php opening tag (<?php) is in your source code too, then this is the reason :) Make sure that no output has been done before your session_start call.

You can check with firebug the exact output, by going to the net tab, locating your PHP file transfer, expanding it, and seeing the Response tab. You could put a print just after the session_start and see where it appears... it should be the very first thing.

Otherwise, if you have php errors directed to log, go and see the log if there is anything in it. Finally, make sure that the browser is displaying session cookies too: some browsers don't show them.


Something is being sent to the browser first which is causing the headers to be sent. Check your code to make sure that there isn't even a single space before your PHP code.


Some editors add an invisible UTF-8 byte order mark at the beginning of the file. Depending on the various server software versions, it may or may not be sent to the browser. Could you check a hex dump of your source code and make sure the first 5 bytes are 3c 3f 70 68 70?


Works for me. Make sure your server has write access to the folder where session data is kept (check your session path: http://php.net/manual/en/function.session-save-path.php)

      <?php
    session_start();
    $_SESSION['cart'] = "test<br />";
    print $_SESSION['cart'];
    if (!isset($_SESSION['cart'])) $_SESSION['cart']='';
    print $_SESSION['cart'];
//blank it out
    if (isset($_SESSION['cart'])) $_SESSION['cart']='';
    print $_SESSION['cart'];
    ?>

outputs

test
test

@Palantir: And the whitespace doesn't matter.


Is that the first PHP page called or is it included from another one?

If it is the first, try enabling output_buffering on php.ini. You may then be able to see what is being sent before by calling ob_get_contents()


I had this problem caused by two different things:

1- there were an empty line at the beginning of the file case the php 5.2 to push 'headers already sent' warning.

http://board.phpbuilder.com/showthread.php?10310794-RESOLVED-Warning-session_start()-Cannot-send-session-cookie-headers-already-sent

if < ?php is at line 2 of your script and a blank line is above it then that can cause problems such as the php header function not working.

2- PHP output_buffering was turned off by default. If you're using shared hosting and code works on localhost maybe this can be the reason. just set these parameter in php.ini or local .user.ini

output_buffering=4096

0

精彩评论

暂无评论...
验证码 换一张
取 消