Tuesday, 19 February 2013

PHP Session Example and PHP Session Tutorial | PHP Tutorials For beginners

In this tutorial of php code for beginners we will see how to use php session and give you proper php session tutorial.


What is php session:-

In normal HTML web site the boundary of data is limited that is we can not transfer data from one page to another. But in some cases we require to pass our data to another page like login form or shopping cart websites. In this situation php session help us to transfer the data. The data is user information which is store on server for later use. This information is temporary and it will be deleted when browser is closed.

A Session variable is used to store the information of user. We can store any information required for user in session.

Sessions work by creating a unique identification number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage.

Syntax of session is:- $_SESSION['variable_name'].

How to start php session:-

We can to use session directly and store the user information. First we need to start the session. To start session we have to write session_start() at the beginning of our code, before any HTML or text.

Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent.

Here is the example of how to start the session.

<?php
session_start(); // starting our PHP session! 
?>

Storing data in a session variable:-

When we want to store user information in session the we have to use $_SESSION. $_SESSION is an associative array. In this all information are stored and retried. 

Example:-
<?php
session_start(); 
$_SESSION['topic'] = "PHP Session example."; // storing session data
echo $_SESSION['topic']; //retrieving session data
?>
Output:-
PHP Session example.

In this example we learned how to store a variable to the session associative array $_SESSION and also how to retrieve data from that same array.

This example show you how to count the number of time user came in perticular page.
<?php 
session_start(); 
($_SESSION['number']) ? $_SESSION['number']++ : $_SESSION['number'] = 1;
?>
This is your <?php echo( $_SESSION['number'] ); ?> time in this page.

Destroying session data:-

Php session data are temporary and will be deleted when we leave page or browser. But in some situation we need to clear user information as soon as task is completed. At this situation we use php function to destroy session. 

Example:-
<?php
session_start();  
if(isset($_SESSION['topic'])){
unset($_SESSION['topic']);
}
?>
To completely destroy the session you can call the session_destroy function.

Example:-
<?php
session_start(); 
session_destroy();
?>
This session_destroy() will completely remove the session.


Hope this php tutorial is useful for you. Keep following www.phpcodeforbeginner.blogspot.in for more help.

3 comments:

  1. Great php tutorial for php beginners.

    ReplyDelete
  2. Great blog. All posts have something to learn. Your work is very good and i appreciate you and hopping for some more informative posts.

    ReplyDelete