articles

Home / DeveloperSection / Articles / PHP Cookies

PHP Cookies

Anonymous User6794 14-Sep-2011

Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. I.e. a cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. Cookies are part of the HTTP header, so cookies must be called before any output is sent to the browser. This is the same limitation that HTTP header has.

With PHP, you can both create and retrieve cookie values.

Creating a Cookie:

The setcookie() function is used to create a cookie in PHP. The setcookie() function must appear before html tag.

Now let’s we have an example, how to create cookie in PHP.

Note: The value of the cookie is automatically URL encoded when sending the cookie, and automatically decoded when received. To prevent URL encoding, use setrawcookie()function instead setcookie() function.

Syntax:

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

Here, $name specifies the name of cookie, $value specifies the value of cookie, $expire specifies the cookie expiration time and $path specifies the creating path of cookie. The ‘/’ sign is used for creating cookie in current domain.

Example:
<!DOCTYPE html>
<?php
// Creating cookie
   setcookie("TempCookie", "PHPCookie", time()+1*24*60*60,'/') ;
 ?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title></title>
    </head>
    <body>
        <p>Testing of Cookie Creating </p>
        </br>
        <h3><p>Now retrieve the cookie value </p></h3>
        </br>
        <?php
        // Retrieve the cookievaluewith $_COOKIE[] function
            echo 'Cookie value is : '.$_COOKIE["TempCookie"];
        ?>
    </body>
</html>

 

In the example above the expiration time is set to a day (60 sec * 60 min * 24 hours * 1 day).

Note: $_COOKIE[“cookieName”] function is used for retrieving cookie value.

Output:

PHP Cookies

In the following example we use the isset() function to find out  a cookie has been set or not.

<!DOCTYPE html>
<?php
// Creating cookie
   setcookie("UserTempCookie", "UserCookie", time()+1*24*60*60) ;
  
 ?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>Cookie Test</title>
    </head>
    <body>
        <p>Testing of Cookie Creating </p>
        </br>
        <h3><p>Is Cookie value set?  </p></h3>
        </br>
        <?php
            // check, is cookie set or not
       
        if(isset ($_COOKIE["UserTempCookie"]))
            echo 'Answer is : yes'.' '.'and cookie value is :'.$_COOKIE["UserTempCookie"];
        else
            echo 'Answer : No';
        ?>
    </body>
</html>

Output:

PHP Cookies

Deleting Cookie:

When deleting a cookie you should assure that the expiration date is in the past.

Let’s we have an example, how to delete cookie in PHP.

Example:
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>Cookie Test</title>
    </head>
    <body>
        <?php
             setcookie("UserTempCookie","",time()-3600);
             echo 'Cookie deleted successfully';
        ?>
    </body>
</html>
Output:

PHP Cookies



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By