Posted By:
7 Mar, 2014 12:10 pm PST

How to identify user using cookie?

 

How to identify user using cookie?

 

Cookies are the mechanism used to track and indentify user or return user. Cookies are a mechanism for storing data in the remote browser instead storing information on server side. You can set cookies using the setcookie() or setrawcookie() function.

Cookies are associate with HTTP header, so setcookie() must be called before any output is sent to the browser. So setcookie() method should appear before HTML tags

 

Syntax:-

setcookie(name, value, expire, path, domain, security);

 

name:- sets the name of the cookie

value:- sets the value of named variable

expire:- sets time in seconds after which cookie will be inaccessible

path:- specifies the directories for which the cookie is valid

domain:- specify the domain name in very large domains and must contain at least two periods to be valid

security:- this field contains value either 0 or 1. 1 indicates that the cookie should only be sent by secure transmission using HTTPS otherwise 0 indicates cookie can be sent by regular HTTP.

   
 

<?php

 

if( isset ( $_COOKIE ['newuser '])) // checking cookie is set or not ; if yes

 

header ('location : Secure .php '); // then redirecting to already login page

 

?> <html >

 

<head >

 

<title ></ title >

 

<meta http - equiv =" Content - Type " content =" text / html ; charset =UTF -8" -

 

 

</head > <body >

 

<form action =" Validate .php " method =" POST "> <table border ="1" align =" center ">

 

<tbody > <tr >

 

<td > User Name :</td >

 

<td >< input type =" text " name =" loginID " value ="" size = "20" placeholder =" Enter Login ID"/></td >

 

</tr > <tr >

 

<td > Password :</td >

 

<td >< input type =" password " name =" password " value ="" - size ="20" placeholder =" Enter password "/></td >

 

</tr> <tr >

 

<td >< input type =" submit " value =" Submit " name =" submit " /></td >

<td >< input type =" submit " value =" Reset " name =" reset " /></td >

 

</tr >

 

</tbody >

 

</table >

 

</form >

 

</body >

 

</html >

 

 

 

 

Cookie.php

 
 
 

<?php

 

echo "<H2 >";

 

echo " Login Successful <br ><br >";

 

echo " Welcome { $_COOKIE [' newuser ']}";

 

?> <html > <body >

 

<form action =" Logout .php " method =" POST "> <table border ="1" align =" center ">

 

<tr >

 

<td >< input type =" submit " value =" Submit " name =" Logout " /></td >

 

</tr > </tbody >

 

</table > </form >

 

</body > <html >

Secure.php

 

 

 

 

 

       
   

<?php

 

 

echo "<h2 >";

$user_db = array ( array ("ID"=>" clitus ","PWD"=>"123"),

array ("ID"=>" oliver ","PWD"=>" 1234 "),

array("ID"=>" manish ","PWD"=>"789"),

array("ID"=>" joyce ","PWD"=>" 123456 "));

 

$LoginID = $_POST ['loginID '];

 

$password = $_POST ['password '];

 

$flag = FALSE ;

 

foreach ( $user_db as $user )

 

{

 

if( $user ['ID ']== $LoginID && $user ['PWD '] == $password )

 

{

 

$flag = TRUE ; break ;

 

}

 

}

 

 

 

if( $flag )

 

 

{

 

 

 

 

setcookie ('newuser ', $LoginID , time () +50000) ;

 

header ('location : Secure .php ');

//

echo

" Login Successful .... <br > ";

//

echo

" Welcome

{ $_COOKIE [' newuser ']}";

}

 

 

 

else

 

 

 

{

 

 

 

 

echo " Login

Fail .... Try again ";

}

 

 

 

 

?>

 

?>

Validate.php

 
 
 

<?php

 

setcookie ('newuser ', 45, time () -50000) ;

 

header ('Location : Cookie .php ');

 

?>

Logout.php

 

 

 

 


LoginPage

 

 

CookieResult

 

 

Attachment: image001.jpg


How to handle cookies in PHP?

 

Cookies can be said as a small piece of information that is sent from a website.They are stored on the computer of client .The user loads the websitewhile the browser sends the cookie back to the server in order to signify  the website of the user's previous activity.

Syntax:

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

//Creating cookies

 

<?php

    setcookie("name","JoyFelice",time()+2500,"/","",0);

    setcookies("id","245",time()+2500,"/","",0);

?>

<html>

<head>

<title>Cookies in PHP</title>

</head>

<body>

<?php

echo "Setting COOKIES";
echo $_COOKIE["name"];
print_r($_COOKIE);

?>

</body>

</html>

 

 

 

Attachment: cookies.php
Was it useful?  




Please login to reply to this problem.