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

时间:2012-09-12 05:11:16

标签: php javascript html

我遇到输出问题,我似乎无法追踪问题,这里是代码:

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

<?php echo implode($_COOKIE)?>

并显示

713qnihjmdt7mdq8eejvlcd1q1

但我想显示存储在速度变量中的日期

我尝试直接调度节奏变量,并显示正确的输出,

有什么建议吗?我想我需要在php端添加代码。

3 个答案:

答案 0 :(得分:4)

我刚刚更改了以下内容

document.cookie='fcookie='+tempo; 

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

答案 1 :(得分:3)

你的脚本有几个错误,我修改了它们并添加了一些额外的代码,希望这对你有用

<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";
    ?>

有关 Javscript cookies Php Cookies

的更多信息

答案 2 :(得分:1)

首先,您看到的$ _COOKIE是PHPSESSID cookie ...您没有查看JS cookie。本文详细介绍了PHP和JS cookie之间的关系:http://www.quirksmode.org/js/cookies.html