Reputation:
I have a php upload handler in upload.php, and there, I have to following
<? setcookie("test",100,time()+3600); ?>
but, when I check the cookies that are set, I dont see any "test" cookie at all.
Can you please help me set a cookie on file upload? why is this upload script any different from any normal script that is accessed by the browser?
Here is the code I have
<?php
if (!empty($_FILES)) {
if(move_uploaded_file($tempFile,$targetFile))
{
setcookie("targetPath",$targetPath,time() + 3600,'/');
print $_COOKIE['targetPath']; // prints fine here
echo 1;
}
else
echo -1;}
else
{
//print_r($_COOKIE);
print "start cookie >> ";
print $_COOKIE['targetPath']; // does not print when I call upload.php standalone
print " << end cookie";
}
?>
Upvotes: 1
Views: 1360
Reputation: 186662
Try specifying the domain?
<?php
setcookie( 'test', 100, time()+3600, '/', '.sitename.com' );
Are you fetching it with $_COOKIE['test']
?
PS - You should not be using short tags. Replace <?
with <?php
.
Upvotes: 0
Reputation: 88816
This may or may not solve your problem, but I thought I should point it out:
$_FILES["pictures"]["tmp_name"][0]
Cookies set with setcookie don't show up until the next page load. And yes, this is documented in the PHP manual:
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.
This means that this code:
setcookie("targetPath",$targetPath,time() + 3600,'/'); print $_COOKIE['targetPath']; // prints fine here
should print the cookie's old value.
setcookie returns false if it fails. You might want to check that return value.
Upvotes: 2
Reputation: 9429
setcookie has "path" argument. If it is not specified: "The default value is the current directory that the cookie is being set in." So most likely you're trying to set cookie for something like www.youdomain.com/actions/upload.php and in that case cookie will be set for /actions/ path.
Also check that call setcookie is done before any output from your script
Upvotes: 0
Reputation: 162841
Are you checking to see if cookies are set in upload.php, ie. the same script you set them? If so, I wouldn't expect them to be set. The cookie will be sent by the client on the next HTTP request after they have received the cookie from upload.php.
Upvotes: 0