Jump to content

Archived

This topic is now archived and is closed to further replies.

Janus13

Cookies

Recommended Posts

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

Share this post


Link to post
Share on other sites
post email address upper case ok

example
[code]
setcookie("EmailAddress", '$_POST[EmailAddress]', time()+3600*24*365);
[/code]

Share this post


Link to post
Share on other sites
How does that help.  My variable is emailAddress with a small e, not EmailAddress. Does the capital or not make a difference?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
if its anything like my current page, totalling 345 lines, still incomplete, then NO
We only need to know about cookies.

Everything seems ok

Share this post


Link to post
Share on other sites
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 area
else 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 here
else
{
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!

Share this post


Link to post
Share on other sites
What I do when something dont work is

Create a new BLANK php file

Try what you want in there

For example, check cookies are ok on you and the server, by setting the cookie, then viewing the cookie
Load the page, and the cookie SHOULD be blank
Reload and the cookie should have a value.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

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.