Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a unique session ID in php

On our websites we want to make it possible to share sessions accros multiple domains. All these websites are on the same server but some of them have a different IP address.

The possible solution I found was to set the session ID myself:

<?php
session_id($someUniqueHash);
?>

And this works, if I make the hash like md5('test'). On a other domain on te same server we have the session again.

The problem is generating the ID. I see some solutions on the internet with microtime etc, but when I use that approach I can't predict the session ID on the other domain / PHP page.

Does anyone have an idea? Or shouldn't we implement this? Are there other options to share session over multiple domains? (NOT subdomains!)

like image 547
Kees Schepers Avatar asked Jun 22 '12 11:06

Kees Schepers


People also ask

How can I create session ID in php?

Starting a PHP Session To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user.

Is php session id unique?

It's not a firm uniqueness condition especially from a security perspective.

How can you generate a session ID?

The session ID is generated using the Random Number Generator (RNG) cryptographic provider. The service provider returns a sequence of 15 randomly generated numbers (15 bytes x 8 bit = 120 bits). The array of random numbers is then mapped to valid URL characters and returned as a string.

How can I get php session id in php?

Check Session ID With session_id() Function Before you can check for a session ID, you need to start a PHP session with session_start() . Afterward, you can call on the session_id() function. This function will return the current session id.


1 Answers

I've achieved this system by using an OAuth type flow but we replaced the Consumer with the User.

So each domain would have the authenticated Access_Token in its own session. You would then use that Access_Token to get information about user from an api.

I also solved the session problem using session_set_save_handler and storing sessions in a database table... This table would have the Access_Token also, making it really easy to find the session with a DB Query.

Hope this helps with ideas.

like image 193
Lex Avatar answered Sep 28 '22 09:09

Lex