Get session ID in php

Session variables are supported by PHP 4 and are the method of passing information to subsequent Web pages. When sessions are enabled, a user to your Web page is assigned a unique session identifier. <?php session_start(); ?> <HTML> <TITLE>Get PHP Session ID</TITLE> <HEAD> </HEAD> <BODY> <?php echo "The session ID is ".session_id()."<br>"; ?> </BODY> </HTML>
Continue reading…

Enjoyed this post? Share it!

 
 

Encode session data to a string using PHP

Developers may want to save all the current session variables in string format and extract them back into variables at a later point. This can be achieved using session_encode and session_decode functions. <?php session_register("my_items"); $my_items = array("one", "two");   $string_items = session_encode(); $fp = @fopen ("save_items.txt", "w") or die ("Cannot open save_items.txt"); @fwrite ($fp, $string_items); […]
Continue reading…

Enjoyed this post? Share it!

 
 

Save objects in session using PHP

It is easy to store an object in a session in PHP. We can do it using session_register(). We must always include the class definition in every place you use the object. person_class.inc: <?php // // File: person_class.inc // Contains the class definition necessary to let an object be a session // variable. // class […]
Continue reading…

Enjoyed this post? Share it!

 
 

Clear all variables in current session in PHP

We can use the session_destroy() function for deleting all the session variables in the current session. The session_destroy() function will delete the session data from the session store. It will not delete session variables’ contents. The session_unset() function can be used to go through all session variables and remove them from the symbol table. <?php […]
Continue reading…

Enjoyed this post? Share it!

 
 

Remove a session variable in PHP

We can use PHP’s session_unregister() function instead of unset() to remove the variable from the session. The session_unregister() function removes the specified variable from the session registry, so that when the session is saved, it will not contain the newly unregistered variable. This function does not delete the variable’s contents. <?php session_register(’my_var’); if (session_is_registered(’my_var’)) { […]
Continue reading…

Enjoyed this post? Share it!

 
 

Change session name in PHP from PHPSESSID

The session_name() function can be used to change the name of the session cookie.The new session name must be alphanumeric and the session_name() function must be called before the session_start() function or session_register() function. <?php $old_session = session_name(’SomeSiteSession’); // // when called with no parameters session_name // simply returns the current session name // $new_session […]
Continue reading…

Enjoyed this post? Share it!

 
 

Save PHP sessions to a database

We can use the session_set_save_handler() function to register functions that are working with the database. session_set_save_handler() sets the user-level session storage functions which are used for storing and retrieving data associated with a session. This is most useful when a storage method other than those supplied by PHP sessions is preferred. i.e. Storing the session […]
Continue reading…

Enjoyed this post? Share it!

 
 

Create a session variable in PHP

The session_register() function in php can be used to register a session variable. session_register() registers the global variable with that name in the current session. Example: <?php session_register(’my_variable’); $my_variable = $my_variable ? $my_variable + 1 : 200; echo $my_variable; ?> Session variables are simply regular variables that are declared using the session_register() function. They can […]
Continue reading…

Enjoyed this post? Share it!

 
 

Retrieve the Session ID of the current session using PHP

There are two ways to get the session id for a visitor. A call to the session_id() function will return the session ID value. Another way is to use the built-in constant SID. SID contains a string of session ID name and value. Example code to retrieve the session ID: <?php session_start(); print("<html><b>");   $sid […]
Continue reading…

Enjoyed this post? Share it!

 
 

Session IDs in PHP

A session ID is an identification string of a session. The PHP engine maintains multiple sessions concurrently one per visitor to the site. Session IDs are created and maintained by the PHP engine to identify sessions. When a visitor comes to your Web site requesting the first PHP page for the first time, the PHP […]
Continue reading…

Enjoyed this post? Share it!

 
 

Retrieve values from the current session in PHP

We can retrieve the values stored in a session by using the pre-defined associative array called $_SESSION. The following PHP script shows you how to retrieve values from the session: <?php session_start(); print("<html><b>"); $_SESSION[’sitename’] = "W3M’; $sitename = $_SESSION["sitename"]; print("Value of sitename has been retrieved: ".$sitename."\n");   print("</b></html>\n"); ?> Output: Value of sitename has been […]
Continue reading…

Enjoyed this post? Share it!

 
 

Save information in the current session in PHP

The pre-defined associative array called $_SESSION can be used to store session specific information. The following PHP script shows you how to save values to the session: Example code: <?php session_start(); print("<html><b>");   $_SESSION["sitename"] = "W3M"; print("A value saved in the session named as sitename.\n");   $_SESSION["MyChoice"] = "red"; print("A value saved in the session […]
Continue reading…

Enjoyed this post? Share it!

 
 

How to enable session support in PHP

The session support can be turned on automatically at the site level, or manually in each PHP page script: Turning on session support automatically at the site level can be done by setting session.auto_start = 1 in php.ini. Turning on session support manually in each page script can be done by calling session_start() funtion.
Continue reading…

Enjoyed this post? Share it!

 
 

Sessions in PHP : Part 4 – Cleaning and destroying PHP session

The unset() function is used to unset or free a session variable that has been set. A session can be completely destroyed by using the session_destroy() function. Example code: 1 2 3 <?php unset($_SESSION[’views’]); ?> All the stored data of the session will be deleted on calling the session_destroy() function. Example code: 1 2 3 […]
Continue reading…

Enjoyed this post? Share it!

 
 

Sessions in PHP : Part 3 – Storing and retrieving session variables

The $_SESSION associative array is used to store and retrieve session variables. This is a ‘superglobal’, or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods. Example code: 1 2 3 4 5 […]
Continue reading…

Enjoyed this post? Share it!

 
 

Sessions in PHP : Part 2 – Starting a session

Before storing the information for a user session, the session should be started using the session_start() function. This function should appear before the tag. Example code: 1 2 3 4 5 6 7 8 9 <?php session_start(); ?>   <html> <body>   //rest of the code   </body> </html> The above code will register the […]
Continue reading…

Enjoyed this post? Share it!