Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP FAQs
|
PHP FAQs This moderated forum is where our PHP experts answer some of the questions they see asked most frequently in the other PHP forums. You cannot post other messages here, use the other PHP forums for that.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP FAQs section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 4th, 2004, 07:24 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default Q. My sessions are failing... what's wrong??

[u]Symptom:</u>
Warning: session_start(): open(/tmp\sess_9d0fb76e93c38f1824377131f3c2e154, O_RDWR) failed: No such file or directory (2) in {{{ file path }}} on line X

[u]Remedy:</u>
You do not have a sessiondata directory configured to store session data. This is because the default values of the session.save_path directory is configured for Linux operating systems. Windows PHP installs must specify the value of the session.save_path directive explicitly.
  •  
  • Open php.ini located at C:\Windows, or wherever Windows is installed.
     
  • Change the value of the session.save_path directive to reflect a valid file path, I recommend C:\PHP\sessiondata, or whereever PHP is installed.
     
  • Create the sessiondata directory, if it doesn't exist.
     
  • Make sure that the proper write permissions are applied to the directory.
     
  • Restart your HTTP server if necessary.
[u]Symptom:</u>
Warning: session_start(): Cannot send session cookie - headers already sent by (output started) in {{{file path}}} on line X

[u]Remedy:</u>
When session_start() is called it makes changes to the outgoing HTTP headers. As it does such, no output can come from the PHP script before it is called, e.g. any markup, whitespace or other kind of output appearing either before or after the opening <?php delimiter. Remove the output before session_start() and this error goes away. If this error is second, third, etc in a list of session_start() errors, its likely the error was triggered as a result of the errors before it.

[u]Symptom:</u>
Session variable doesn't persist between page requests.

[u]Remedy:</u>
Make sure that you are passing the session id between page requests. Since a session is intended to tie one user to data that persists on the server, PHP must have some way of associating the user with the session data. This is done with the session id. The session id is passed using cookies by default. When session_start() is called, it outputs a cookie to the client. The client's browser saves that cookie, then ever time the user requests a page the process starts all over again.. the browser sends the cookie that it stored from the first request back to the server. Then session_start() is called, session_start() looks to see if a session id has been passed to it, e.g. it looks for that cookie. If the cookie is found it says hey, I remember you, and it populates the $_SESSION superglobal with the session data from the previous request. If you are using cookies to pass the session id, you must therefore have first party or session cookies enabled in your browser.

A second method of passing the session id, not relying on cookies, is by embedding the session id directly in every URL or form used in the page, for example.
Code:
<?php
    session_start();


    $_SESSION['foo'] = 'Hello, world!';

    echo "<a href='other_page.php?sid=".session_id()."'>Next page...</a>";
?>
Here, I've called on session_id() to output the current session id directly in the URL, which frees the user from needing cookies to be enabled in their browser. (some modern browsers disable cookies by default, BTW)

Then page 2 can be called to output the value of $_SESSION['foo']
Code:
<?php
    //other_page.php 
    session_start();

    echo $_SESSION['foo'];
?>
You see that when this test script is run through PHP's sausage maker, that $_SESSION['foo'] still exists!

For further reference:
http://www.php.net/session





Similar Threads
Thread Thread Starter Forum Replies Last Post
Failing DAO Tests dgalehouse BOOK: Beginning Spring Framework 2 ISBN: 978-0-470-10161-2 13 September 28th, 2009 07:13 PM
DlookUp Macro failing AidanEnos Access 1 November 23rd, 2007 12:51 AM
newbie failing with the first exercise victorjitlin BOOK: Beginning JavaServer Pages 3 June 3rd, 2006 03:08 AM
Mixing classic ASP sessions with ASP.NET sessions scorpion_king General .NET 2 August 4th, 2004 08:20 AM
Update Query failing patwadd Classic ASP Databases 1 July 7th, 2003 10:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.