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 8820365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:35:27+00:00 2026-06-14T05:35:27+00:00

i have a very specific problem regarding a custom session handler using a mysql

  • 0

i have a very specific problem regarding a custom session handler using a mysql database. I looked through other topics since there are some topics here involving custom session handler but no one of them answered my question.
I use a custom session handler in a class called “sessions”:

require_once "mySQL.php";

class sessions {
private $db;
public function __construct(){
    $this->db=new mySQL('localhost', user, pw, database);
    session_set_save_handler(array(&$this,"open"),
                     array(&$this,"close"),
                     array(&$this,"read"),
                     array(&$this,"write"),
                     array(&$this,"destroy"),
                     array(&$this,"gc"));
    register_shutdown_function('session_write_close');
    session_start();
} 
public function read($sessionID){
    $abfrage="SELECT * FROM sessions WHERE id='".$sessionID."'";
    $result= $this->db->query($abfrage);
    if ($result == false){
        return '';
    }
    if (count($result) > 0){
        return $result[0]["value"];
    }
    else{ 
    return '';
    }
}

In “mySQL.php” there is just a class “mySQL” written which creates a mysqli connection to the database. This is working and not the problem. I would like to draw the focus to the “read” routine because this seems to be the primary problem. If i now create a session via

require_once "classes/sessions.php";
$SESSION=new sessions();
$token=hash("sha256",session_id().time());
$_SESSION['token']=$token;
$_SESSION['running']=1

and write values into the session the right database value is created:

id (varchar 64) = 54ul84nhc3aimqc55efcs49js4
lastUpdated (int 11) = 1352226239
start (int 11) = 1352226233
value (varchar 20000) =     token|s:64:"2b92aa848a86741ef11d800c3dce527fdcee264f50de10381941241c3d9cc9ee";running|i:1;

and even the garbage collector works fine but when i reload the page and try to echo

$_SESSION['running']

i get the error “Notice:Undefined index: running”. A debugger run informed that the above posted read routine returns 1 (which would be equal to the boolean “true”?!) instead of the value. I checked that the “sessionID” in the read routine for that case was the one in the session table, so i’m basically lost. It would be very nice if you can help me, because so far i’m already impressed of the possibilities offered by such custom session handler.

Perhaps it’s possible to pose also a side question here. Is it right that when i reload the page the constructor of sessions is called by “$SESSION=new sessions();” even when there is already a session running (however this behaviour does not change the session_id)? Or is this due to my broken session read routine?

Thank you for your help.

EDIT: I’m using PHP/5.4.4 and MySQL 5.5.25a in a xampp distribution along with phpmyAdmin 3.5.2 to set up the database. The session table is a table of format “MEMORY”, that’s why i’m using a varchar 20000 field instead of a text field. I just saw this article Why does mysql_query() return TRUE with a SELECT statement?
stating that a mysql_query returns true from time to time when the server interrupts connections suddenly. However i can’t fiddle out what may be the reason for this.
I’m adding now the mySQL class, perhaps a error in there is involved in this problem:

<?php
class mySQL
{
public $mySQLiObj;
public $lastSQLQuery;
public $lastSQLStatus;
public $lastInsertID;
public function __construct($server,$user,$password,$db,$port="3306"){
    //erstelle db-objekt
    $this->mySQLiObj= new mysqli($server,$user,$password,$db,$port);
    if (mysqli_connect_errno()){
        echo "Keine Verbindung zum MySQL-Server möglich.";
        trigger_error("MYSQL-Connection-Error",E_USER_ERROR);           
        die();
    }
    //Zeichensatz auf utf8 setzen
    $this->query("SET NAMES utf8");
}
public function __destruct(){
    $this->mySQLiObj->close();
}
public function query($sqlQuery,$resultset=false){
    $this->lastSQLQuery=$sqlQuery;
    $result=$this->mySQLiObj->query($sqlQuery);
    if($resultset == true){
        if($result == false) {
            $this->lastSQLStatus=false;
        }
        else
        {
            $this->lastSQLStatus = true;
        }
        return $result;
    }
    if($resultset == false) {
        $return = $this->makeArrayResult($result);
        return $return;
    }
}
private function makeArrayResult($ResultObj){
    if ($ResultObj==false){
        //Fehler aufgetreten
        $this->lastSQLStatus=false;
        return false;
    }
    else if ($ResultObj == true) {
        //UPDATE,INSERT etc. statement es wird nur true zurückgegeben
        $this->lastSQLStatus = true;
        $this->lastInsertID = $this->mySQLiObj->insert_id;
        return true;
    }
    else if ($ResultObj->num_rows == 0){
        //Kein Ergebnis bei SELECT,SHOW,DESCRIBE oder EXPLAIN Statement
        $this->lastSQLStatus = true;
        return array();
    }
    else {
        $array = array();
        while($line = $ResultObj->fetch_array(MYSQLI_ASSOC)){
            array_push($array,$line);
        }
        $this->lastSQLStatus=true;
        return $array;
    }
}

}
  • 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-06-14T05:35:28+00:00Added an answer on June 14, 2026 at 5:35 am

    so after a lot of try and error and looking back into the book where i got the basic layout of the session handler from i answered this question on my own.
    Seems like this

    $SESSION=new sessions();
    

    was the crucial point. Instead of creating a session by explicit call one has to call

    session_set_save_handler(new sessions);
    

    and remove the line out of the constructor in the sessions class. However it’s interesting that such weird error arises by that.

    Thanks for taking a look, perhaps this helps someone.

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

Sidebar

Related Questions

The problem that I have is somehow very specific. I have to implement a
I have a custom closed-hashset/open-addressing (i.e. no linked lists) class. It's very specific to
I have a very odd issue. When I execute a specific database stored procedure
I have a very specific question regarding my Hangman program. You will probably want
Good day to all, i have a problem regarding CSV parse using File Helper.
I have a very specific problem in T-SQL. If I can solve this example
I have a very specific problem here. I have a multi-dimensional array that I
I have a specific problem regarding my website that I built with HTML, CSS,
I have a very specific case that I need to debug. I need to
I have a very specific SSL issue on my Android. If 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.