Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Generating "Ghost" sessions in the sessions table

Tags:

php

mysql

  • Windows Version: Win 8 64bit
  • WAMP Version: 2.5 64Bit
  • PHP Version: 5.5.12

PHP is generating what I'd have to describe as "ghost" sessions in the sessions table, for every page request. The weirdest part is that just clicking on the browser window seems to be enough to create a new session. It happens in Firefox and Chrome (never installed any extensions/addons in Chrome) but doesn't happen in IE11.

At this point I've no idea if it's a bug/glitch in the way that Firefox and Chrome handle cookies or it it's a bug with PHP. The code I'm testing with is below (including the SHOW CREATE TABLE for the test table).

<?php
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_RECOVERABLE_ERROR);

$db_type = 'mysql';
$db_host = 'local_host';

$dsn = 'mysql:dbname=test;host=localhost';
$user = 'sess_test_user';
$password = 'password';

try {
    $db = new PDO($dsn, $user, $password);
// Create a new instance of PDO (connect to the database)
}
catch(Exception $e) {
    die('Failed To Connect To Database');
}

class db_session_handler implements SessionHandlerInterface {

    public function __construct($db) {
        $this->db = $db;

    }

    public function open($save_path,$session_name) {
        $this->db;
        return true;
    }

    public function close() {
        unset($this->db);
        return true;
    }

    public function read($session_id) {

        try {
            $sql="
                SELECT
                    sess_data
                FROM
                    session_table
                WHERE
                    sess_id = :sess_id
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':sess_id', $session_id);
            $stmt->execute();
            $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
            if (count($res) <> 1 ) {
                return false;
            } else {
                return $res[0]['sess_data'];
            }
        }
        catch (PDOException $e) {
            return false;
        }
    }

    public function write($session_id,$session_data) {      
        try {
            $sql="
                SELECT
                    sess_data
                FROM
                    session_table
                WHERE
                    sess_id = :sess_id
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':sess_id', $session_id);
            $stmt->execute();
            $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
        catch (PDOException $e) {
            return false;
        }

        try {
            if (count($res) === 0) {
                $sql="
                INSERT INTO
                    session_table
                (
                      sess_id
                    , user
                    , start
                    , last_activity
                    , expires
                    , sess_data
                )
                VALUES
                    (
                          :sess_id
                        , '0'
                        , NOW()
                        , NOW()
                        , DATE_ADD(NOW(), INTERVAL 30 MINUTE)
                        , :sess_data                        
                    )               
                ";
                $stmt->bindParam(':sess_id', $session_id);
                $stmt->bindParam(':sess_data', $session_data);
            } else {
                $sql="
                    UPDATE
                        session_table
                    SET
                          last_activity = NOW()
                        , expires = DATE_ADD(NOW(), INTERVAL 30 MINUTE)
                        , sess_data = :sess_data
                    WHERE
                        sess_id = :sess_id              
                ";
            }
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':sess_id', $session_id);
            $stmt->bindParam(':sess_data', $session_data);
            $stmt->execute();
            return true;
        }
        catch (PDOException $e) {
            return false;
        }
    }

    public function destroy($session_id) {
        try {
            $sql="
                DELETE FROM
                    session_table
                WHERE
                    sess_id = :sess_id
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':sess_id', $session_id);
            $stmt->execute();
            return true;
        }
        catch (PDOException $e) {
            return false;
        }
    }

    public function gc($max_lifetime) {
        try {
            $sql="
                DELETE FROM
                    session_table
                WHERE
                    last_activity < expires
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
        }
        catch (PDOException $e) {
            return false;
        }
    }

    public function __destruct() {
        session_write_close();
    }
}

$db_session_handler = new db_session_handler($db);
session_set_save_handler($db_session_handler,true);
session_start();

echo session_id();

echo "
CREATE TABLE IF NOT EXISTS `session_table` (
  `sess_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `user` int(11) NOT NULL,
  `start` datetime NOT NULL,
  `last_activity` datetime NOT NULL,
  `expires` datetime NOT NULL,
  `sess_data` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`sess_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
";      
?>
like image 579
SpacePhoenix Avatar asked Apr 14 '26 00:04

SpacePhoenix


1 Answers

Looks like FireFox and Chrome were the culprits, after completely uninstalling them then reinstalling them the problem has gone away so probably something in each has gotten corrupted at some point

like image 118
SpacePhoenix Avatar answered Apr 16 '26 13:04

SpacePhoenix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!