1

Problem: cookies are fine in local host but not in server, can be accessed but value seems to be random letters

I am storing the image file name as cookie like this

//storing image filepath as cookies
if (isset($_COOKIE['imgName'])) {
  setcookie("imgName", "", time()-3600);
}
$expire=time()+60*60*24*30;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
$cookie = $_FILES["file"]["name"];
setcookie("imgName", $cookie, $expire,'/',$domain,false);

and in the local host the cookie is set fine as eg: hp_1.jpg. but when i upload it to the server the cookie is set as complete "random" letters eg: jhSerZR6i1T952C3bk7vEOGCj8Pz_tBYtuHcgrgj81A Am I missing something?

edit: i removed the / domain etc from the cookie and set it to the simplest form of setcookie('imgName',$photoName,time()+3600); and I still get the same random letters. is there anything that might encrypt the cookie on the live server but not bother on the localhost??

lattest edit: so after a lot of testing i noticed that javascript can get a php cookie but somehow encrypts? it or something (encrypt seems wrong because its always the same string for a certain string eg: aaa will always generate qwe (or whatever)) i fixed this by completely removing cookies and using php echo to get the value i want. =[

hahaha
  • 1,001
  • 1
  • 16
  • 32

1 Answers1

0

I'm unable to understand the sense of this line

$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;

You can directly do this :

setcookie('imgName', $_FILES['file']['name'], time()+3600, '/', $_SERVER['HTTP_HOST'], false);

( I tried your code online, that is returning NULL while this is return the right cookie name )

EDIT (for example) :

PHP CODE :

if(isset($_POST['submit']))
{
$cookie = $_FILES['img']['name'];
setcookie('myCookie',$cookie,time()+3600,'/');
foreach ($_COOKIE as $key=>$val)
{
echo $key.' is '.$val."<br>\n";
}
}

HTML CODE :

<form action="#" method="post" enctype="multipart/form-data">
<input type='file' name="img">
<input type='submit' name="submit" value="GO">
</form>
Prateek
  • 368
  • 3
  • 16
  • my code WORKED offline on localhost, yours does not work on localhost or on server – hahaha May 06 '14 at 06:55
  • i don't know what is happening with your code, but i'm sure this is "domain" issue. Can you try without domain parameter? like this `setcookie('imgName',$_FILES['file']['name'],time()+3600,'/');` because it will automatically set cookie on your parent domain. Check and post result. – Prateek May 06 '14 at 07:22
  • still same random letters :(, it occurred to me could something encrypt it when its on a live server instead of a localhost?? – hahaha May 06 '14 at 07:28
  • i've added a simple example which is working fine for me on online server. Check that. – Prateek May 06 '14 at 07:48
  • your example works fine offline on localhost but generates the same random numbers on the server – hahaha May 06 '14 at 08:00
  • i just noticed something, the echo text is fine but the retrieved cookie from javascript is not, i am using the jquery.cookies, might be doing something to it?? altough on localhost is working perfectly – hahaha May 06 '14 at 08:05
  • are you using jquery cookie plugin? if yes, are you sure that you are writing right name? for above example : `alert($.cookie('myCookie'));` – Prateek May 06 '14 at 08:12
  • yess it works perfectly fine on localhost! why do i have the feeling you are skipping this line :) – hahaha May 06 '14 at 08:15
  • when i php echo it is fine when i alert(it) its random letters, even with a custom getCookie(cookieName) function – hahaha May 06 '14 at 08:18
  • i followed this answer to work with cookies in javascript http://stackoverflow.com/a/1599291/1853133 and everything worked fine, online too. If is still the same, then it is your server issue. Wait for expert's reply. (Add `jquery-cookie` tag to your question) – Prateek May 06 '14 at 08:27
  • it looks like it is my server problem because again this works fine on localhost but not on the live server. thank you very much for your time – hahaha May 06 '14 at 08:32