Cookies & Session In PHP

Cookie:

  • Cookie is state management object used to maintain state of application.
  • cookie data is storing in browser memory location that data we can access from only web page in application.

Cookies are divided into two type

  1. In-memory Cookie.[RAM]
  2. Persistent Cookie.[HD]

In-Memory:

  • If we crate cookie without explicit expiry time comes under in-memory cookie.
  • thees store data in RAM memory location in client system.
  • the data will destroy when user close the browser.

Persistent Cookie:

  • cookie with explicit expiry time comes under persistent cookie
  • the cookie store the data in the Hard disk memory location and destroy when life time is completed.
  • By using persistent cookie we can maintain to browser in client system up to year up to some year according to browser time

Cookies Function:

  • set Cookie:-using this super global variable we can create cookie.
  • $_COOKIE:- using this super global variable we can read the data of cookie
Create the persistent cookie
<?php
Setcookie(“city”,”Pune”);
Setcookie(“state”,”Maha”,time()+3600);
echo $_cookie[‘city’];
echo $_cookie[‘state’];
?>

SESSION

  • By using this super global variable we can create session and we can access this session.
  • Session is state of management object sue to maintain state of application.
  • session data we can access from any location within the application.
  • session store data in we server.
  • session can store large amount of data.
  • session start using session_start();
  • $_session is use for to set session variable.
  • Set the session before your html tags.
How to start session and How to set the session variable.
Start.html
<?php
Session_start();
$_session[‘x’];
echo $session[‘x’];
?>
GO

p2.php
<?php
Session_start();
echo $_session[‘x’];
echo “Hi”;
?>

Session Id:

  • session id is alpha numeric string genereted by web server when user sends 1st request to server.
  • At the time of receiving request server checks request contain session,cookie or not if request doesn’t contain session cookie then server create new session_id.

Session Perform This Following task if session start:

  1. session_id:-using this function we can get session id generated by web server.
  2. session_unregistered:- To delete session variable.
  3. session_unset:-To delete all session variable.
  4. session_destroy:-To destroy session file.
Simple program for session id
bill.php
<?php
Session_start();
echo session_id().””;
forech($_SESSION as $k => $v)
{
echo $k “=”. $v.””;
}
?>
session ID

Product
Quantity

Bill

Program for Login and Logout using session:
login.php
Login and Logout

Log in

User ID: Password:

<?php
If(isset($_post[‘sub’]))
{
Extract($_POST)
If($uname=”scott” and $pass=”scott123”)
{
Session_start();
$_SESSION[‘aut’]=1;
Echo “”;
}
else
echo “Invalid”;
}
?>

welcome.php

<?php
if(isset($_POST[‘aut’]))
{
echo “Welcome to user”;
}
else
echo “”;
?>
Logout

Logout.php
<?php
Session_start();
Session_ destroy();
echo “Logout successfully ”;
?>

Program for Login Using database and session
login.php
<?php
include(“config.php”);
session_start();
if($_SERVER[“REQUEST_METHOD”] == “POST”)
{
$uname=mysql_real_escape_string($db,$_POST[‘uname’]);
$pass=mysql_real_escape_string($db,$_POST[‘pass’]);
$selectsql=”SELECT id FROM register WHERE uname=’$uname’ and pass=’$pass'”;
$result=mysql_query($db,$selectsql);
$row=mysql_fetch_array($result);
$active=$row[‘active’];
$count=mysql_num_rows($result);
if($count==1)
{
$_SESSION[‘login_user’]=$uname;
header(“location: welcome.php”);
}
else
{
$error=” user name/pass is incorrect”;
}
}
?>
Session.php
<?php
include(‘config.php’);
session_start();
$ses_sql=mysql_query($db,”select uname from register where uname=’$uname’ “);
$row=mysql_fetch_array($ses_sql);
$loggedin_session=$row[‘uname’];
if(!isset($loggedin_session))
{
header(“Location: #”); //put login.php here
}
?>
Welcome.php
<?php
include(‘session.php’);
?>

Welcome

You are now logged in. you can logout by clicking on signout link given below.

Sign Out

//put logout.php here
Logout.php
<?php
session_start();
if(session_destroy())
{
header(“Location: index.php”);
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *