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

  • Home
  • SEARCH
  • 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 8843349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:11:53+00:00 2026-06-14T11:11:53+00:00

I was using $_SESSION to store users IDs but I want to change to

  • 0

I was using $_SESSION to store users IDs but I want to change to using a regular cookie (with a KEY) paired with SQL so I can keep users authenticated when they close their browsers.

How do I create an unique key for each row in my SESSIONs table?

  • 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-14T11:11:54+00:00Added an answer on June 14, 2026 at 11:11 am

    You’re effectively looking to write your own session handler to bypass PHP’s cookie expiration issue on session cookies. This is pretty straightforward, and pretty optimized if done properly.

    Step 1: generating the session ID

    A session ID is unique. However, if you’re planning for permanent sessions, you have to bear a couple of things in mind: you want the session to carry over per browser, regardless of connection issues. So, you can cheat the system a bit by mapping the session ID with the user agent of the navigator (which doesn’t change).

    This allows you to reduce the chance for your session ID generator to generate the same ID for two distinct visitors. The rest is up to a random number generator and a hash algorithm, though – md5(microtime().$_SERVER['REMOTE_ADDR']) is usually a trusted friend.

    Step 2: storing and retrieving the data

    Storing the data is also trivial. Your aim is to create a MySQL table with a minimum of two columns: the session key (set to PRIMARY), and the data (serialized array for the simplest form, stored as TEXT).

    When you create a session, just insert a new row and watch for errors. If an error comes up, the key was already used, so you’ll need to re-generate another. If the row was inserted successfully – all good, you now have a session row for the user! All you need to do from there on is to read/write to this row as you see fit.

    Cookie the user with the session ID, and you’re done!

    Caveats

    • Never ever use a userID as session ID. Cookies can be very easily manipulated. You want the cookie value to be random and completely separated from the user. It should be meaningless.
    • You’ll need to write clean-up code to clean up the session table from time to time. A good way to do this is to keep track of when a session was last used – and delete accordingly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to store the user's ID in the session using django.contrib.auth.login . But
I'm using PHP/MySQL, and I want to publish for some offline users, I have
I am using the session object to store success/error messages based on user actions.
i was developing my php application and using mysql to store session data and
I am using the following code to store values in aspx session. Code: $(div.menu_body
How can I access a user session objects from another thread? I want to
I have entities stored in RavenDB (build #888). I need to change theirs IDs,
I am using servlets for the first time but I made a lot of
I am creating a site using AngularJS that will allow users to create accounts
I have a variable that users can modify in a JSP/Struts application that must

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.