Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    session_start() Cannot send session cookie

    I am using cookies in PHP and my session_start() function seems to be having troubles.

    here is the errors I am gtting:

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/paper754/public_html/millionaire/add_to_cart.php:3) in /home/paper754/public_html/millionaire/functions/functions.php on line 3

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/paper754/public_html/millionaire/add_to_cart.php:3) in /home/paper754/public_html/millionaire/functions/functions.php on line 3
    I have my session_start() at the very top of the page, so I am not sure what it is refering to.

    Here is my code using $_SESSION:

    Code:
    // returns a shoppingCart() object
    // function called in add_to_cart.php
    function get_shopping_cart()
    {
    	// $_SESSION is a means for working with cookies 
    	// shopping carts hold temporary data with cookies
    	if(!isset($_SESSION['cart']))
    	{
    		// creates new shoppingCart() object from classes/shoppingCart.php
    		return new shoppingCart();
    	}
    	else 
    	{
    		// unserialize essentially "unzips" an object from memory for use
    		return unserialize($_SESSION['cart']);
    	}
    }
    
    /********************/
    
    function set_shopping_cart($cart)
    {
    	$_SESSION['cart'] = serialize($cart);
    }
    Thanks a lot for the help

  • #2
    New Coder
    Join Date
    Jul 2008
    Location
    India
    Posts
    39
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Is this the code from from first line on your page? I don't see any session_start() there. This warning is occurring because you have not used session_start on all pages. It should be written at the first, before commented lines. Hope this helps.

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks for the reply, I have placed session_start(); at the top of many pages that get included including the web page the url points to (one at a time of course), but still the same response. Is there anything else I should know about session_start() that may explain this problem?

  • #4
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Well I looked into it some more and I found suggestions:

    1. Use ob_start to cache any output before session_start function.

    2. Make sure the session.save_path in php.ini is set and point to a valid path

    We had tried the above but still having some the same problem.

    We are using notepad ++ for the coding.

    And we notice that this file is somehow being encoded to UTF-8 by one of my colleague because he though that this will help to display the result in the correct languages.

    Solution:
    Change the encode to use ANSI and make sure the EOL conversion is set to UNIX.
    Bingo. Things are back to normal as expected.
    I have tried all but the php.ini setting (not sure how to configure that)

    Also the "EOL conversion is set to UNIX" How do I go about doing that? I am using notepad++.

    Thanks a lot for the help

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That error is NOT tossed from session_start missing. Its tossed from session_start being called after output has been flushed to the browser.
    You do not need to use output buffering to use sessions, rather you just need to plan where it goes. Generally, session_start() would be among the first calls in your script if you plan to use them to prevent any problems with the call of session_start trying to create a session cookie.
    The save path is important, but I recall forgetting to set it on a windows system before and it just went to my %TEMP% location. A bummer since you need to clean these out yourself on a Windows system, so it took some time to track them down. To change, you find your install path for PHP (C:\PHP for example), and you open the php.ini file. Scroll down or search for session.save_path and change it to a new location (they have examples as well in the .ini). If PHP is running as a module in Apache, you'll need to restart your webserver for the changes to take effect.

    The error indicates a problem with output @ line #3 in add_to_cart.php. This functions.php should be included before this point if it also calls a session_start. Your character set will most likely not be a problem at this point - watch for output errors indicating line #1 for those which are mostly caused by BOM within the text itself. I don't use notepad++, I'd assume its an option under the saveas dialog. Standard notepad has an option for the 'Encoding', which should be ansi.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks a lot for the help, I looked around and read up on session.save_path. Here is the code I have put together on the issue based on what I understand about the set up.

    Code:
    $path = dirname($_SERVER['DOCUMENT_ROOT']) . '/millionaire/database/sessions/';
    ini_set('session.save_path', $path);
    include_once'functions/functions.php'; 
    session_start();
    This is at the very top of 'add_to_cart.php'

    I understand I am setting the path for my session cookies. So would I be defining the path of the cookies being stored on the users machine or the server hosting the web site? I think its the first, not sure how to go about defining paths for unknown users configurations.

    Here is the errors I am getting as of now:

    Code:
    Warning: session_start() [function.session-start]: open(/home/paper754/millionaire/database/sessions//sess_39d3e7af2de8b6f06f7787d81c239bb9, O_RDWR) failed: No such file or directory (2) in /home/paper754/public_html/millionaire/add_to_cart.php on line 7
    
    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/paper754/public_html/millionaire/add_to_cart.php:3) in /home/paper754/public_html/millionaire/add_to_cart.php on line 7
    
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/paper754/public_html/millionaire/add_to_cart.php:3) in /home/paper754/public_html/millionaire/add_to_cart.php on line 7
    I appreciate the help

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You cannot change the path that the user stores their cookies. The only control we have is to say where it applies; .domain for the whole domain for example. These are irrelevant with session cookies though as they should be pumping a domain wide cookie. So to answer your question, the use of session.save_path changes the location of where the session information is stored as a file on the server, not the client.

    The second and third errors are caused by the first error. The first error indicates that it cannot open that path to read and write its cookies. Because of this error, it has output a message to the user which qualifies as a flush of the headers. It then causes the session_start to fail since it cannot even make the attempt to set the cookie. The line number's don't quite jive, but I'd expect that is the cause.

    You need to ensure that this path exists: /home/paper754/millionaire/database/sessions/ and is either owned or grouped to the apache user (which unfortunately changes on distros, though nobody does tend to be quite common) with read and write privileges or given privilege to all users. 666 should be sufficient for this directory. Either this, or don't change the path at all; the default is likely /tmp.
    I don't think that the extra / will cause problems, but you can try to remove it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks a lot for the feed back, so it seems the file path is problem now. I have tried multiple different file paths and none seem to work.

    Here is my file path directory on my server :

    paperlesswasp.com/millionaire/
    - add_to_cart.php (has session_start() and session.save_path)

    /database/sessions/
    Here are the file paths I have tried but to no avail

    millionaire/database/sessions
    /millionaire/database/sessions
    millionaire/database/sessions/
    /millionaire/database/sessions/
    paperlesswasp.com/millionaire/database/sessions
    http://paperlesswasp.com/millionaire/database/sessions
    /database/sessions
    /database/sessions/
    database/sessions
    database/sessions/
    I wasnt really sure if the session function looks for file paths in the same manner as php (not needing the full URL) or more like HTML (needing the full URL) so I tried both

    After trying the urls I noticed that the error showed the ouput which automatically included a '/' after my sessions folder. so to add a '/' at the end of sessions caused the file path to be '//', so obviously leave out the last / in the file path is one key thing.

  • #9
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,285
    Thanks
    4
    Thanked 201 Times in 198 Posts
    Is there a particular reason you are trying to set your own path for sessions? The system default is usually just fine and does not need to be altered.
    Dave .... HostMonster for all of your hosting needs

  • #10
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    I am just trying to figure my problem with session_start(). I am getting errors

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/paper754/public_html/millionaire/add_to_cart.php:3) in /home/paper754/public_html/millionaire/add_to_cart.php on line 7
    Its saying that the output started at <?php

    I cant obviously put session_start() before <?php and I understand that I am suppose to place session_start() as one of the first things in the code due to output issues like this one... Its asking me to place it before some syntax it has to come after.

    People say it may be a path issue, so I am trying to calibrate that. my hoster mentioned that I needed to set the session.save_path for php ini for windows servers... I am using their servers so I dont see why I would need to set this for them.

    This has just been a rather illogical process so far trying to get session_start() working.

    I appreciate any suggestions you have on the matter.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If your <?php doesn't start until line 3, then yeah there is a problem there. That means that you have two previous lines of output before this point, which is not doable when you intend to send a header. Whitespace and html both qualify as output.
    The path you have posted that shows the /database/sessions in comparison to the file directory isn't a whole lot of help. It doesn't jive with your error code paths, so the only thing I can do is a relative one which will only work from that script and scripts within the same directory:
    PHP Code:
    $path dirname(__FILE__) . '/../../database/sessions';
    ini_set('session.save_path'$path); 
    Using the document root will work as well. From the error code, it indicates that your public_html is not included as a part of the directory root.
    PHP Code:
    $path $_SERVER['DOCUMENT_ROOT'] . '/public_html/millionaire/database/sessions';
    ini_set('session.save_path'$path); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    surreal5335 (12-13-2010)

  • #12
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks a lot for that tip.... Never thought about that being a problem. I made the changes (got rid of the white space before the <?php) and pow, back in business.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •