Posted on: 20.08.2020 Posted by: Alex D Comments: 0

How to set and get cookies in Java Script and PHP.

Set cookies using PHP.

Use the setcookie(“name_cookie”,”value”) function to set a cookie to the client’s browser.

Important! You should write this function at the beginning of the PHP file. Immediately after opening the <?php.

<?php
setcookie("city", "New York");
//setcookie() must be the first line in the file .php
//Code PHP ......
?>

Set the cookie lifetime to 1000 seconds.

setcookie('cookie_name', 'cookie_vlue', time() + 1000);

Set the cookie lifetime to 30 days.

setcookie('cookie_name', 'cookie_vlue', time() + (3600 * 24 * 30));

Attention! You should write function setcookie() at the beginning of the file. Otherwise setcookie() will not work correctly.

Get cookies using PHP.

In order to get cookies from the client’s browser, use $_COOKIE[“name_cookie“].

<?php
//Code PHP ......
echo "City of cookie PHP: " . $_COOKIE["city"];
?>

Delete php cookie.

<?php
setcookie("name_cookie","",time()-10000, '/');
//setcookie() must be the first line in the file .php
?>

Setcookie should be the very first in the .php file.

Sample code.

<?php
setcookie("city", "New York");
echo '<P>Save "city" used function php setcookie("name","value"). OK.</P>';
//Other code PHP ......
echo '<P>Now get cookies using the function COOKIE[]!</P>';
if (isset($_COOKIE['city'])) {
  echo "<P>Result: <B>City of cookie PHP: " . $_COOKIE["city"] . "</B></P>";
} else {
  echo "<P>The cookie was installed before the echo statement. Please reload the page.</P>";
}
?>

DEMO

Question: Is it possible to limit the lifetime of data when stored in localstorage?

Answer: No. There are no standard options for saving the data lifetime in local localstorage.

Set cookies using Java Script.

This Java Script function localStorage.setItem() sets a cookie to the browser of the site visitor.

localStorage.setItem("name_cookie","value_cookie");
var set_lang = "English";
localStorage.setItem("language", set_lang);

Get cookies using Java Script.

You can get cookies from the website visitor’s browser through the localStorage.getItem() function on the Java Script.

localStorage.getItem("name_cookie");
var lang = localStorage.getItem("language");

Delete cookies.

Delete cookies by name.

localStorage.setItem("name_cookie","");

Delete all cookie.

storage.clear();

Sample code.

<SCRIPT>
var set_lang = "English";
localStorage.setItem("language", set_lang);
function Cookie_Language() {
  var lang = localStorage.getItem("language");
  alert(lang);
}
</SCRIPT>
<P><BUTTON onClick="Cookie_Language();">Get Language</BUTTON></P>

DEMO

Old version of installing and receiving cookies in JS.

The old standard for creating cookies uses the syntax document.cookie. This standard works in modern browsers now. But there are limits on the size of cookies..

document.cookie = 'user=John Smith';
document.cookie = "user=John; domain=site.com"
document.cookie = 'name=Вася; path=/; domain=.site.ru; expires=+100';
//Cookies "user" are only available via https.
document.cookie = "user=John; secure";

Getting a cookie by her name.

This command will show all cookies saved in the browser.

alert( document.cookie ); // cookie1=value1; cookie2=value2;...

In order to receive cookies after document.cookie, you must use a match() and regular expression.

document.cookie = 'age=25';
var age = document.cookie.match(/age=(.+?);/);
alert(age[1]); //input 25

Delete all cookies

document.cookie = 'name=; path=/; expires=-1';
Categories:

Leave a Comment