Jump to content

setcookie problem


Krash

Recommended Posts

 

Just started tinkering with cookies - trying to pass a variable between scripts (can't use session variables), and am unable to retrieve cookie values that are saved by one script, and used by another.  The cookie is saved and can be retrieved by a test script in the same browser session, but isn't available to a script running in a different session.  I've tried to set the cookie using the time() parameter to prevent it from expiring at the end of the session, but it doesn't work.  The ccokie will not set if there's a time() parameter, and an existing cookie is deleted if I try to set it again with time().  No clue why this is happening - tried it in three different browsers.

 

This is the test script that sets the cookie successfully -

 


<?php

setcookie('keyval', '3');

echo 'COOKIE set';

?>

 

 

...and this doesn't -

 


<?php

setcookie('keyval', '3', time()+3600);

echo 'COOKIE set';

?>

 

 

 

- Why doesn't the time() parameter work?

- Is it possible to retrieve a cookie value that is set in a different session?

 

Thx.

 

Link to comment
Share on other sites

1. the code above will echo "cookie set" even if its not set.. use this

 

<?php
$cookie = setcookie('keyval', '3', time()+3600, "/");
if($cookie){
print "COOKIE set";
}else{
print "error in setting cookie";
}
?>

 

2. the "/" that I have added indicates that the cookie can be used in all subdomains as well..

3. yes the time() function will work here and should be used..

Link to comment
Share on other sites

Ok, here's what I have:

 

- The cookie does not set if I include the time() parameter.  I allowed for the 60 min. difference between my local time and server time, didn't help.

 

- The 'COOKIE set' is not there to confirm that the cookie is set, just to echo something so I know the script ran.

 

- I tried the subdomain parameter '/' with no effect.

 

- I'm using a separate script to display all cookies and the new cookie:

 


<?php

print_r($_COOKIE) . '<br>';

if (isset($_COOKIE['keyval']))
echo '<br><br> COOKIE = ' . $_COOKIE['keyval'] . '<br>';
else
echo ' No COOKIE <br>';

?>

 

and it confirms that the cookies are deleted if I add the time() parameter to setcookie.

 

I ran your script and it tells me cookie is set, but my read script doesn't find it unless I remove the time() parameter.

 

I'm on a shared Apache server, Unix box, running php5.

 

 

Link to comment
Share on other sites

 

Um... yes, you'll see that's the first line in the read script in my previous post.  It returns a null array - there are no cookies.

 

I tried setting the cookie with javascript, and it works the same.  The cookie will not set if there's an expiration parameter.  The first line doesn't work, second one does -

 


<html>
<body>

<script>
document.cookie = "keyval=xyz; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/"; 
//document.cookie = "keyval=xyz;"; 
</script>

</body>
</html>

 

 

Either the cookie is not being set, or it's expiring immediately, both in php and js.

 

 

Link to comment
Share on other sites

This is an issue on your side. Some clients can be extremely picky when it comes to cookies.

 

I've heard of issues with clients rejecting cookies that don't have all the arguments, especially the domain argument.

 

Try setcookie( 'cookie', 'value', time()+3600, '/', '127.0.0.1', FALSE ); but obviously, replace 127.0.0.1 with '.yourdomain.com' (the dot at the start will allow for www.yourdomain.com to work as well)

Link to comment
Share on other sites

 

I've tried every combination of parameters, and the only one that works is ('name', 'value').  If it's client dependent, then it's not really useable on a production site.

 

Thanks.

yeah I think you're confused.. just because you set cookies using server side PHP, doesn't meant the cookie is server side..

Link to comment
Share on other sites

Actually, no, I'm not confused :confused:.  I need a way to pass a variable from a verification widget running in an iframe on the SMF registration form to the SMF reg handler.  The variable must be unique to each user, so multiple registrations don't step on each other.  Every user runs a unique session, but $_SESSION variables don't work because the widget and SMF use different sessions, so the variable is lost.  Cookies would work because they're client-side and unique to each user, but this problem with setting/retrieving the cookie value has me stumped.  I'm currently using a workaround that has the widget write the value to a text file, and the reg handler reads it and deletes it.  Works fine, and the file only exists for a few seconds, but if multiple users register simultaneously on a busy board, it'll cause a problem.

 

 

Link to comment
Share on other sites

Why don't you use JavaScript to grab the innerHTML of the iFrame?

 

This isn't a good use of cookies. You shouldn't use them for cross-page communications, use them for keeping a single state over multiple page requests.

Link to comment
Share on other sites

Why don't you use JavaScript to grab the innerHTML of the iFrame?

 

How would that work?  The verification variable I need to pass is generated by php in the form handler, and there's nothing in the iframe html that would give me that.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.