Jump to content

Cookies


Janus13

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
Link to comment
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.
Link to comment
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!
Link to comment
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.
Link to comment
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!
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.