Working with Cookies with php

Cookies are used to store data on the remote browser at the user side, this is usefull for example for saving username or userid so each time the user browse to URL the php will ask from the remote browsers its cookies then it will be able to gather the userid.

here is an example

<?php // Uniq Visitor

// Check if remote browser have visitor id cookie on the browser

if (!isset($_COOKIE['vistid']))

{

// No the user does not have visitor ID we will give him an allocate for vistor ID

$count = file_get_contents("count.txt");

$count = trim($count); $count = $count + 1;

$fl = fopen("count.txt","w+");

fwrite($fl,$count); fclose($fl);

// Sending a cookie to the remote browser

setcookie('vistid',$count,time()+3600*24*365);

echo "<!– New Visitor: id=" . $count . "–>";

}

else

{

// User have visitor ID we will just print it

echo "<!– vistor id:"  .  $_COOKIE['vistid'] . " –>";

} // End of visitors

?>

 

תגים: , ,

הפוסט הזה סגור לתגובות כרגע.