Use saved variable in cookie with php

Over the past two weeks, I have been working on storing the page id in cookies and then retrieving it on another page.

Finally, I solved it, but now I have another problem, I want to use this id (the one that I saved in the cookie and get it) in my php code.

I know that javascript is client side code and php is server side code, but I have to do this. Please help me with this.

This is my javascript code that works great, and I get the saved id with this line "+ value.favoriteid +"

<script> /* * Create cookie with name and value. * In your case the value will be a json array. */ function createCookie(name, value, days) { var expires = '', date = new Date(); if (days) { date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = '; expires=' + date.toGMTString(); } document.cookie = name + '=' + value + expires + '; path=/'; } /* * Read cookie by name. * In your case the return value will be a json array with list of pages saved. */ function readCookie(name) { var nameEQ = name + '=', allCookies = document.cookie.split(';'), i, cookie; for (i = 0; i < allCookies.length; i += 1) { cookie = allCookies[i]; while (cookie.charAt(0) === ' ') { cookie = cookie.substring(1, cookie.length); } if (cookie.indexOf(nameEQ) === 0) { return cookie.substring(nameEQ.length, cookie.length); } } return null; } function eraseCookie(name) { createCookie(name,"",-1); } var faves = new Array(); $(function(){ var favID; var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); var favID = (pair[0]=='ID' ? pair[1] :1) //alert(favID); } $(document.body).on('click','#addTofav',function(){ var fav = {'favoriteid':favID}; faves.push(fav); var stringified = JSON.stringify(faves); createCookie('favespages', stringified); location.reload(); }); var myfaves = JSON.parse(readCookie('favespages')); if(myfaves){ faves = myfaves; } else { faves = new Array(); } $.each(myfaves,function(index,value){ var element = '<li class="'+index+'"><h4>'+value.favoriteid+'</h4> '; $('#appendfavs').append(element); }); }); </script> 
+5
source share
2 answers

Read the cookie on the php side, this is easiest after you set them under js .

Any cookies sent to you from the client will be automatically included in the $ _COOKIE auto-global array if theorder variable contains "C". If you want to assign multiple values ​​to a single cookie, simply add [] to the cookie name. Regular PHP_ variables from cookies can be created depending on register_globals.

Here php are some examples:

 <?php echo $_COOKIE["your cookie name"]; ?> <?php print_r($_COOKIE); ?> 

Relying on them is not recommended, as this feature is often disabled for security reasons.

http://php.net/manual/en/features.cookies.php

+2
source

If you already managed to save the cookie in javascript, then there is no problem extracting it in PHP, just use $_COOKIE["COKKIE_NAME"] (where you, of course, change COOKIE_NAME to the name of the cookie you saved in JS) ..

See http://php.net/manual/en/features.cookies.php for more examples.

+1
source

All Articles