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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:49:39+00:00 2026-06-10T14:49:39+00:00

<?php try{ $conn = new PDO(mysql:host=$DB_SERVER;dbname=$DB_NAME,$DB_USER,$DB_PASS); } class SessionManager { var $life_time; function SessionManager()

  • 0
<?php
    try{
     $conn = new PDO("mysql:host=$DB_SERVER;dbname=$DB_NAME",$DB_USER,$DB_PASS);
    }

class SessionManager {

        var $life_time;
        function SessionManager() {
            global $conn;
            $this->life_time = get_cfg_var("session.gc_maxlifetime");

            // Register this object as the session handler
            session_set_save_handler( 
                array( &$this, "open" ), 
                array( &$this, "close" ),
                array( &$this, "read" ),
                array( &$this, "write" ),
                array( &$this, "destroy"),
                array( &$this, "gc" )
            );
        }



        function read( $id ) {
            global $conn;
            $data = "";
            $time = time();
            $newid = $id;       
            $sql = "SELECT session_data FROM session_tmp WHERE session_id=? AND expired_date > ?";
            $q = $conn->prepare($sql);
            $result = $q->execute(array($newid, $time));
            while($r = $q->fetch(PDO::FETCH_ASSOC)){
                $data = $row['session_data'];
            }
            return $data;
        }


        function write( $id, $data ) {            
            $time = time() + $this->life_time;
            global $conn;
            $newid = $id;
            $newdata = $data;
            $sql = "SELECT session_id FROM session_tmp WHERE session_id = ?"; // error happen here!!!!
            $q = $conn->prepare($sql);
            $result = $q->execute(array($newid));
            return TRUE;
        }
}

I add global $conn; onto function read() and it fix the error. I don’t know why global $conn; cannot fix the error on function write(). How to fix the error?

  • 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-10T14:49:41+00:00Added an answer on June 10, 2026 at 2:49 pm

    In PHP, a variable like $conn is available in a function if and only if it was defined inside the function. The word ‘defined inside of’ means ‘assigned inside of’.

    In your case, $conn is not defined in the write() function, but outside of all functions. Therefore, you need to tell write() that it should refer to the global variable $conn. This is done using

       global $conn;
    

    Note, that use of global is strongly bad code style! You should never need to use global.

    Instead of global, you might do something like this:

    // This class should actually be a singleton class
    // http://en.wikipedia.org/wiki/Singleton_pattern
    //
    class CDBSession {
    
      protected 
        $conn;
    
      public function __construct( $DB_SERVER, $DB_NAME, $DB_USER,$DB_PASS ) {
    
          $this->conn 
             = new PDO("mysql:host=$DB_SERVER;dbname=$DB_NAME",$DB_USER,$DB_PASS);
    
      } // __construct
    
      public function getPDO() {
    
          return $this->conn;
    
      }      
    
    }
    
    class SessionManager {
    
      protected 
        $_PDOSession;
    
      public function __construct( CDBSession $theDBSession ) {
    
          $this->_PDOSession = $theDBSession;
    
      } // __construct
    
    
      ... other methods need to access $this->_PDOSession->getPDO() too ...
    
    } // class SessionManager
    

    Finally, you have to use SessionManager like this:

    $mySessionManager 
      = new SessionManager(new CDBSession($DB_SERVER, $DB_NAME, $DB_USER,$DB_PASS));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

<?php try { $db = new PDO(mysql:host=localhost;dbname=DBNAME, USER, PASSWD); $stmt = $db->prepare(SELECT id, name
<?php try { $dbh = new PDO('pgsql:host=localhost;port=5432;dbname=###;user=###;password=##'); echo PDO connection object created; } catch(PDOException
I have the following code : <?php try { # MySQL with PDO_MYSQL $DBH
I got this example from the php site: <?php try { $dbh = new
Hi I'm new to php mysql development. I'm using wamp server on windows. php5.3.4,
I am trying to connection with php-MySQL using below code try{ HttpClient httpclient =
Here's my code: include 'conn.php'; $conn = new Connection(); $query = 'SELECT EmailVerified, Blocked
I've wrote a quick PHP class to ease the access to a mysql database.
Im very new in php and try to use cookie but it is not
Background: Suppose I have the following obviously-incorrect PHP: try{ $vtest = ''; print(array_pop($vtest)); }catch(Exception

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.