/    Sign up×
Community /Pin to ProfileBookmark

Cookies and headers

After doing a search on php.net and these forums i have tried to set some cookies, i have a form which sends a name and email address toa page which adds it info to a databse then sets a cookies so when hey go to another page or come back in a wek or month it remembers that they have signed up to the mailinglist and prints there name/email instead of the form.

mailiglist.php //the form sends the data to this page

[code=php]
<?
include(‘library/top.php’);
include(‘library/connect.php’);

print “<p class=’list’><div align=’center’>Mailing list successfully joined</div></p>”;

$name = $_POST[‘name’];
$email = $_POST[’email’];

setcookie(“$cookie[name]”, $name);
setcookie(“$cookie[email]”, $email);

$addmailinglist = “INSERT INTO mailinglist(name, email)
VALUES(‘$name’,’$email’)”;
mysql_query($addmailinglist);

include(‘library/footer.php’);

?>
[/code]

and the header included in the top of every page

[code=php]
<?php
//check to see if cookies are set, if so prints the name and email. else sets the form
if (isset($_COOKIE[‘cookie’])) {
foreach ($_COOKIE[‘cookie’] as $name => $value) {
echo ” Welcome $name : $value <br />n”;
}

}else{

//print the sign up form.
?>
<p class=”list”>
<div align=”center”>Join the mailing list to know when new photos and messages have been added!!!<br><br>
<form action=”mailinglist.php” method=”post” name=”joinmailinglist”>
Name:<input type=”text” name=”name” size=”25″> Email:<input type=”text” name=”email” size=”25″>
<input type=”submit” name=”join” value=”Join”>
</form>
</div>
</p>
<?php //end the form and the cookie code.
}
?>
[/code]

Thanks Alot, i really just cant get cookies.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 22.2005 — Try something like this:
[code=php]<?
ob_start();
include('library/top.php');
include('library/connect.php');

print "<p class='list'><div align='center'>Mailing list successfully joined</div></p>";

$name = $_POST['name'];
$email = $_POST['email'];

setcookie('name', $name, time()+ (365 * 24 * 60 * 60), '/', '', 0);
setcookie('email', $email, time()+ (365 * 24 * 60 * 60), '/', '', 0);



$addmailinglist = "INSERT INTO mailinglist(name, email)
VALUES('$name','$email')";
mysql_query($addmailinglist);



include('library/footer.php');

?>




<?php
//check to see if cookies are set, if so prints the name and email. else sets the form
if (!empty($_COOKIE['name'] and !empty($_COOKIE['email']))) {
echo 'Welcome '.$_COOKIE['name'].' '.$_COOKIE['email'];
}else{

//print the sign up form.
?>
<p class="list">
<div align="center">Join the mailing list to know when new photos and messages have been added!!!<br><br>
<form action="mailinglist.php" method="post" name="joinmailinglist">
Name:<input type="text" name="name" size="25"> Email:<input type="text" name="email" size="25">
<input type="submit" name="join" value="Join">
</form>
</div>
</p>
<?php //end the form and the cookie code.
}
?> [/code]
Copy linkTweet thisAlerts:
@SheldonauthorSep 22.2005 — Thanks Bokeh, i got this error

Parse error: parse error, unexpected T_LOGICAL_AND, expecting ')'

so i changed this line
[code=php]
if (!empty($_COOKIE['name'] and !empty($_COOKIE['email']))) {
[/code]


to

[code=php]
if(!empty($_COOKIE['name']) && !empty($_COOKIE['email'])) {
[/code]


But i couldnt have done it with out your help!!!!!




Thanks!
Copy linkTweet thisAlerts:
@bokehSep 22.2005 — Sorry! I didn't test that. When something has MySQL it's not so easy to test it. Just a silly syntax error anyway. It looks like I pasted the second part in and put it one bracket too far to the left.
Copy linkTweet thisAlerts:
@SheldonauthorSep 22.2005 — Yer no worries! It is hard when you cant test it with mySQL.

Thanks!

I would like to write a likn to delete the cookie, say if the user wants to sign up with another email address or something

?

The link?
[code=php]
if (!empty($_COOKIE['name'] and !empty($_COOKIE['email']))) {
echo 'Welcome '.$_COOKIE['name'].' '.$_COOKIE['email'] '<a href="mailinglist.php?do=remove">Not you?</a>';
}else{
[/code]


and on mailinglist.php?

[code=php]
<?
ob_start();
include('library/top.php');
include('library/connect.php');

if ($_GET['do'] == 'remove'); {

$name = $_POST['name'];
$email = $_POST['email'];

setcookie('name', $name, time()+ (0), '/', '', 0);
setcookie('email', $email, time()+ (0), '/', '', 0);


}
else
{
print "<p class='list'><div align='center'>Mailing list successfully joined</div></p>";

$name = $_POST['name'];
$email = $_POST['email'];

setcookie('name', $name, time()+ (365 * 24 * 60 * 60), '/', '', 0);
setcookie('email', $email, time()+ (365 * 24 * 60 * 60), '/', '', 0);



$addmailinglist = "INSERT INTO mailinglist(name, email)
VALUES('$name','$email')";
mysql_query($addmailinglist);





include('library/footer.php');

?>
[/code]


Would this remove the cookie?
Copy linkTweet thisAlerts:
@bokehSep 23.2005 — You don't need their details to delete the cookies

the following enters an expiry time 24 hours earlier

gauranteeing deletion for all time zones
[code=php]setcookie('name', '', time() - 86400, '/', '', 0);
setcookie('email', '', time() - 86400, '/', '', 0);[/code]
Copy linkTweet thisAlerts:
@SheldonauthorSep 23.2005 — Cheers Bokeh!
×

Success!

Help @Sheldon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...