There are two methods to process cookies: Server (php, asp, etc.) and client (javascript ). in this tutorial, we will learn how to create cookies using php and javascript.
There are two methods to process cookies: Server (php, asp, etc.) and client (javascript ). in this tutorial, we will learn how to create cookies using php and javascript.
HTTP is a stateless protocol, which means that every request to the website is independent, and therefore data cannot be stored by itself. But this simplicity is also one of the reasons it spread widely in the early days of the Internet.
However, there is still a way for you to store information between requests in the form of cookies. This method enables you to manage sessions and maintain data more efficiently.
There are two methods to process cookies: Server (php, asp, etc.) and client (javascript ). in this tutorial, we will learn how to create cookies in php and javascript.
Cookies and php
Setting cookies
To create a cookie in php, you need to use the setcookie method. It requires some parameters (except the first parameter is required, other parameters are optional)
The Code is as follows:
Setcookie (
'Pagevisits ', // cookie name, required
$ Visited, // cookie value
Time () + 7*24*60*60, // expiration time, set to a week
'/', // The available file path of the cookie
'Demo .tutorialine.com '// domain name bound to the cookie
)
If the expiration time is set to 0 (the default value is 0), the cookie will be lost when the browser restarts.
The '/' parameter indicates that all file paths under your domain name can be used as cookies (you can also set a single file path for it, for example, '/admin /').
You can also pass this function two additional parameters, website space, which is not provided here. They are specified as boolean type.
The first parameter indicates that the cookie runs only on a secure HTTPS connection, and the second parameter indicates that the cookie cannot be operated using javascript.
For most people, you only need the fourth parameter, and the rest will be ignored.
Reading cookies
Using php to read cookies is much easier. All the cookies passed to the script are in the $ _ COOKIE super global array.
In our example, we can use the following code to read cookies:
The Code is as follows:
$ Visits = (int) $ _ COOKIE ['pagevisits '] + 1;
Echo "You visited this site:". $ visits. "times ";
It is worth noting that when the next page is loaded, you can use $ _ COOKIE to obtain the cookies you set using the setcookie method,
You should be aware of something.
Cookies ing cookies
To delete cookies, you only need to use the setcookie function to set an expiration date for cookies.
The Code is as follows:
Setcookie (
'Pagevisits ',
$ Visited,
Time ()-7*24*60*60, // set to the previous week. The Hong Kong server is rented and the cookie will be deleted.
'/',
'Demo .tutorialine.com'
)
Cookies and jQuery
When using cookies in jquery and server space, you need a plug-in Cookie plugin.
Setting cookies
Setting cookies with cookies plug-in is intuitive:
The Code is as follows:
$ (Document). ready (function (){
// Setting a kittens cookie, it will be lost on browser restart:
$. Cookie ("kittens", "Seven Kittens ");
// Setting demoCookie (as seen in the demonstration ):
$. Cookie ("demoCookie", text, {expires: 7, path: '/', domain: 'demo .tutorialine.com '});
// "Text" is a variable holding the string to be saved
});
Reading cookies
To read a cookie, you only need to call the $. cookie () method and give it a cookie-name. This method will return the cookie value:
The Code is as follows:
$ (Document). ready (function (){
// Getting the kittens cookie:
Var str = $. cookie ("kittens ");
// Str now contains "Seven Kittens"
});
Cookies ing cookies
To delete a cookie, you only need to set the second parameter to null in the $. cookie () method.
The Code is as follows:
$ (Document). ready (function (){
// Deleting the kittens cookie:
Var str = $. cookie ("kittens", null );
// No more kittens
});
Complete example:
Demo. php
The Code is as follows:
// Always set cookies before any data or HTML are printed to the page
$ Visited = (int) $ _ COOKIE ['pagevisits '] + 1;
Setcookie ('pagevisits ', // Name of the cookie, required
$ Visited, // The value of the cookie
Time () + 7*24*60*60, // Expiration time, set for a week in the future
'/', // Folder path, the cookie will be available for all scripts in every folder of the site
'Demo .tutorialine.com '); // Domain to which the cookie will be locked
?>
MicroTut: Getting And Setting Cookies With jQuery & PHP | tutorialine demo
MicroTut: Getting And Setting Cookies With jQuery & PHP
Go Back To The Tutorial»
The number above indicates how many times you 've visited this page (PHP cookie). Reload to test.
Write some text in the field above and click Save. It will be saved between page reloads with a jQuery cookie.
Jquery. cookie. js
The Code is as follows: