简体   繁体   English

在javascript中设置cookie值并使用php显示它

[英]set cookie value in javascript and displaying it with php

i am having an output problem and i can't seem to trace the problem, here is the code: 我遇到输出问题,我似乎无法追踪问题,这是代码:

sample.js sample.js

var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var cmonth = myDate.getMonth();
var cdate = myDate.getDate();
var temp1 = m_names[cmonth];
var tempo = escape(temp1 + " " + cdate);
document.cookie=fcookie"=" + tempo;

output.php output.php

<?php echo implode($_COOKIE)?>

and it displays 它显示

713qnihjmdt7mdq8eejvlcd1q1 713qnihjmdt7mdq8eejvlcd1q1

but i want to display the date stored in the tempo variable, 但我想显示存储在速度变量中的日期,

i tried dispaying the tempo variabe directly and it dispalyed the right output, 我尝试直接付清拍子节奏,但它却显示了正确的输出,

any suggestions? 有什么建议么? i think i need to add a code in the php side. 我想我需要在php端添加代码。

i just changed the following 我只是改变了以下

document.cookie='fcookie='+tempo; 

and

if (isset($_COOKIE["fcookie"])) 
echo $_COOKIE["fcookie"]; 
else 
echo "Cookie Not Set";

Your script has couple of mistakes, I have modified them and added some extra codes, Hope this works for you 您的脚本有几个错误,我已对其进行了修改并添加了一些额外的代码,希望这对您有用

<script>
    fcookie='mycookie';
    var monthname = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var myDate=new Date();//--->getting today's date
    var cmonth = myDate.getMonth();
    var cdate = myDate.getDate();
    var temp1 = monthname[cmonth];
    var tempo = escape(temp1 + " " + cdate); 
    document.cookie=fcookie+"=" + tempo;//-->missing cookie name and concatenation
    </script>
    <?php
    if (isset($_COOKIE["mycookie"]))
      echo $_COOKIE["mycookie"];
    else
       echo "Cookie Not Set";
    ?>

More about Javscript cookies and Php Cookies 有关Javscript cookiePhp Cookies的更多信息

First of all, the $_COOKIE you are seeing is the PHPSESSID cookie... You are not viewing the JS cookies. 首先,您看到的$ _COOKIE是PHPSESSID cookie ...您没有在查看JS cookie。 This article has good info on the relationship between PHP and JS cookies: http://www.quirksmode.org/js/cookies.html 本文对PHP和JS cookie之间的关系提供了很好的信息: http : //www.quirksmode.org/js/cookies.html

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

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