All web programming language is stateless – i.e, you cannot save state variables within the programming of the pages. As they work with requests, every time a page is fully loaded, all variables used are “erased” and with it, its further use is not possible.

Creating_sessions_PHP

In some cases, however, it is necessary that certain information navigate between pages. This requirement is vital in certain cases, such as when it needs to retrieve information in the database. A unique registration code may be necessary on several screens and therefore we need to keep this information, so that, when required, it is available – optimizing thus the productivity and time. In order to assist on this point, PHP provides the calls sessions.

Briefly, session is an array of variables that can be stored on the server and retrieved by the application later. Following, it will be shown how to create, change and delete sessions in PHP.

CREATING SESSIONS IN PHP

Creating a session in PHP takes place in two steps: First you must call the session_start () method, which creates and instantiates the array $ _SESSION used to store variables. Then we create the position in the array indicating the name and the value to be stored.

 

<?php
session_start();
$_SESSION[“new”]= 1;
?>

 

RECOVERING DATA SESSION

Once the value is stored, any application page can work with the stored value or manipulate the session already created.

<?php
$var 1 = $_SESSION[“new”];

$_SESSION[“new”]= 2;
?>

In the above, we have two examples of use mentioned above. To modify the session value, it is not necessary to call any other method of support; just perform the default assignment.

 

EXCLUDING ONE SESSION

The use of session consumes server processing resources – thus it needs to be used carefully. Once a routine is finished and there is no more need for session usage, it must be finalized, as the following example:

 

<?php
unset($_SESSION[“new”]);
?>

 

GENERAL TIPS OF SESSION USE IN PHP

Below are listed some tips that help in optimizing a session:

  • • Avoid using many sessions in the application;
  • • In case of simple traffic information, evaluate other possibilities such as using Storage or Querystrings;
  • • At the end of the process, always delete the session;
  • • Each system user opens a new session instance. Therefore, in the logout routine of users, you need to validate the sessions used and delete them.

By , The most efficient web development environment. Create your web solutions in a fast and innovative way, reducing the development time in up to 80%. Access http://scriptcase.net and learn more.

July 21, 2015

a

You might also like…

The importance of training in the use of software

Today we are going to reflect on the importance of training in the use of software that, when explo...

A sneak peek on scriptcase 9.4.027!

Come check out all the new details for scriptcase version 9.4.027 !! This week we launched our n...

8 tips for working from home

Hello, how are you? We hope you are well! We know that the moment is difficult and many people a...

You might also like…

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.