Rechercher une fonction PHP

session_create_id

(PHP 7 >= 7.1.0)

session_create_idCreate new session id

Description

session_create_id ([ string $prefix ] ) : string

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.

PHP: session_create_id - Manual Home of Manuel PHP  Contents Haut

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.

PHP: session_create_id - Manual Home of Manuel PHP  Contents Haut

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.

PHP: session_create_id - Manual Home of Manuel PHP  Contents Haut

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
?>

PHP: session_create_id - Manual Home of Manuel PHP  Contents Haut

See Also

Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.session-create-id.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut