[Frustrating Issue] with session_set_save_handler()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

[Frustrating Issue] with session_set_save_handler()

Post by drayfuss »

I'm having a nightmare with a php script that makes use of the session_set_save_handler() function to send session information to my mysql database.

I borrowed a script from a tutorial by tuxradar.com entitled 'Files vs. Database'. Although I modified it slightly, it's worth noting that even an identical script to the tutorial brings about the same issue.

The issue is that I get two warning messages from php on load of the page in the browser:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/otis/session.php:1) in /var/www/otis/session.php on line 47

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/otis/session.php:1) in /var/www/otis/session.php on line 47

The PHP code is as follows:

Code: Select all

<?php
    mysql_connect("localhost", "user", "mysqlpass");
    mysql_select_db("test");

    function sess_open($sess_path, $sess_name) {
        return true;
    }

    function sess_close() {
        return true;
    }

    function sess_read($sess_id) {
        $result = mysql_query("SELECT Data FROM sessions WHERE SessionID = '".$sess_id."';");
        if (!mysql_num_rows($result)) {
            $CurrentTime = time();
            mysql_query("INSERT INTO sessions (SessionID, DateTouched) VALUES (".$sess_id.", ".$CurrentTime.");");
            return '';
        } else {
            extract(mysql_fetch_array($result), EXTR_PREFIX_ALL, 'sess');
            mysql_query("UPDATE sessions SET DateTouched = ".$CurrentTime." WHERE SessionID = ".$sess_id.";");
            return $sess_Data;
        }
    }

    function sess_write($sess_id, $data) {
        $CurrentTime = time();
        mysql_query("UPDATE sessions SET Data = ".$data.", DateTouched = $CurrentTime WHERE SessionID = ".$sess_id.";");
        return true;
    }

    function sess_destroy($sess_id) {
        mysql_query("DELETE FROM sessions WHERE SessionID = ".$sess_id.";");
        return true;
    }

    function sess_gc($sess_maxlifetime) {
        $CurrentTime = time();
        mysql_query("DELETE FROM sessions WHERE DateTouched + ".$sess_maxlifetime." < ".$CurrentTime.";");
        return true;
    }

    session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
    session_start();

    $_SESSION['foo'] = "bar";
    $_SESSION['baz'] = "wombat";

?> 
The results in the resulting MySql table are mixed. The session, for the most part, is created in the table, with the session id string and the time.

However, the warning messages persist, and the $_SESSION[] variables do not get passed to the Data field in the mysql table either. The mysql table was created with:

CREATE TABLE sessions (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, SessionID CHAR(26), Data TEXT DEFAULT '', DateTouched INT);

Any thoughts? I've looked up and down the internet for an answer to this issue but nobody else seems to be mentioning it...

Thanks for any help,

drayfuss
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [Frustrating Issue] with session_set_save_handler()

Post by requinix »

Make sure your editor is saving the files in ANSI/ASCII format. PHP doesn't properly handle UTF-8 files yet so the byte-order mark at the beginning of the file gets outputted - breaking any future usage of header() or session_start().
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

Re: [Frustrating Issue] with session_set_save_handler()

Post by drayfuss »

Oh cripes,

thanks very much for the post.

Do you mean my text editor or the mysql table? I use Aptana to edit (bit of a cpu hog) but I copied the php code to a txt file and made sure I saved it as ASCI. With the same problem.

Perhaps I'm not following your point? Dare I say, I'm just beginning to get my head round this stuff. I feel like the penny is just about to drop but I need a little push. Been programming actionscript for years, but only just moved to the world of linux/mysql/php/apache2 which I appreciate is a much bigger beast.

drayfuss
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Frustrating Issue] with session_set_save_handler()

Post by Eran »

There's probably some output before sessions are started. You need to remove all white-space (including line-breaks) before you start the session. Probably there's some before the first PHP tag.
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

Re: [Frustrating Issue] with session_set_save_handler()

Post by drayfuss »

Solved! The strangest thing is I did see a few comments about whitespace, but I didn't even realise there was a space before the <?php.

Thanks so much.

As much as I extremely grateful, I was hoping a lamans terms explanation of why a whitespace was such a problem specifically for sessions?

Not to worry if you've not got the time, the fix was much appreciated.

drayfuss
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Frustrating Issue] with session_set_save_handler()

Post by Eran »

Once output is sent to the browser, response headers are sent with it. Session data is sent with the headers, so if you start the session after any output has been sent to the browser it would be too late to add session data to the headers (those having been sent already).
drayfuss
Forum Newbie
Posts: 24
Joined: Sun Apr 25, 2010 4:57 pm

Re: [Frustrating Issue] with session_set_save_handler()

Post by drayfuss »

Thanks for your help.

drayfuss
Post Reply