简体   繁体   English

在cookie中查找并​​保存变量

[英]find and save variable in cookie

I am trying to save variable in cookie and then retrieve it, so far i can save just a text and page url and then retrieve it where ever i want. 我试图将变量保存在cookie中然后检索它,到目前为止我只能保存一个文本和页面URL,然后在任何我想要的地方检索它。

Could u please help me out with it?for example in this code i want to save $room and $renthome values in cookie. 你可以帮我解决一下吗?例如在这段代码中我想在cookie中保存$ room和$ renthome值。

this is my current code 这是我目前的代码

 <!DOCTYPE html> <html> <head> <title>New page string name</title> <link rel="stylesheet" href="css/reset.css"> <!-- Resource style --> <link rel="stylesheet" type="text/css" href="css/buy-rent.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <script> 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; } /* * Erase cookie with name. * You can also erase/delete the cookie with name. */ function eraseCookie(name) { createCookie(name, '', -1); } var faves = new Array(); function isAlready(){ var is = false; $.each(faves,function(index,value){ if(this.url == window.location.href){ console.log(index); faves.splice(index,1); is = true; } }); return is; } $(function(){ var url = window.location.href; // current page url $(document.body).on('click','#addTofav',function(e){ e.preventDefault(); var pageTitle = $(document).find("title").text(); if(isAlready()){ } else { var fav = {'title':pageTitle,'url':url}; 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(); } }); </script> </head> <body> <a href="javascript:void(0);" id="addTofav">Add me to fav</a> <div class="img"> <div class="desc"> <ul id="appendfavs"> </ul> </div> </div> <?php error_reporting(0); include("config.php"); $quer= "SELECT*FROM ".$db_table." "; $query=mysqli_query($connect,$quer) or die(mysqli_error()); ?> <?php while($row = mysqli_fetch_array($query)): $idhome= $row['idhome']; $room=$row['room']; $renthome=$row['renthome']; $pricehome=$row['pricehome']; ?> <?php endwhile;?> </body> </html> 

I use jQuery Cookie 我使用jQuery Cookie

https://github.com/carhartl/jquery-cookie https://github.com/carhartl/jquery-cookie

usage: $.cookie('name', 'value'); 用法: $.cookie('name', 'value');

retrieving: $.cookie('name'); // => "value" 检索: $.cookie('name'); // => "value" $.cookie('name'); // => "value"

In order to save complex data in a cookie, do the following: 要在cookie中保存复杂数据,请执行以下操作:

1) save the whole data in an object. 1)将整个数据保存在对象中。 Something like var cookieData = {}; var cookieData = {}; , and then you can add properites like cookieData.date = yourDate; cookieData.myUrl=yourUrl; ,然后你可以添加像cookieData.date = yourDate; cookieData.myUrl=yourUrl;这样的cookieData.date = yourDate; cookieData.myUrl=yourUrl; cookieData.date = yourDate; cookieData.myUrl=yourUrl;

2) transfer the object to a sting using JSON.stringify 2)使用JSON.stringify将对象转移到sting

3) save the string in the cookie. 3)将字符串保存在cookie中。

Now, in order to use it again - 现在,为了再次使用它 -

1) retrieve the cookie (See the code above) 1)检索cookie(参见上面的代码)

2) Turn the string back into an object using JSON.parse 2)使用JSON.parse将字符串转回对象

3) Fetch the data you desire from the object 3)从对象中获取所需的数据

In jQuery, you should use jquery.cookie plugin. 在jQuery中,你应该使用jquery.cookie插件。

This creates a cookie 这会创建一个cookie

$.cookie(name, value [, options]);

and then retrieve it value 然后检索它的值

$.cookie(name);

You can generate in PHP <script> block which will save the variable. 您可以在PHP <script>块中生成将保存变量的块。 Example show saving cookie named room by $room value. 示例显示通过$room值保存名为room的cookie。

<script>
     createCookie("room", "<?= $row['room'] ?>", 2);
</script>

another (and I think better) way is to set the cookie by PHP. 另一种(我认为更好)的方法是通过PHP设置cookie。

<?php setcookie("room", $row['room'], 2 * 24 * 60 * 60 * 1000); ?>

Don't forget that if you want to set cookie by PHP yhou have to do it before send any output. 不要忘记,如果你想通过PHP设置cookie,你必须在发送任何输出之前做到这一点。 In your case you have to prepare output, set cookie and then send the output. 在您的情况下,您必须准备输出,设置cookie,然后发送输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM