Tuesday, 12 July 2011

Sessions

A cookie stores info within the browser, a maximum of 4KB.Then, whenever the
browser requests a page on your Web site, all the data in the cookie is automatically sent to the server
within the request. This means that you can send the data once to the browser, and the data is
automatically available to your script from that moment onward.

Setting a cookie
setcookie(name,value,expires) //there are some more options like domain ,secure , but they are not necessary

Acessing the cookie
$_COOKIE['name'];

Removing Cookie
set the expiry value to the past


2 disadvantages of using Cookies
-Cookies are not very secure
-They are stored in the Browser. Each time a browser requests a URL from the server, all the Cookies must be passed.
 So if there are a lot of cookies stored, it can cause a lot of overhead.

sessions are stored on the server with a session ID.The PHP engine then sends a cookie containing the SID to the browser to store. Then, when the
browser requests a URL on the Web site, it sends the SID cookie back to the server, allowing PHP to
retrieve the session data and make it accessible to your script.

The session IDs generated by PHP are unique, random, and almost impossible to guess, making it very
hard for an attacker to access or change the session data

The session data is stored in a temporary file on the server. The location can be found by

echo 'ini_get( 'session.save_path');

ini_get() lets you access the value of most PHP configuration directives, and ini_set() lets you
set directives

The data stored in sessions is lost when the connection is lost i.e; when the browser is closed.
create a session using session_start();
session_start() must be given before anything is input into the browser because the SESSION ID must
be sent in an HTTP header when a session is created.

Storing info in SESSION superglobal array
$_SESSION['firstName']="Indy";
Any kind of info like an array, can be stored inside a session

Destroy the session after use by
session_destroy();

Count the number of visits
___________________________
<?php
session_start();

if(isset ($_SESSION['view']))
    $_SESSION['view']=$_SESSION['view']+1;
else
    $_SESSION['view']=1;


?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">

</style>
</head>
<body>
   <?php
     echo "No: of visits to the current page in this session=". $_SESSION['view'];
   ?>
    
    </form> 
  
</body>
</html>

No comments:

Post a Comment