Janus13 Posted August 2, 2006 Share Posted August 2, 2006 I'm having a problem setting and reading cookies and wanted to see if anyone has any ideas on what I'm missing..I have a form that prompts for a persons email address, and once the email address is submitted via a form a check is performed to see if the address is valid, and if that passes then the following code is called[code]setcookie("EmailRegistration", "true", time()+3600*24*365);setcookie("EmailAddress", '$_POST[emailAddress]', time()+3600*24*365);header('Location: homelow.htm');[/code]The header command works fine, but the next time I load the page this part gets ran:[code]if(isset($_COOKIE['EmailRegistration'])){ //cookie is set, see if user exists in database. If they don't then redisplay validation page and exit. $emailAddress = $_COOKIE['EmailAddress']; $query = "select * from table where emailAddress = '$emailAddress'"; $result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows < 1) { setcookie("EmailAddress", '', time()); setcookie("EmailRegistration", '', time()); header('Location: index2.php'); exit(); } header('Location: homelow.htm'); exit(); }[/code]Basically I check to see if the email address exists in a tracking database and if it does it goes on into the rest, but the initial check is to see if the cookie even exists to begin with, and this check always fails, so I have to ask if I am setting the cookie wrong. In a test page I can set a simple cookie, and if I load the page twice it'll display the cookie values. I'm scratching my head on this one so any help would be greatly appreciated!Jon Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 2, 2006 Share Posted August 2, 2006 post email address upper case okexample[code]setcookie("EmailAddress", '$_POST[EmailAddress]', time()+3600*24*365);[/code] Quote Link to comment Share on other sites More sharing options...
Janus13 Posted August 3, 2006 Author Share Posted August 3, 2006 How does that help. My variable is emailAddress with a small e, not EmailAddress. Does the capital or not make a difference? Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 no, php is case sensative, as long as $_POST["emailAddress"] is your actual name of the input, its cool.Are cookies turned off on your machine?remember that cookies dont load on the same page they are set Quote Link to comment Share on other sites More sharing options...
Janus13 Posted August 3, 2006 Author Share Posted August 3, 2006 Right I got that. But if I have a check the second time the page loads then the cookie check should show valid, but it seems like I'm either never actually getting the cookie set, or it's being destroyed somewhere. From what I have posted of the code does anything seem like it would fail. I can post the entire page if that would help. Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 if its anything like my current page, totalling 345 lines, still incomplete, then NOWe only need to know about cookies.Everything seems ok Quote Link to comment Share on other sites More sharing options...
Janus13 Posted August 3, 2006 Author Share Posted August 3, 2006 At the moment it's 245 :)It may seem ok, but something certainly seems to be wrong. I'll post a condensed version of the page:[code]<?if($_GET['action'] == 'resendmail'){ }//Check to see if cookie is set, if not display registration page.else if($_GET['action'] == 'submit'){ //verify email address is valid if(!check_email_address($_POST['emailAddress'])) { echo "There appears to be a problem with your email address"; exit(); } //next verify that the address doesn't already exist $addyCheck = "select * from tblEmailTracker where emailAddress = '$_POST[emailAddress]'"; $result = mysql_query($addyCheck) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $validated = $row['validated']; } //if the address exists, then check to see if the user is validated. If yes then allow in, if not display message! $num_rows = mysql_num_rows($result); if($num_rows > 0) { if($validated == '0') { //HTML stripped to make this more brief. exit(); } else { setcookie("EmailRegistration", "true", time()+3600*24*365); setcookie("EmailAddress", '$_POST[emailAddress]', time()+3600*24*365); header('Location: homelow.htm'); exit(); } } else { $query = "insert into tblEmailTracker (emailAddress) values ('$_POST[emailAddress]')"; $db->dbQuery($query); //email functions stripped out // here it would display a page telling you about the email. exit(); }}//next areaelse if($_GET['action'] == 'validate'){ $email = $_GET['emailaddress']; $query = "update tblEmailTracker set validated = '1' where emailAddress = '$email'"; mysql_query($query) or die(mysql_error()); setcookie("EmailRegistration", "true", time()+3600*24*365); setcookie("EmailAddress", $email, time()+3600*24*365); header('Location: homelow.htm'); exit();}//On default load of page it goes hereelse{ if(isset($_COOKIE['EmailRegistration'])) { //cookie is set, see if user exists in database. If they don't then redisplay validation page and exit. $emailAddress = $_COOKIE['EmailAddress']; $query = "select * from tblEmailTracker where emailAddress = '$emailAddress'"; $result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows < 1) { setcookie("EmailAddress", '', time()); setcookie("EmailRegistration", '', time()); header('Location: index2.php'); exit(); } header('Location: homelow.htm'); exit(); } else { //display registration form - default page deleted to make this more brief. }}?>[/code]The page flows like so: On the first ever visit it displays the last else which is a page with a form asking for your email address. Once you submit it, it adds the email address to a sql table and generates an email to the use at the email address they provided. The form calls the action = submit section of the page. The email link calls validate and validates the email address, then sets a cookie and forwards you on to the main page. During the initial page load it checks to see if the cookie exists and if so it should verify that the user is in the db and if so continue on it's way. It can make it prompt for the address on every page load, but it wont evaluate the cookie. I've went so far to setup a temp page to test the cookie, and it never gets created as far as I can tell. I'm hoping with the code visible someone can point out what I'm missing. Thanks! Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 What I do when something dont work isCreate a new BLANK php fileTry what you want in thereFor example, check cookies are ok on you and the server, by setting the cookie, then viewing the cookieLoad the page, and the cookie SHOULD be blankReload and the cookie should have a value. Quote Link to comment Share on other sites More sharing options...
Janus13 Posted August 6, 2006 Author Share Posted August 6, 2006 I've done that, and the cookie code does work. There must be some logic in my code that does not flow correctly and I was hoping someone could see it where I can't. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 6, 2006 Share Posted August 6, 2006 If you are having problems with logic, go through and echo something meaningful at various places through your code. That might help you identify where something is going wrong. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted August 7, 2006 Share Posted August 7, 2006 The single quotes around [quote]'$_POST[emailAddress]'[/quote] in the statement below are the problem[code]setcookie("EmailAddress", '$_POST[emailAddress]', time()+3600*24*365);[/code] Quote Link to comment Share on other sites More sharing options...
Janus13 Posted August 7, 2006 Author Share Posted August 7, 2006 Actually I think the problem is that I was trying to set the cookie with a $_POST instead of a standard variable, I used $var instead of $_POST and it works now. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted August 7, 2006 Share Posted August 7, 2006 no actually the statement above is a problem because it will return the value [quote]'$_POST[emailAddress]'[/quote] as a string instead of the value of that post value. the single quotes turn it into a string and no retrieval of the $_POST variable is done! Quote Link to comment Share on other sites More sharing options...
Janus13 Posted August 7, 2006 Author Share Posted August 7, 2006 Oh, I didn't realize that. I rewrote the page and got it working, so apparently I got around it by using a set variable instead of the $_POST. Thanks for the info! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.