Cookies
Cookies are a mechanism to store the data at the client or browser’s machine. These cookies are stored as a small files. When a browser send a request to web
server, it sends the cookies along with it.
On the server, Cookies can accessed using the $_COOKIE array. This array is an asscotive array where the contents are stored as name, value pairs. The name
of the cookie is set by the setcookie function when it is creating the cookie as follows:
-
-
<?php
-
-
-
?>
-
The above script creates a cookie at the client machine with the name “example_cookie” and value as “HI”.
Counting user Hits
Counting how many times the user visted our sites using cookies is simple.First if you look at the script that do this, then you can understand easily:
-
-
<?PHP
-
-
{
-
-
echo " Hi, You are Most welcome to our web site";
-
}
-
else
-
{
-
-
$_COOKIE[‘user_hits’] = $_COOKIE[‘user_hits’] + 1;
-
-
-
}
-
-
?>
-
We save the count of user hits in a cookie on the client machine and the cookie is namedĀ as user_hits ( as shown above).
When the script executes for the first time, the cookie (named user_hits) is not set at the client machine. So, the isset function in the ‘if’ statment
returns false, ,so, we are setting a new cookie at the client machine using the function called setcookie.
when the user visits second time, the if statement got failed to execute becuase, the cookie exists at tyhe client machine and else part executes. In the
else part, increating the value of cookie.
Hi, this is Shiva Kumar. You can call me Shiva. I'm a developer like you. Earlier I used to write articles for Computers Today. Now I started this web site to help each other ( or ) atleast contribute some thing from my end. I'm always a student, if you need any articles or help which is not there in this web site, please