Breaking

LightBlog

Saturday 4 July 2015

PHP session and cookies


PHP Session:  $_SESSION is just like a memory area which use for storing data about site visitors. In $_SESSION if you want to store information about visitor on your site then you can use this Global Array in PHP Programming. $_SESSION stored a unique ID each visitor online. It is variable is used to store information about, or change settings for user session. Session variable hold information about single user.  Session information is temporary and will be deleting after the user has left the website. If you can use the session in PHP then you can call it with session_start( ) at the begin of the PHP script.

                                   

Program


 
    <html>
     <form method="post">
      Username :<input type="text" name="username"/>
      Password :<input type="password" name="email"/>
     <input name="Submit" type="submit" value="Submit"/>
    </form>
    </html>
    <?php
     session_start();
     $_SESSION['username']= $_POST['username'];
     $_SESSION['email']= $_POST['email'];
     echo "Username :".$_SESSION['username']."<BR>";
     echo " password ". $_SESSION['email'];
    ?>

Here Video Tutorial

                                        


PHP Cookies: A Cookie is an item of data that web servers save to your computer hard disk via a web browser. It can contain almost any alphanumeric information and can be retrieved from your computer and returned to the server. Common uses include session tracking, maintaining data across multiple visits holding shopping cart content store login details and more.

                                           

Cookies have some important security features:

·         A Cookie can only be read by the web site or domain that created it.
·         A Single cookie cannot exceed 4 kilobytes in size.
·         A single domain cannot set more than 20 cookies.
·         The maximum number of cookies that may be set on a user system is 300.
The setcookie( ) is used to set a cookie. And setcookie( ) must use before the <html>tag.

Syntax: setcookie(name, value, expire, path, domain);

Cookies some parameters

·         Name – the name of cookie
·         Value – the value of cookie and cookie’s contents.
·         Expire- it is the time set to expire the cookies.
·         Path- the path of the cookie on the server .
·         Domain- the Internet domain of the cookies.

Program


<?php
$expire= time( )+60*60*24*30;
setcookie("username", "amandeep" , $expire);
?>
To retrieve the cookie using the $_COOKIE variable
<?php
echo $_COOKIE["username"];
print_r($_COOKIE);
?>

Here Video Tutorial
                                    


Adbox