WORK WITH SESSION

Posted on Updated on

Image result for WORK WITH SESSION in phpWORK WITH SESSION

An option approach to make information open over the different pages of a whole site is to utilize a PHP Session.

A session makes a document in a transitory index on the server where enlisted session factors and their qualities are put away. This information will be accessible to all pages on the site amid that visit.

The area of the impermanent record is controlled by a setting in the php.ini document called session.save_path. Before utilizing any session variable ensure you have setup this way.

At the point when a session is begun taking after things happen –

PHP first makes an interesting identifier for that specific session which is an arbitrary string of 32 hexadecimal numbers, for example, 3c7foj34c3jj973hjkop2fc937e3443.

A treat called PHPSESSID is consequently sent to the client’s PC to store one of a kind session ID string.

A record is naturally made on the server in the assigned impermanent registry and bears the name of the remarkable identifier prefixed by sess_ ie sess_3c7foj34c3jj973hjkop2fc937e3443.

At the point when a PHP script needs to recover the esteem from a session variable, PHP consequently gets the one of a kind session identifier string from the PHPSESSID treat and afterward looks in its brief registry for the document bearing that name and an approval should be possible by contrasting both qualities.

A session finishes when the client loses the program or in the wake of leaving the site, the server will end the session after a foreordained timeframe, usually 30 minutes span.

Beginning a PHP Session

A PHP session is effectively begun by making a call to the session_start() function.This work first checks if a session is now begun and if none is begun then it begins one. It is prescribed to put the call to session_start() toward the start of the page.

Session factors are put away in affiliated cluster called $_SESSION[]. These factors can be gotten to amid lifetime of a session.

The accompanying case begins a session then enlist a variable called counter that is increased every time the page is gone by amid the session.

Make utilization of isset() capacity to check if session variable is as of now set or not.

Put this code in a test.php document and load this record ordinarily to see the outcome –

<?php

   session_start();

   if( isset( $_SESSION[‘counter’] ) ) {

      $_SESSION[‘counter’] += 1;

   }else {

      $_SESSION[‘counter’] = 1;

   }

   $msg = “You have visited this page “.  $_SESSION[‘counter’];

   $msg .= “in this session.”;

?>

<html>

   <head>

      <title>Setting up a PHP session</title>

   </head>

   <body>

      <?php  echo ( $msg ); ?>

   </body>

</html>

Destroying a PHP Session

A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

Here is the example to unset a single variable –

<?php

   unset($_SESSION[‘counter’]);

?>

Here is the call which will destroy all the session variables –

<?php

   session_destroy();

?>

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s