Welcome to WebmasterWorld Guest from 54.197.187.2

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Cookie help, please

cookies are disappearing, sort of

     
12:48 pm on Nov 15, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:July 2, 2004
posts:607
votes: 0


I'm not at my computer right now, and I can't access my code (which sucks) - as soon as I can get to it, I'll post code - but I was hoping someone would have an idea or two in the meantime.

I'm new to the world of PHP cookies. I have a script (I doctored it from the Zend tutorial) that sets a cookie when a user inputs his email address into a form, then sends the info to a third party database. When the end user is redirected back to the page he came from, the script sees the cookie and outputs a "thank you" message, and won't allow further submissions for 10 minutes.

Here's my problem: the script works fine, when it's only on one page of the site. However, the client has several of these email fields on his site, all on different pages. I *thought* that if the end user surfed to another page that had the email submission form, the script would recognize the cookie and display the "thank you" message on that form, even though it was sent from another page. Of *course* it doesn't work that way. If I put the script on a second, third and fourth page, it seems to completely cancel out the functionality of the script altogether.

If I go to a secondary page with the script, I can still fill out the form and submit over and over again. The cookie *is* being set, and seems to be re-written over by the new input. Without looking at code (for the moment) would anyone have any idea what might be contributing to this?

3:51 pm on Nov 15, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:July 2, 2004
posts:607
votes: 0


Okay! I'm at my computer. Here's my script (extensively commented out!)

<?

isset($_POST['action'])? $action = $_POST['action'] : $action = '';
isset($_POST['error'])? $error = $_POST['error'] : $error = '';
isset($_POST['emailerror'])? $emailerror = $_POST['emailerror'] : $emailerror = '';
isset($_POST['email'])? $email = $_POST['email'] : $email = '';
isset($_POST['send'])? $send = $_POST['send'] : $send = '';

// Define unique cookie prefix
$ID = "email_submitted";

// Cookie lifetime in seconds
$cookie_life = 600; // Currently 10 minutes

// Name of cookie that holds the user's email
$n_email = $ID . "_email";

if($action!="submit"){

// if end user is brought to this page and a cookie has already been set, show the "thank you"
// message, but do not allow a resubmission.

if($$n_email) {

$cookieerror = "2";

$send = "no";

}

// else if the end user is brought to this page - no cookie, no submission (in other words, the first
// time they come to the page), then just show the plage with the blank form.

include ("index.shtml");

exit;

}

// if the user clicks "submit...

if ($action == "submit") {

// and a cookie is already set...

if($$n_email) {

// check to see if the field is empty. If so, return the "invalid input" error.

if($email == ""){

$emailerror = "1";

$send = "no";

}

// if the field is not empty, then return the "you've already sent in a submission" error.

else if(isset($email)){

$cookieerror="1";

$send = "no";

}

}


// if there is no cookie set...

if(!$$n_email) {

// then check to see if the input is in valid syntax. If no, return the "invalid input" error.

if(isset($email)) {

if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{¦}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+$', $email) ¦¦ ereg("'", $email))

{ $emailerror = "1";

$send = "no";

}

// if the syntax *is* valid, the set a cookie and submit the email address.

setcookie($n_email,$email,time()+$cookie_life);
$$n_email = $email;

}

}

if ($send == "no") {

$error = "1";

include ("index.shtml");

exit;

}

include("productspass.php");
exit;

}

?>

All of it is working fine - as long as it's only on one page of the site. I need the same script on *four* pages - and when I do that, it seems to cancel the effectiveness of the cookie. The cookie gets set, all right - but when you go to the next page, the secondary page doesn't recognize a cookie is already there, and overwrites it with the new info. Then, when you go back to the secondary page - it still acts like the cookie isn't there, and the whole point is lost.

Anyone? I'd so appreciate it!

3:08 am on Nov 16, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12547
votes: 2


You may want to set the
path
argument as well in the setcookie() [php.net] function. If your pages are not within the same path you may have issues. I would highly recommend reading and absorbing that page link as well as the Netscape link from that page. A must read for understanding cookies and using them ;)
10:40 pm on Nov 20, 2005 (gmt 0)

Full Member

10+ Year Member

joined:July 7, 2003
posts:282
votes: 0


I agree to the thing of path. I had a similar problem, setting cookies with no path, and the cookie was not found even by the same file just after making it! This is, if I tried
setcookie('myCookie', 'value', time()+30*24*60*60); //30days cookie
echo isset($_COOKIES['myCookie'])?"Cookie set":"Cookie not set!";
then I got
Cookie not set!
Just added two params: my web's IP as domain (I still don't have a domain for this site) and '/' as path. Then it worked perfectly.
So, I suggest you to make a try adding these params to the setcookie call. If it still fails, review your 'if' structures. And, no matter if you solve it or not, if you haven't done it yet, ˇˇtabulate the code! A consistently tabulated code is far easier to debug. (I'm not sure if either you didn't tabulate the code or the forum removed the tabs).

Hoping be useful,
Herenvardö

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members