[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Cookie that remembers email address

Posted on 2011-10-31
21
Medium Priority
?
347 Views
Last Modified: 2012-05-12
Hey Experts!!

I have a landing page I'm building that requests the users email. My client wants the user to enter in the email and their name, nothing else. When the user hits submit, the information will be sent to my client. No entry into any database.

However, once you enter in your email, he doesn't want that user to be able to sign up again. I'm not sure how to go about it without a database, so I figured a cookie.

Question: Can someone help me through the process either with a tutorial or some sample code on how to implement this kind of thing?  Haven't had to deal with incorporating cookies, since I'm mostly front-end.

Thanks in advance!
0
Comment
Question by:LZ1
  • 9
  • 6
  • 3
  • +2
21 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37058055
Using a cookie will not help much.  The user can delete cookies anytime.  Without some kind of storage on the backend.  You are not going to be able to do it reliably.  anything on the users computer including localStorage is out of your control.
0
 
LVL 30

Author Comment

by:LZ1
ID: 37058073
@COBOLdinosaur: Thanks for the quick reply.  I understand. The client however has requested that I still implement something like this. He seems to think users won't kill their cookies for a while, or at least until after the landing page has come down.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37058104
This part makes it impossible: No entry into any database.

But there are some articles here with design patterns that can be useful to you.  Look this over.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3314-How-to-Show-an-Introductory-Web-Page-Once-Using-PHP.html

It does not make sense to store important information in a cookie.  The usual design pattern is to store a data base key in the cookie, and then look up the client information in the data base.  This article shows how to do that.  Actually it shows a lot more, but the part you want to focus on is the "remember-me" functionality.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

Best of luck with it, ~Ray
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37058353
To 'prevent' them from signing up again, you can store any identifying data in a cookie.  http://us.php.net/manual/en/function.setcookie.php  Note that you have to read the cookie too...

However, I have 5 different browsers on this computer and also on the computer next to it and more on the other 7 computers and they do Not share cookies.  If this is for convenience, then the cookie might be ok.  If it is for security, then it's not Ok because even with the cookie, I could sign up at least 20 times without your client knowing it.  A real login system would be needed in that case.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37058511
Here's the data base "solution:"

CREATE TABLE users (email VARCHAR(96) UNIQUE...

If you try to insert a duplicate value into a column defined as UNIQUE your server will throw mySQL_ErrNo() == 1062.  You can test for this error and your program can react appropriately.

Of course if a user has as many emails as I do, and several instances of the browser, it may not be possible to prevent duplicate registrations, if the individual (not the email) is the important part of the process.  In other words, it will be difficult to know more than "all emails are unique" unless you take additional measures.
0
 
LVL 30

Author Comment

by:LZ1
ID: 37058762
@Ray:
The article you have is good. That sounds pretty close to what I'm looking for.

@Dave: I completely understand and have even briefed my client regarding the same user with different PC's/browsers. However, he seems to believe that once people sign up they won't come back again. I wanted to go with the database solution first, however the client quickly put a stop to it. He wants no backend, as he wants to keep his costs down.

What about submitting the form to a database AND emailing him. I suppose we could do it that way and just not tell the client about the DB. However, that is completely unethical and in the long run may be more trouble than it's worth.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37058944
As long as there isn't any money involved, I'd give him what he asked for since you have briefed him.  If money is involved, it is too insecure and I would refuse the project.   But then, I don't need the work.
0
 
LVL 30

Author Comment

by:LZ1
ID: 37058970
@Dave: Obviously I'd rather do a DB and call it a day. However, I would never go against my clients wishes if it meant doing something unethical or immoral.  It's just another regular project that we've all been on for a while now, so money is involved. I'm going to see about implementing Ray's PHP code found in his article and go from there.  
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37059230
I don't get the part about "no data base."  They're free!  And it's the right way to achieve the goals!  Maybe you should show your client this thread and let the client in on the recommendations of your fellow experts.  Just a thought...
0
 
LVL 5

Expert Comment

by:liveaspankaj
ID: 37061454
hi LZ1

its so simple to use a cookie you might not believe when you read this:

http://www.w3schools.com/PHP/php_cookies.asp

or just do this:
when you are getting the values set the cookie variable say "verified" like this:

<?php
setcookie("verified", "1");
?>

put your email form script inside a condition like this:

<?php if(!isset($_COOKIE['verified']) { ?>

//User Form

<?php } ?>

0
 
LVL 30

Author Comment

by:LZ1
ID: 37070231
Ok, after hashing it out with the client I came up with a different solution. What about using a php redirect.
1. Check if the there's a cookie
2. If not, set a cookie
3. If there is, redirect to a different page

Not to sure on the PHP for that.  
0
 
LVL 30

Author Comment

by:LZ1
ID: 37070585
I have this so far, but it seems that if I use the back button and/or hit refresh on the initial page it doesn't redirect properly
<?php
$value = 'been here';
setcookie("capture", $value, time()+9999999); 

if (isset($_COOKIE["capture"]))
	header('Location: http://www.mysite.com/folder/folder/landing.html');
?>

Open in new window

0
 
LVL 5

Accepted Solution

by:
liveaspankaj earned 2000 total points
ID: 37071214
you cannot get the value when you set it.

cookie is stored on browser so when you set a cookie it goes to browser and stored there.. next time when you load the page, you will get the cookie value.

when you refresh, cookie is not sent because it sends old values

so it will work for all practical purposes. you can do this.


if(isset($_POST['submit']) && !empty($_POST['email']) && !empty($_POST['name'])){
setcookie("capture", $value, time()+9999999);
header('Location: http://www.mysite.com/folder/folder/landing.html');
}

if(isset($_COOKIE["capture"]))
header('Location: http://www.mysite.com/folder/folder/landing.html');

0
 
LVL 30

Author Comment

by:LZ1
ID: 37071324
So what is the first line for?
It is a capture page with an email form, but I wanted to set the cookie when they visit, not necessarily when they submit
if(isset($_POST['submit']) && !empty($_POST['email']) && !empty($_POST['name'])){

Open in new window

0
 
LVL 5

Expert Comment

by:liveaspankaj
ID: 37071602
yes for that.

ok you can remove if so...but you would set the cookie only after someone submits the form i guess???
0
 
LVL 30

Author Comment

by:LZ1
ID: 37071611
As soon as the user lands on the page, I want the cookie set, regardless of form entry.
0
 
LVL 5

Expert Comment

by:liveaspankaj
ID: 37071643
OK

your initial requirement was little different...you wanted to show the name, email form only if its not submitted.

so setting a cookie when name, email form is submitted and then showing the form only if cookie is not set willl do the job.

0
 
LVL 5

Expert Comment

by:liveaspankaj
ID: 37071651
rest you can try what you wanted with the info :) and ask if it doesnt work....
0
 
LVL 30

Author Comment

by:LZ1
ID: 37079244
Sorry for the delay.  Now it's not even setting a cookie.
Here's the code:

<?php
if(isset($_POST['submit']) && !empty($_POST['email']) && !empty($_POST['name'])){
setcookie("capture", $value, time()+9999999);
header('Location: http://www.mysite.com/landing.html');
}

if(isset($_COOKIE["capture"]))
header('Location: http://www.mysite.com/landing.html');
?>

Open in new window

0
 
LVL 5

Expert Comment

by:liveaspankaj
ID: 37080813
if(isset($_POST['submit']) && !empty($_POST['email']) && !empty($_POST['name'])){
it will set the cookie if your Name, Email, Submit form has those names in it : name, email, submit

and the form is submitted
0
 
LVL 30

Author Closing Comment

by:LZ1
ID: 37082357
Just what I needed
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question