A Complete guide on PHP Session- Start to Destroy

Posted by Anoushka on January 9th, 2018

Even though you can store data and information with the help of cookies but cookies have some security problems since it is stored in the computer and it may be hacked by the hackers by injecting harmful content that may affect your application. All the cookies data of your website is sent to the server when you request a URL to the server in the browser. When this process is happening for every website you request in your system, for example, if your system stored 5 cookies in your system, each will have 4 kb and browsers have to load 20 kb whenever you load the page, this damage your website’s performance. We can solve this issue by using PHP Session, this PHP session will store cookies on the server instead of storing in the user’s system. Here, every user has identified with the SID or session identifier. This makes PHP Training in Chennai, the most demandable course in the IT market.

Start a PHP Session  

Before you store any data in session variables, start a new session by calling the PHP session start () function. After starting a new session, it will create a new session and generate a unique session ID for every user.

Example code

<?php

Session_start();

?>

Storing and Accessing Session Data

With the use of $_SESSION [] superglobal array, you can store all the session data as key-value pairs. The data can be accessed within the lifetime of a session.

 Example code

 <?php

Session_start();

$_SESSION[“firstname”]=”Ishu”;

$_SESSION[“lastname”]=”Sathya”;

?>

Destroying a Session

To remove certain session data, unset the respective key of the $_SESSION array with the help of the following example

<?php

Session_start();

If (isset($_SESSION[“lastname”])){

    Unset($_SESSION[“lastname”]);

}

?>

Even though you destroy a session, just call a session destroy() function. No need of any special argument of function to call this method.

<?php

Session_start();

Session_destroy();

?>

These are the most important operations in PHP session. Every concept in session is handled practically in PHP Course in Chennai. With the help of PHP Training, one can learn about the benefits of the session over cookies. This will secure your website from unwanted threats. The coding part of PHP session also very simple to understand and learn. Above I have clearly explained the coding part of PHP session. Don’t hesitate to learn new languages. Every language base is from C language, if you know the basic of C language, then start to explore yourself in programming languages.

Like it? Share it!


Anoushka

About the Author

Anoushka
Joined: December 29th, 2017
Articles Posted: 3

More by this author