SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: Cookie script not working
-
Feb 26, 2005, 07:25 #1
- Join Date
- Nov 2003
- Location
- austalia
- Posts
- 59
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Cookie script not working
I have a page which I wish to restrict access to unless the visitor supplies an email address. I want the script on another page to check for the presence of the cookie named "email". If present a link to the restricted page is to be displayed to allow entry and if not present an input form is displayed requesting the email address.
When executed access is gained, but on returning to the linking page I am expecting the cookie to be found and the input form not to be displayed but the link instead. This is not currently working as intended.
Appreciate someone pointing out my error. (relevant code below)
<!-- this code above <html> tag -->
<? if (isset($email))
{
setcookie("email", $email, time()+365 * 86400);
}
?>
<!-- this code in <body> tag -->
<?
if (isset($email)) {
?>
<a href='freeresourceslinks.php'>Enter Free Resources</a>
<?
} else {
?>
<form name="emailaddress" id="emailaddress" title="Email address" onsubmit="return formvalidation();" method="post" action="cgi-bin/formtomailscript2.cgi">
<table>
<tr>
<td><input name="email" id="email" type="text" /></td>
</tr>
<tr>
<td><input name="submit" id="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
<? } ?>
Thanks
David
-
Feb 26, 2005, 11:22 #2
- Join Date
- Feb 2003
- Location
- Dog Street
- Posts
- 1,819
- Mentioned
- 1 Post(s)
- Tagged
- 1 Thread(s)
Instead of this:
PHP Code:if (isset($email)) {
?>
<a href='freeresourceslinks.php'>Enter Free Resources</a>
PHP Code:if (isset($_COOKIE['email'])) {
?>
<a href='freeresourceslinks.php'>Enter Free Resources</a>
--ed
-
Feb 26, 2005, 17:59 #3
- Join Date
- Nov 2003
- Location
- austalia
- Posts
- 59
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Tried this variation without any noticable difference. Was the original approach wrong?
Any other ideas?
David
-
Feb 27, 2005, 14:12 #4
- Join Date
- Feb 2003
- Location
- Dog Street
- Posts
- 1,819
- Mentioned
- 1 Post(s)
- Tagged
- 1 Thread(s)
You have to reload the page for the cookie to show up.
Try this:
PHP Code:<?php
if (preg_match("/\w+/", $_POST['email']) )
{
setcookie("email", $email, time()+365 * 86400);
header("Location: $_SERVER[SCRIPT_NAME]?checkit=1");
exit;
}
?>
<!-- this code in <body> tag -->
<?
if (isset($_COOKIE['email'])) {
?>
<a href='freeresourceslinks.php'>Enter Free Resources</a>
<?
} elseif ($_GET['checkit']) {
echo "Sorry, your browser needs to support cookies.";
}
else {
?>
<form name="emailaddress" id="emailaddress" title="Email address" onsubmit="return formvalidation();" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<table>
<tr>
<td><input name="email" id="email" type="text" /></td>
</tr>
<tr>
<td><input name="submit" id="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
<? } ?>
-
Feb 28, 2005, 07:33 #5
- Join Date
- Nov 2003
- Location
- austalia
- Posts
- 59
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you
That seems to work fine. Now I just have to make sense of it.
David
Bookmarks