Page 1 of 1

Setting a Session Cookie

Posted: Thu Nov 23, 2006 6:48 pm
by spycat
Hello all,

This pertains to a form I have on my page.
As it is now, if the person filling out the form makes a mistake or leaves something out, the form reloads, but all the prior responses are wiped out.
I can see how this might annoy someone, to have to fill everything out all over again, even if they neglected to fill in just one field.
What I would like is to have the error noted (as it is now) but to reload the values the visitor posted before hitting "Submit".
I assume the way to do this is via session cookies (the kind that only last while the browser is open).
I must have looked at about 50 different sites on the net, but I could not find a simple explanation. Most go off on some complex application of setting cookies, far beyond what I am looking for.

Basically, all I am looking for is:

A) A way to set the above cookie
and

B) Where to place it

Also,it would be nice to have:

C) The cookie is destroyed upon SUCCESSFUL completion of the form.

Can anyone please help?
(PLEASE, while I appreciate all responses, I have noticed that a lot of responses in these forums come in the way of "hints".
I know this is to "help" rather than "do", but I don't want to be a full-fledged PHP programmer -- I just need this to work.
Thanks :D


Below is my code:

Code: Select all

<?

$RandomStr = md5(microtime());
$Str = substr($RandomStr,0,5);

if($_GET['error'] == "empty")
{
$msg = "Please fill all form fields.";
}

elseif($_GET['error'] == "name")
{
$msg = "Please fill in your name.";
}

elseif($_GET['error'] == "email")
{
$msg = "Please provide valid email address.";
}

elseif($_GET['error'] == "phone")
{
$msg = "Please provide your daytime phone number.";
}

elseif($_GET['error'] == "event_title")
{
$msg = "Please provide the title of the event.";
}

elseif($_GET['error'] == "event_date_and_time")
{
$msg = "Please provide the date and time of the event.";
}

elseif($_GET['error'] == "event_location")
{
$msg = "Please provide the location of the event.";
}

elseif($_GET['error'] == "event_phone_number")
{
$msg = "Please provide a phone number for the venue.";
}

elseif($_GET['error'] == "event_price")
{
$msg = "Please provide a price to attend the event.";
}

elseif($_GET['error'] == "event_description")
{
$msg = "Please provide a description for the event.";
}

elseif($_GET['error'] == "spam")
{
$msg = "Please enter correct turing code.";
}

elseif($_GET['msg'] == "success") {
$msg = "We have received your feedback and will get back with you as soon as possible, if required.";
}
else {
$msg='';
}

?>

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Clubs Submission Form</title>
</head>

<body>
<p style="color:#ff0000;font-weight:600;"><?=$msg?></p>
<form name="Contact" action="SendMail.php" method=post>
<!--<input name="recipient" type="hidden" value="clubsub@metronews.com">-->
<!--<input name="subject" type="hidden" value="Clubs submission from Metroactive.com">-->
<!--<input name="redirect" type="hidden" value="http://www.metroactive.com/contact/thanks.html">-->

<p>Your Name:</p>
<input type=text name="name" class="class_form_field">

<p>Your E-mail Address:</p>
<input type=text name="uemail" class="class_form_field">

<p>Your Daytime Phone:</p>
<input type=text name="phone" class="class_form_field">
<br><br>

<p>Club Event Title:</p>

<input type=text name="event_title" class="class_form_field">
<br><br>

<p>Date and Time:</p>
<input type=text name="event_date_and_time" class="class_form_field">
<br><br>

<p>Location:</p>
<input type=text name="event_location" class="class_form_field">
<br><br>

<p>Phone Number:</p>
<input type=text name="event_phone_number" class="class_form_field">
<br><br>

<p>Price:</p>

<input type=text name="event_price" class="class_form_field">
<br><br>

<p>Event Description:</p>
<textarea class="class_form_field" rows=20 wrap=virtual name="event_description"></textarea>

<p>Enter Security Code:</p>

<p style="background-image:url(images/anit-spam.gif);width:150px;height:50px;"><b><?=$Str?></b> &nbsp;</p><input type="text" size="8" name="code">
<input type="hidden" size="8" name="hcode" value="<?=$Str?>">
<br /><br />
<input type=submit value="Submit Contact Form">
          <input type=reset value="Clear Form" size="20">
          </form>
</body>
</html>

Posted: Thu Nov 23, 2006 6:58 pm
by evilchris2003
to set a cookie you use the

Code: Select all

setcookie('name', $array[0]);
if desired add time() to give an expiry

once an action is complete
use setcookie again but without the attributes
to check if the cookie exists use

Code: Select all

if (isset($_COOKIE['name']))
{
do something;
}
else
{
dont do it or error;
}
alternatively use

Code: Select all

if (!isset($_COOKIE['name']))
to check if its not set

Posted: Thu Nov 23, 2006 7:42 pm
by spycat
Hi Chris,

Thanks for the response.
I was wondering tho, what components should exist in the $array
Also, "do something" and "don't do it"?
I am afraid I am lost on those points.

Happy Thanksgiving.

Posted: Fri Nov 24, 2006 4:20 am
by evilchris2003
well an example is this

Cookies can be used to authenticate a login so you do this

Code: Select all

if (!isset($_COOKIE['username'])) //If no username exists ie Cookie is unset
{
         header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/login.php");
}
else
{
         Show page etc
}
the header() function performs a redirect and sends the user where ever you specify

here is an example of performing the same task using sessions instead of cookies

http://www.tutorialized.com/tutorial/Us ... gout/18314

the array is set to contain the values within that COOKIE as i mentioned in the first post you can add a time() for expiry

Code: Select all

setcookie ('username', $row[0], time()+3600, '/', '', 0);
sets an expiry time of 1 hour