Current Location: Home> Function Categories> session_create_id

session_create_id

Create a new session ID
Name:session_create_id
Category:Session Session
Programming Language:php
One-line Description:Generate a new session ID

Function name: session_create_id()

Function description: session_create_id() is used to generate a new session ID. This function can be used to customize how session ID is generated.

Function usage: session_create_id([string $prefix = ""])

Parameter description:

  • $prefix (optional): Prefix for session ID. The default is an empty string.

Return value: Returns a newly generated session ID.

Example:

  1. Generate a new session ID:
 $id = session_create_id(); echo $id;

The output is similar: 2e9g5fpqf6jj4q3q2s2o2f0o2c

  1. Generate a prefixed session ID:
 $id = session_create_id("myapp_"); echo $id;

The output is similar: myapp_2e9g5fpqf6jj4q3q2s2o2f0o2c

  1. How to generate a custom session ID:
 function custom_session_id() { $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $length = 32; $id = ''; for ($i = 0; $i < $length; $i++) { $id .= $chars[random_int(0, strlen($chars) - 1)]; } return $id; } session_id(custom_session_id()); echo session_id();

The output is similar: gEHLQaOcD4f3GhIjKlmnOpQrStUvWxYz

Notes:

  • The session_create_id() function is available in PHP 7.1.0 and above.
  • If the session has been started before calling session_create_id(), the newly generated session ID will not take effect immediately. The session_regenerate_id() function needs to be called to update the session ID.
Similar Functions
Popular Articles