By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

Reading a cookie set in javascript with php

Khaiya
P: 4
I'm trying to read a cookie that I set with javascript with php. I've tried many websites and nothing I've found works at ALL (except var_dump() printed up null) but other then that all I've gotten is a blank page.

Here is what I've tried...


(page1.php)
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. createCookie('name','jim',7)
  3.  
  4.  
  5. function createCookie(name,value,days) {
  6.     if (days) {
  7.         var date = new Date();
  8.         date.setTime(date.getTime()+(days*24*60*60*1000));
  9.         var expires = "; expires="+date.toGMTString();
  10.     }
  11.     else var expires = "";
  12.     document.cookie = name+"="+value+expires+"; path=/";
  13. }
  14. </script>
  15.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

(page2.php)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $var1=$_COOKIE['name'];
  4.  
  5. echo $var1;
  6.  
  7.  
  8. ?>
  9.  
I've also tried...
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $var1=var_dump($_COOKIE['name']);
  4.  
  5. echo $var1;
  6.  
  7.  
  8. ?>
  9.  
but so far nothing has happened.
May 29 '07 #1
Share this Question
Share on Google+
2 Replies


pbmods
Expert 5K+
P: 5,821
Expand|Select|Wrap|Line Numbers
  1. $var1=var_dump($_COOKIE['name']);
var_dump doesn't return anything. Instead, try this:

Expand|Select|Wrap|Line Numbers
  1. var_dump($_COOKIE);
  2.  
Check to make sure the domain and expiration date for your cookies are valid.
May 29 '07 #2

Khaiya
P: 4
var_dump doesn't return anything. Instead, try this:

Expand|Select|Wrap|Line Numbers
  1. var_dump($_COOKIE);
  2.  
Check to make sure the domain and expiration date for your cookies are valid.

I've tried that now and it didn't work either, and I guess the main problem is that I only sort of know the domain and expiration date of my cookies, and I don't really understand what var_dump() even does...

I know that a cookie automatically attaches the domain that it was saved from to the cookie but when I call the cookie how do I specify the domain name it came from?
May 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.