Home > PHP >
Remembering Non-Registered Visitors | Sitemap Search |
|
Sections Membership Features
Recent comments
very difficult by alfin Taking the credit for another persons work ? by curious dude. |
Remembering Non-Registered VisitorsPosted by martin on 4 Sep 2002. Remember visitors' information entered on your site, using cookies, to save time if the same details are needed again. You have a couple of forms on your site that require the same user information or a single form that is frequently resubmitted by visitors. You don't have the time to implement user registration and don't want to spend hours or days learning somebody else's code. You are not sure if people will sign-up at all. What you need is to "remember" non-registered visitors. Why people may not register
What are the benefits of "remembering" visitors
And the drawbacks
What is requiredA server-side scripting language such like PHP, ASP, JSP or anything as CGI that will both display the form and process the submitted values. What exactly is itIt is a way to remember some information the visitor of your site entered either for the current session or for a specified amount of time with a cookie. When the visitor submits the form the reusable information, which doesn't change when the same user fills the form another time, is saved. Depending on the implementation an option to permanently save the information as a cookie, or just to save it for the session, may be offered. Example codeThe provided the code is written in PHP, but as mentioned above it can be done in any server-side scripting language or CGI application.
define('COOKIE_REMEMBER', 'remember-me');
session_load_remembered();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
processData();
} else {
displayForm();
}
Our cookie name is defined with a constant, easier to change if the name doesn't suit you. The first function call initializes the session variables from that cookie, if available. Next we check if we should display the form or process the submitted one.
function session_load_remembered() {
if (isset($_COOKIE[COOKIE_REMEMBER]) ) {
// the order of name, email has to be the same as in the code to save the data
list($_SESSION['name'], $_SESSION['email']) =
@unserialize($_COOKIE[COOKIE_REMEMBER]);
}
}
PHP's I've used PHP 4.1 style superglobal global $HTTP_COOKIE_VARS; echo $HTTP_COOKIE_VARS[COOKIE_REMEMBER];
function displayForm($subject = '', $comment = '') {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>"
onsubmit="return true">
<table border="0" cellspacing="5" cellpadding="0" summary="Comments form">
<tr>
<td>Name:</td>
<td><input type="text" name="fullName" id="fullName"
value="<?php echo $_SESSION['name']?>" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" id="email"
value="<?php echo $_SESSION['email']?>" /></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" name="subject" id="subject"
value="<?php echo $subject?>" /></td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" name="remember" />
Remember me
</td>
</tr>
<tr>
<td colspan="2">Comments:</td>
</tr>
<tr>
<td colspan="2">
<textarea id="comment" name="comment" rows="8" cols="40">
<?php echo $comment?></textarea>
</td>
</tr>
<tr>
<td><input type="submit" value="Add comment" /></td>
<td><input type="reset" value="Clear fields" /></td>
</tr>
</table>
</form>
<?php
}
This isn't perfect HTML, The example PHP function displays a comments form. Note that we've set the default values of the
function processData() {
// some validation
// redisplay the form if it fails with a line like:
// displayForm($_POST['subject'], $_POST['comment']);
// if all is ok
$_SESSION['name'] = $_POST['fullName'];
$_SESSION['email'] = $_POST['email'];
if (isset($_POST['remember']) ) {
setcookie(COOKIE_REMEMBER,
serialize(array($_SESSION['name'], $_SESSION['email'])), time() + 31104000);
}
// add the info to database
}
This one handles the submitted data, validation needs to be put in place of course. If all data is correct we can save the data in the session, and if the user requested also set up a cookie that will remember the info for his/her next visit to the site. Note: If you display user supplied information on your site you should always remove HTML code with The POST variable If your website is accessed under a directory name you should add the next optional parameter to Commentsawesome by derek (derek@geekunity.com) on 30 Jun 2004 5:21am GMT That was the simplest, easiest to follow 'remember me' script i've found. Thanks. I dont understand... by Adrien (american_piggy@hotmail.com) on 8 Oct 2004 3:38am GMT Hi, I would love to have a remember me code on my php, but I do not understand. I have successfully made the script that http://www.evolt.org/article/Creating_a_login_script_with_PHP_4_part_II/17/27093/ they have on their site and I would like to know where to add all the information you have for the remember me part. Please help me with the code given on that site. Thank you. |