Ad

PHP Cookies


What is a Cookie 

Cookies are frequently used to help identify users. A cookie is a little file that is embedded on the user's computer by the server. The cookie will be sent each time a browser request is made by the same machine. Cookie values may be created and retrieved using PHP.

Client-side storage is used for PHP cookies. Sessions are more secure than PHP cookies.Please try to use sessions rather than cookies as much as you can.
A PHP cookie is a little data file that is kept on the client browser. It is employed in

acknowledge the person.Cookies are stored in the client browser after being generated on the server. Three steps are involved in the cookie to user info process. The browser receives a collection of cookies from the PHP Server script, such as name, age, address, or phone number, and so forth.This data is stored by the browser locally on the computer for further use.The browser transmits the cookies the next time it makes a request to the web server. information to the server, which then utilizes it to determine 


Create Cookies with PHP

Cookie is created with the setcookie() function.

   Syntax

setcooki(name, value, expire, path, domain, secure, httponly);


1] Name :- set the name of the cookie and is stored in enviroment variable called                        HTTP_COOKIE_VARS. variable  use accessing cookies.

2] Value :- set the value name variable and content store.

3] Expiry :- future  time in second 00:00:00 GMT 7st Jul 2024

       parameter is not set cookie. cookie automatically expire web Browser is closed.

4] Path :- specifies directories which cookie a valid. single forward slash character                permits cookie be valid all direction.

5] Domain :- use to specify the domain name. all cookies only valid host and domain created 

6] Secure :- cookie should only sent secure transmission using HTTPS.  cookie sent by      regular HTTP.

Example :-


<?php
setcookie("user", "geeta");
?>
<html>
    <body>
        <?php
           if(!isset($_COOKIE["user"]))
           {
            echo "cookie not found";
           }
            else{
           echo " cookie :- ".$_COOKIE["user"];
           }
        ?>
</body>
    </html>

Output :- 






2] Retrieve a Cookie

$cookie super global variable .

Example:-

Return cookie value:-

$value=$COOKIE["cookiename"];


 3]Delete a Cookie

delete or remove cookie. expire the cookie. 

Example :-

 
<?php
setcookie("cookiename", "", time()-3600);
?>


Related Post :-


html heading tag

php program print diamond number pattern

php program using sum of digits

No comments

Powered by Blogger.