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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:50:29+00:00 2026-05-13T05:50:29+00:00

I’ve (so far) been handling user sessions with client-side cookies and database entries. When

  • 0

I’ve (so far) been handling user sessions with client-side cookies and database entries.

When the user logs in, I generate a guid and place it in a cookie on the client’s computer. Then I create an entry in a ‘sessions’ MySQL table, and add there the guid, ip address, the username, privileges, etc. Then when the user accesses the page, I check if there’s a session cookie. if so, I check the database for the guid in the cookie and make sure the ip address matches. If it does then the user is logged in with the rest of the information in the db table. If something is wrong (bad ip address, expired session, etc) I remove the database entry and remove the guid cookie.

I’ve never used the $_SESSION global before.

Is my way good practice or do I need to re-think how I’m handling this?

  • 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-13T05:50:29+00:00Added an answer on May 13, 2026 at 5:50 am

    It sounds like you’ve got the basics covered. However, if you’re doing that all manually, then you are effectively just implementing your own $_SESSION, and not taking advantage of the fact that it can already do all that for you.

    If you want to use a database to handle a session, you can override the default session handling with your own. Take a look at session_set_save_handler(). I do this in my apps.

    class SessionHandler
    {
    
        public function open($save_path, $session_name)
        {
            $this->sessionName = $session_name;
        return(true);
        }
        public function close() {
            //stuff
        }
    
        public function read($id) {
            $expiretime = date("Y-m-d H:i:s",time() - $this->maxLifeTime);
            $sql = "SELECT * FROM sessions where sessionid='".$this->db->escapeData($id)."' AND lastupdated>='".$expiretime."' LIMIT 1";
        $result = $this->db->query($sql);
            //etc.
        }
    
        //etc.
    
        public function setAsSessionHandler()
        {
        session_set_save_handler(
            array($this,'open'),
            array($this,'close'),
            array($this,'read'),
            array($this,'write'),
            array($this,'destroy'),
            array($this,'gc')
        );
        }
    }
    
    $sessionHandler = new SessionHandler();
    $sessionHandler->setAsSessionHandler();
    

    You can have all the functionality you just described that you’ve implemented yourself by using this, but still have the power of $_SESSION to do it for you.

    For instance, if you wanted to add an IP check to see if the session is still valid before you start it, you can add that as part of the “open” function. If you wanted to write the session data to ten different databases (not that you would), you could accomplish this in the ‘write’ function.

    Those functions are all used based on how you use $_SESSION, and by putting them into a simple class, you can manage how it works very effectively.

    You’ll see that the session id is a parameter passed to the read/write/destroy functions, and you’ll still manage this the same way using your GUID generation routine. However, you could stick the guid generation and checks into this session manager class and simply have the open() function do them. Centralized, no muss, no fuss.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I need to clean up various Word 'smart' characters in user input, including but
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has

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.