session_create_id

(PHP 7 >= 7.1.0, PHP 8)

session_create_idCreate new session id

Description

session_create_id(string $prefix = ""): string|false

session_create_id() is used to create new session id for the current session. It returns collision free session id.

If session is not active, collision check is omitted.

Session ID is created according to php.ini settings.

It is important to use the same user ID of your web server for GC task script. Otherwise, you may have permission problems especially with files save handler.

Parameters

prefix

If prefix is specified, new session id is prefixed by prefix. Not all characters are allowed within the session id. Characters in the range a-z A-Z 0-9 , (comma) and - (minus) are allowed.

Return Values

session_create_id() returns new collision free session id for the current session. If it is used without active session, it omits collision check. On failure, false is returned.

Examples

Example #1 session_create_id() example with session_regenerate_id()

<?php
// My session start function support timestamp management
function my_session_start() {
    
session_start();
    
// Do not allow to use too old session ID
    
if (!empty($_SESSION['deleted_time']) && $_SESSION['deleted_time'] < time() - 180) {
        
session_destroy();
        
session_start();
    }
}

// My session regenerate id function
function my_session_regenerate_id() {
    
// Call session_create_id() while session is active to 
    // make sure collision free.
    
if (session_status() != PHP_SESSION_ACTIVE) {
        
session_start();
    }
    
// WARNING: Never use confidential strings for prefix!
    
$newid session_create_id('myprefix-');
    
// Set deleted timestamp. Session data must not be deleted immediately for reasons.
    
$_SESSION['deleted_time'] = time();
    
// Finish session
    
session_commit();
    
// Make sure to accept user defined session ID
    // NOTE: You must enable use_strict_mode for normal operations.
    
ini_set('session.use_strict_mode'0);
    
// Set new custom session ID
    
session_id($newid);
    
// Start with custom session ID
    
session_start();
}

// Make sure use_strict_mode is enabled.
// use_strict_mode is mandatory for security reasons.
ini_set('session.use_strict_mode'1);
my_session_start();

// Session ID must be regenerated when
//  - User logged in
//  - User logged out
//  - Certain period has passed
my_session_regenerate_id();

// Write useful codes
?>

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial