html5andphp: Cookies

Monday, 5 August 2013

Cookies

Cookies


A cookie is often used to identify a user. Cookies are text files stored on the client computer and they are kept of use tracking purpose.

Each time the same computer requests a page with a browser, it will send the cookie too. PHP transparently supports HTTP cookies.

A cookie is a small file that the server embeds on the user's computer.
Server script sends a set of cookies to the browser.
such as user name ,password etc.
Browser stores this information on local machine for future use.

How to Create a Cookie?



** The setcookie() function must appear before the <html> tag.
setcookie(name, value, expire, path, domain, security);
 There are six arguments explain in detail below.

Name - This sets the name of the cookie and is stored in an environment variable called HTTP_COOKIE_VARS. This variable is used while accessing cookies.

Value -This sets the value of the named variable and is the content that you actually want to store.

Expiry – This specify a future time in seconds since 00:00:00 GMT on 1st Jan 1970. After this time cookie will become inaccessible. If this parameter is not set then cookie will automatically expire when the Web Browser is closed.

Path -This specifies the directories for which the cookie is valid. A single forward slash character permits the cookie to be valid for all directories.

Domain - This can be used to specify the domain name in very large domains and must contain at least two periods to be valid. All cookies are only valid for the host and domain which created them.

Security – This can be set to 1 to specify that the cookie should only be sent by secure transmission using HTTPS otherwise set to 0 which mean cookie can be sent by regular HTTP.
Syntax
setcookie(name, value, expire, path, domain,security);

Example
Generaly we use three orgument because of other three orgument used in web servers not in local machines.
<?php
setcookie("cookies_name", "umar farooque khan", time()+3600);
?>
<html>
<head>
.............
.............
</html>

time() used for current time and +3600 second  are used for cookies present in one hour in local machine.
It is always in second.
It is not show in browser .

How to Retrieve a Cookie Value?



The PHP $_COOKIE variable is used to retrieve a cookie value.
In the example below, we retrieve the value of the cookie named "cookies_name" and display it on a page:
<?php
echo $_COOKIE["cookies_name"];  // Print single cookie
print_r($_COOKIE);   // A way to view all cookies
?>



How to Delete a Cookie

Cookies can also be deleted. This is useful for situations such as when a user logs out of your site. To delete a cookie, call the setcookie() function again with the same name,
 folder and domain that you used earlier to set the cookie. However, instead of an expiry date set in the future,
 this time give an expiry date some time in the past.
<?php
setcookie( "cookies_name"," ", "time()-3600"  );
?>

Download PHP PDF php basic tutorial pdf



Example of cookies using form

Simple form and set cookies.
Both code  are save in different file and excute .

<?php
error_reporting(0);  //set cookies
$name=$_POST['name'];
$pass=$_POST['pass'];
$expire=time()+24*60;
setcookie("uk1", $name, $expire);
setcookie("uk2", $pass, $expire);
setcookie("a","www.umar.com","time()-24*60");
?>



<html>
<center><br><br><br><br>
<form action="cookies.php" method="post">
<table width="2" border="2" cellspacing="2" cellpadding="2" bgcolor="#00FFFF">
  <tr>
    <td>Name:</td>
    <td><input name="name" type="text"></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><input name="pass" type="password"></td>
  </tr>
</table>
<input name="" type="submit">
</form>

</center>
</html>

Output

cookies







Retrieve a Cookie Value

<?php
error_reporting(0);  //set cookies
echo $_COOKIE["uk1"];
echo"<br>";
echo $_COOKIE["uk2"];

?>

For more on php visit
php tutorial



Output

cookies

No comments: