PHP - Session

A html website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. for example create user management system in php username required to another page it can not possible in html website this solution is solve php session.

The above problem is solves php session by allowing you to store user information on the server for later use (i.e. username). However, this session information is temporary and is usually deleted very quickly after the user has left the website that means sessions is destroy.

Starting PHP Session

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 page heading.


<?php
	session_start();
?>								
							

Storing a Session Variable

When you want to store user data in a session use the $_SESSION[''] associative array.the following example for storing session value in session variable.


<?php
	session_start();
	$_SESSION[‘uname’] = $username;
?>							
							

Destroying a Session

In php you can destroying session using two method one is unset a session and second is destroy a session the following both type method example.

Using Destroy


<?php
	session_destroy();
?>									
							

Using Unset


<?php
	unset($_SESSION['uname']); 
?>								
							

Count Pageview Using PHP Session


<?php
	session_start();
	if(isset($_SESSION['view']))
	{
		$_SESSION['view']=$_SESSION['view']+1;
	}
	else
	{
		$_SESSION['view']=1;
	}
	echo "Page Views Count On This Session:".$_SESSION['view'];
?>									
							
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!