Friday 19 April 2013

PHP Cookies


PHP Cookies.
Cookies basic used to recognize users who visit your site.
What's Cookies?
Cookies are often used to identify users. Cookies are small files that the server embeds on the user's computer. So every time the computer is running requests to the same web page then the cookies will be sent to the computer user. With PHP, you can take and make cookies value.

How is that how to make Cookies?
Test cookie function () is used to make or take a cookie value and function setcookie () must be before tag <html> better put on top.
Syntax:
setcookie (name, value, expires, patch, domain);
Example:
In the example below we will create a cookie with the user name and the value our test and will also specify that the cookie will expire after 1hr.
<? Php
setcookie ('user', 'test', time () +3600);
?>
In the example below we will create a cookie and cookie set time limit in different ways.
$ Expire = time () +60 * 60 * 24 * 30;
setcookie ('user', 'test', $ expire);

Note: In the above example in the specified time period expires for 1 month (60 seconds * 60 minutes * 24 hours * 30 days).
How is that a way to show the value of cookies?
Fariabale $ _COOKIE medapat value in use for cookies.
In the example below we will show the cookies with the user name.
<? Php
/ / Displays cookies
echo $ _COOKIE ['user'];
/ / Displays semuan cookies
print_r ($ _COOKIE);
?>

Like where do I delete cookies?
When you have to delete the cookies are absolutely sure you reduce Nili expire correctly.
example deleting cookies
<? Php
setcookie ('user', 'test', time () -3600);
?>

What should you do if it turns out that in gunakn browser does not suport with cookies?
Another way to transmit information than you can use it only menggukan cookies <form>
<html>
<body>
The source code is method="POST" action="welcome.php">
name: <input type="text" name="nama" /> <br />
pass: <input type="text" name="pass" /> <br />
<input type="submit" value="Login" />
</ Form>
</ Body>
</ Html>

welcome.php file
<? Php
echo 'User name:'. $ _POST ['name']. '<br />';
echo 'You have successfully logged in';
?>

No comments:

Post a Comment