tags:

views:

37

answers:

2

Hello All,

I am setting a cookie in a PHP script which also has some html , the first line is setcookie function as required.

<?php 
setcookie("user", "xyz", time()+3600);
?>
<html>
------ some html tags 
</html>

In other PHP script where am trying to access , i get a blank value . $user=$_COOKIE["user"];

Can anyone tell me why is this happening ?

+2  A: 

Your browser has cookies enabled, right?

What does var_dump( $_COOKIE['user'] ); print exactly? NULL, false?

meder
Yes cookie is enabled. Infact the value is set , I can see it in the option/privacy tab in firefox . But the value is not acessible.var_dump( $_COOKIE['user'] ); gives me NULL.
mithunmo
can you paste more code? you're not showing the whole script. this is one file, two files? on the same server, not subdomains right?
meder
Ok . I am setting the cookie at http://mydomain/home.php and i am trying to access it on http://mydomain/cl/base/test.php . Is this possible ?
mithunmo
Try specifying more arguments..);setcookie ("user", "xyz", time() + 3600, "/", ".mydomain.com");
meder
+1  A: 

It is not a good practice to store sensitive information in cookies like this. Cookies may be edited by the user, so here he could set his "user" cookie to get being recognized as any user he wants. Its a better practice to use sessions instead. With sessions you can safely and easily bind any sensitive information to this specific user using the $_SESSION superglobal.

Havenard
I was using sessions .But I had some problem (http://stackoverflow.com/questions/1316054/slow-response-to-database-write-from-php)Thats why i am using cookie now.
mithunmo
I have an answer for this other question, I'll be writing it right now.
Havenard
http://stackoverflow.com/questions/1316054/slow-response-to-database-write-from-php/1317534#1317534
Havenard