Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:18:12+00:00 2026-05-30T22:18:12+00:00

I have an odd issue. The first time a visitor comes to the site

  • 0

I have an odd issue. The first time a visitor comes to the site and I set anything is the session, it doesn’t stick. The second and all the following times I try to set something it sticks. After the initial try I can destroy the session and set something and it sticks. Its just the initial attempt to save something fails. I’m trying to save something to the session with $_SESSION['uid'] = $row["Id"];. I know the $row["Id"] is valid and holds data (I echoed it).

I am not using standard sessions. I am saving the session into a database. My session class is below. Is there anything I’m missing or doing wrong to explain this behavior?

Update:

Well I tested the session class on its own and it seems to be working :-/ But when I use it in my larger application _write never gets called, though __destruct does get called. Any idea why that may be?

<?php

include_once('db.php');

class PDOSession
{
    protected $pdo;
    protected $table = 'SessionData';

    public function __construct()
    {
        // Get a database connection
        $db = new PDOConnectionFactory();
        $this->pdo = $db->getConnection(true);

        // Start session
        session_set_save_handler(array($this, '_open'),
                                 array($this, '_close'),
                                 array($this, '_read'),
                                 array($this, '_write'),
                                 array($this, '_destroy'),
                                 array($this, '_gc'));
        session_start();
    }

    public function __destruct()
    {
        session_write_close();
    }

    protected function fetchSession($id)
    {
        $stmt = $this->pdo->prepare('SELECT id, data FROM '.$this->table.' WHERE id = :id AND unixtime > :unixtime');
        $stmt->execute(array(':id' => $id, ':unixtime' => (time() - (int)ini_get('session.gc_maxlifetime'))));
        $sessions = $stmt->fetchAll();

        return empty($sessions) ? false : $sessions[0];
    }

    public function _open($savePath, $sessionName)
    {
        return true;
    }

    public function _close()
    {
        return true;
    }

    public function _read($id)
    {
        $session = $this->fetchSession($id);
        return ($session === false) ? false : $session['data'];
    }

    public function _write($id, $sessionData)
    {
        $session = $this->fetchSession($id);
        if($session === false) {
            $stmt = $this->pdo->prepare('INSERT INTO '.$this->table.' (id, data, unixtime) VALUES (:id, :data, :time)');
        } else {
            $stmt = $this->pdo->prepare('UPDATE '.$this->table.' SET data = :data, unixtime = :time WHERE id = :id');
        }
        $stmt->execute(array(
                        ':id' => $id,
                        ':data' => $sessionData,
                        ':time' => time()
                        ));
    }

    public function _destroy($id)
    {
        $stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE id = :id');
        $stmt->execute(array(':id' => $id));
    }

    public function _gc($maxlifetime)
    {
        $stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE unixtime < :time');
        $stmt->execute(array(':time' => (time() - (int) $maxlifetime)));
    }
}
$newPDOSessionStartHere = new PDOSession();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:18:13+00:00Added an answer on May 30, 2026 at 10:18 pm

    I’m a bit of an idiot I guess. I was calling session_destroy() rather than session_unset() to clear things out at the top of my authentication script. The class works fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an odd issue while using FlashCS4. I have a textfield that, when
I have a very odd issue. When I execute a specific database stored procedure
I have an odd linq subquery issue. Given the following data structure: Parents Children
I am learning JQuery, and I have ran into an odd issue. I made
This is very odd. I have never had this issue with ShowDialog in C#.
I have an odd problem with Django. I have a set of objects that
I have a little bit of an odd issue. When I run this SQL:
I have this odd problem with a third party script. On our site a
We have a very odd problem here with SVN. Let me first explain the
I seem to be having an odd issue whereby every time I try to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.