Jump to content


Photo

Cookies


  • Please log in to reply
13 replies to this topic

#1 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 02 August 2006 - 05:08 AM

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

setcookie("EmailRegistration", "true", time()+3600*24*365);
setcookie("EmailAddress", '$_POST[emailAddress]', time()+3600*24*365);
header('Location: homelow.htm');

The header command works fine, but the next time I load the page this part gets ran:

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();	
}

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

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 06:04 AM

post email address upper case ok

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

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 03 August 2006 - 03:17 AM

How does that help.  My variable is emailAddress with a small e, not EmailAddress. Does the capital or not make a difference?

#4 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 03:22 AM

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
Tell me the problem, I will try tell you the solution

#5 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 03 August 2006 - 03:29 AM

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.

#6 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 03:45 AM

if its anything like my current page, totalling 345 lines, still incomplete, then NO
We only need to know about cookies.

Everything seems ok


Tell me the problem, I will try tell you the solution

#7 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 03 August 2006 - 04:09 AM

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:
<?
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.		
	}
}
?>

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!

#8 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 10:23 AM

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.
Tell me the problem, I will try tell you the solution

#9 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 06 August 2006 - 06:47 PM

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.

#10 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 06 August 2006 - 07:03 PM

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.

#11 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 07 August 2006 - 01:52 AM

The single quotes around

'$_POST[emailAddress]'

in the statement below are the problem
setcookie("EmailAddress", '$_POST[emailAddress]', time()+3600*24*365);


#12 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 07 August 2006 - 01:54 AM

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.

#13 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 07 August 2006 - 02:03 AM

no actually the statement above is a problem because it will return the value

'$_POST[emailAddress]'

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!

#14 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 07 August 2006 - 02:10 AM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users