1

i am having an output problem and i can't seem to trace the problem, here is the code:

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)?>

and it displays

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.

Patrick Narcelles
  • 169
  • 2
  • 3
  • 12

3 Answers3

5

i just changed the following

document.cookie='fcookie='+tempo; 

and

if (isset($_COOKIE["fcookie"])) 
echo $_COOKIE["fcookie"]; 
else 
echo "Cookie Not Set";
Patrick Narcelles
  • 169
  • 2
  • 3
  • 12
3

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

Sibu
  • 4,609
  • 2
  • 26
  • 38
  • both of my scripts are on different files, it gives me a Notice: Undefined index: mycookie – Patrick Narcelles Sep 12 '12 at 05:31
  • @PatrickNarcelles I don't think that will a issue, because if a cookie is set you can access it from different file. – Sibu Sep 12 '12 at 05:34
  • 1
    @PatrickNarcelles that's because your cookie was not set, check this page http://javascript.about.com/library/blwcookie.htm , it explains creating cookie setting domain path and cookie expiry date, also i have edited my answer. – Sibu Sep 12 '12 at 05:47
  • now it goes to the else part, it displays Cookie Not Set, i tried your suggestion, which is to add fcookie='mycookie'; – Patrick Narcelles Sep 12 '12 at 05:48
  • thanks for the tips sir, i just changed the following document.cookie='fcookie='+tempo; and if (isset($_COOKIE["fcookie"])) echo $_COOKIE["fcookie"]; else echo "Cookie Not Set"; – Patrick Narcelles Sep 12 '12 at 05:58
1

First of all, the $_COOKIE you are seeing is the PHPSESSID cookie... You are not viewing the JS cookies. This article has good info on the relationship between PHP and JS cookies: http://www.quirksmode.org/js/cookies.html

christopher
  • 615
  • 5
  • 11