Session id generation process and sessionid length _ php instance

Source: Internet
Author: User
Tags php session
This article describes the session_id () function in php in detail, session id generation process and sessionid length related information. For more information, see session_id () in php () function prototype and description session_id () function description: stringsession_id ([string $ id]) session_id () can be used to obtain/set the current session ID. To easily append session IDs to URLs, you can use the constant SID to obtain strings...

Prototype and description of session_id () function in php

Session_id () function description:

string session_id ([ string $id ] )

Session_id () can be used to obtain/set the current session ID.

To easily append a session ID to a URL, you can use the constant SID to obtain the session name and ID expressed in string format. See session processing.

Session_id () function parameters:

Id

If the value of the id parameter is specified, the specified value is used as the session ID. The session_id () function must be called before the session_start () function is called. Different session managers have different restrictions on the characters that can be used in session IDs. For example, the file Session Manager allows only the following characters in the session ID: a-z A-Z 0-9, (comma) and-(minus sign)

Note: If the session ID is transmitted using the cookie method and the id parameter is specified, a new cookie will be sent to the client after session_start () is called, whether the current session ID is the same as the new specified session ID or not.

Session_id () function return value:

Session_id () returns the ID of the current session. If no session exists, an empty string ("") is returned ("").

Php session ID generation and session ID Length

When a visitor accesses your website, a unique id is assigned, which is the session id. this id can be stored in a cookie on the user side or transmitted through a URL. session is used to track the sessions of each user, and the SessionID generated by the server is used to identify and distinguish users. Sessions are stored in the server's memory, and SessionID is stored in the server's memory and the client's Cookie. In this way, when a user sends a request, the server compares the SessionID recorded in the user Cookie with the SessionID in the server memory to find the user's Session for operation. Therefore, Session cannot be used if the client disables cookies.

How is php session ID generated? Introduction to the default PHP session id Generation Algorithm

Take the source code of php5.3.6 as an example to enter the/ext/session directory. The function that generates the session id is located in row 345 of the session. c file. The c function prototype is as follows:

PHPAPI char * php_session_create_id (PS_CREATE_SID_ARGS );

If you are interested, you can analyze the implementation principles.

PHP default session id Length

Some actual session IDs generated by the php 5.4.6 server are as follows:

Sess_00nrqa20hjrlaiac0eustmi4q5 sess_89j9ifuqrbplk0rti2va2k1ha0 sess_g2rv1kd6ijsj6g6c9jq5mqglv5
Sess_04es72a83tqsl0jqd3cvrc4s01 sess_8b7a5lme60g49lvk4u4jiemdn1 sess_g3uk6d1_bashg5eoq0b2k7vsk0
Sess_04u0ns0oobh2g93t009bij2rq0 sess_8dfvkiv8ml44fdqrk1rcmjchs4 sess_g64tddhbo8pbj8bs7bel44rf35
Sess_0592dolr5m0k392fah6c9preg7 sess_8fhgkjuakhatbeg2fa14lo84q1 sess_g6kl828qqsnvdrse7ff52cl2a4
Sess_066g8irr0m22iqotscepub4e13 sess_8gn03i9j1tta7655106j6nl1l53 sess_g8t45j6qce7mf55nk14cotj5i4
Sess_08nr1fav9jqs2pdi5qlpsmd247 sess_8gvu05313o7p9usksaacaiegu6 sess_gbtjmr57iat86c8ve86ar5nh30

It can be seen that the specific session id is the part after "sess _" and the length is 26 bits. The length is limited to php 5.4.6.

The sample code for php session_id () is as follows:

Output session_id () Detailed code:

<? Php session_start (); echo session_id (); // output 08nr1fav9jqs2pdi5qlpsmd247?>

Detailed code for setting session_id:

<? Php session_id ("www.169it.com"); session_start (); echo session_id (); // output www.169it.com?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.