The first step involved in the use of PHP sessions is naturally indicating to the PHP interpreter that some kind of persistent data must be stored in the course of a session, in such a way that this data will maintain its value across different HTTP requests. How is this done? Basically, when a session is created, these are the key operations performed by the PHP session management mechanism:
Right, these are at least the basic operations performed internally by the PHP session module, when a new session is started. As you saw, they’re very comprehensive. Before I show you some illustrative hands-on examples, let me explain how session IDs are propagated across different HTTP requests. First off, the PHP session module uses cookies as the default way to propagate session IDs between different pages. Additionally, IDs can be propagated by GET/POST requests, by appending to each URL the name of the session together with the corresponding session ID. The following example shows how to pass a session ID between pages: <?php In addition, PHP offers the global constant SID, which can be used to propagate session IDs within the URL, as follows: <?php Finally, it’s possible to propagate session IDs by using the powerful “URL rewrite” feature, which automatically parses relative URLs and includes the session ID as part of the querystring. To enable this handy feature, PHP must be configured with the “—enable-trans-id—option and then recompiled. Having explained the different methods for propagating session IDs, here’s a simple script that creates (or resumes) a session, and registers some data in the superglobal $_SESSION array: session_start(); As you can see, the above script is very simple. It first creates a new session or resumes an existing one by the “session_start()” function, and then stores my first and last names respectively as session data. As I explained before, if there is not a previous session, PHP will first generate a pseudo random session ID, then create a session file on the server using that ID, and save the serialized data in this file. In addition, the corresponding session ID will be stored in the client by using a cookie. Of course, this sequence is the default behavior of the session module. However, as you’ll see later on, this process can be entirely changed, in order to utilize either shared memory or database tables for storing session data. Now, I’ll continue exploring the PHP session module, by using some other interesting functions. Have a look at the following example, which uses the “session_id()” function: session_start(); In this case, after running the above script, these are the values outputted to the browser: Old session ID :e0c9904e70f283343f5aba1bad09aa69 As shown above, the “session_id()” function comes in very handy either for retrieving the ID of the current session, or for generating a new session identifier. To increase the overall security of a session, generating a new ID sometimes can be quite useful, in order to avoid possible session ID interceptions from malicious users. In case you need to regenerate the ID of a current session, PHP also provides the “session_regenerate_id()” function, which can be used as follows: session_start(); And the corresponding output for this script would be similar to this: Old session ID is 8245009bcf9e24a738804323e779a3b7 In this example, as its name suggests, the “session_regenerate_id()” function is used to generate a new ID for the current session, but the good thing is that session data is always maintained. This function is extremely useful for avoiding some issues related to session ID fixation, and should be utilized in order to increase the security of session handling scripts. Now that you’ve learned how to use some helpful PHP session functions, handy for manipulating session IDs, it’s time to leap forward and continue exploring other functions included within the session management module. Please keep on reading to learn more.
blog comments powered by Disqus |
|
|
|
|
|
|
|