Forum Moderators: coopster

Message Too Old, No Replies

help with a contact form and a cookie

contact me, then get hello'ed when you comeback :)

         

tarr74

10:19 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



I,
i am putting up a small website and I am trying to do suchh a thing for the first time, since i've never used PHP and cookies.

I built a smal contact form and i would like the site to say hello to the visitor when he comes back using what he wrote as his name in the contact form as a cookie's content.

So.
I created a small form like this (I stripped down all graphics in this post):


<form method=post action="sendemail.php">
<input type="text" size="60" name="nome">
<input type="text" size="60" name="email">
<input type="text" size="60" name="subject">
<textarea cols="65" rows="6" name="text"></textarea>
<input type="submit" name="submit" value="Invia!">
</form>

Then In sendmail.php, wihich is actually a webpage, I put the form processor script:


<?
echo "Thanks for contacting us, $nome";
?>
<p>
<?
$name = $HTTP_POST_VARS[’name’];
$emailcontent = "
Email sent by the website
NAME OF THE GUY: $nome
HIS EMAIL: $email
WHAT HE WROTE: $text
END
";
$address="info@mysite.it";mail("$address","$subject","$emailcontent","from:$email","$nome");
echo "<html><h3>here's your message: </h3><br>";
echo "<b>Subject: </b>$subject <br>";
echo "<b>Your message: </b>$text<br>";
echo "<b>Your email: </b> $email <br><br>";
?>
</p>

Ok. Up in sendmail.php, even before teh doctype I put this code:


<? setcookie("UsersName", $nome, time()+60*60*24*366*3, "/", ".mysite.it", 1); ?>

In my index.php I put this code:


<?php
if(isset($_COOKIE['UsersName'])){
echo "Hello, ".$_COOKIE['UsersName']."! Welcome back!";
}
?>

But nothing shows up :(

Can any PHP coder help me?

jatar_k

12:06 am on Mar 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



does the value come through in the email?

the setcookie function returns bool so you could test that value, if it's false then it died.

have you checked to see if the cookie is actually on your machine?

also you really shouldn't be using php with register_globals [php.net] on. You should be accessing the posted variables using $_POST [php.net].

tarr74

9:16 am on Mar 5, 2008 (gmt 0)

10+ Year Member



Yes, the email arrives correctly.
The rest of your post isa arab to me :)

jatar_k

2:02 pm on Mar 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if your browser has a way to look at cookies then you could check and see if the cookie was saved on your machine and what it's value is

in mozilla/seamonkey you would go to

Tools > Cookie Manager > Manage Stored Cookies

and then find your site in the list and click on it to view the values

as for checking the return of setcookie [php.net]
the manual page lays it out

If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.

it is just something else to check, though I think it cause an error if it fails so I guess testing it may be moot. I would have to look since I have seldom used setcookie.