Forum Moderators: coopster
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?
<?
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!
pathargument 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 ;)
Hoping be useful,
Herenvardö