0

i want to access the cookies on sub domain,

the cookies working on main domain well but can't able to access the cookie on subdomain, i created two files testing_1.php and testing_2.php, the cookies is set through testing_1.php that is

$domain = '.localhost';
session_set_cookie_params(0, '/', $domain);
session_start();

$_SESSION['name'] = 'Jogesh';
setcookie("f_name", "jogesh", 0, "/", "$domain");

if( isset( $_COOKIE["f_name"] ) )
    echo "Set Cookie on 1: " . $_COOKIE["f_name"];
else
    var_dump(setcookie("f_name", "jogesh", 0, "/", "$domain"));

in the above code i also try to access session, but just got the session id, i want to access $_SESSION['name'] on subdomain if possible other wise cookies

now the testing_2.php is here

session_start();

if( isset( $_SESSION['name'] ) )
    echo "Session Name: " . $_SESSION['name'];
else
    echo "Session not set!";

if( isset( $_COOKIE["f_name"] ) )
    echo "Cookie Name: " . $_COOKIE["f_name"];
else
    echo "Cookie not set!";

the both files working well on localhost but when i am trying to access the file testing_2.php from demo.localhost the can't access anything plz let me know what mistake i had done,,

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65

1 Answers1

0

If you are creating them at www.example.com then you cannot read them from cdn.example.com. You have to create them at example.com. Also you cannot read PHP created cookies with JavaScript and visa-verse. It also depends on the browser. there were issues with tsk.tr. You have to see the browser's list http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1 Might me because the browser handles localhost and subdomain.localhost as complitly different domain names. Try the code in a real envirement or use hosts file.

ilhan
  • 8,700
  • 35
  • 117
  • 201
  • thanks, but what i used is wrong? cause i am testing on the localhost not on the main server, plz let me know thanks again 'EDIT:' i see that you said to use them on real environment, but why it is not working on localhost?? i mean the cookies – jogesh_pi Nov 25 '11 at 18:14
  • Browser's security measurements in order to prevent local fraud attacks I guess. Try 127.0.0.1 with demo.127.0.0.1 if it is possible. Or may be you should ask it to http://forums.mozillazine.org/ – ilhan Nov 25 '11 at 18:35