Question

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. =[

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top