PHP Cookies $_POST help?

Brandizzle

New member
Ok im currently learning about making cookies in php and i was wondering if it is possible to use the $_POST command to get information from a form to input into your cookie? Im trying to create a login cookie that will then display your name and the number of characters in your name using cookies (i can do without the cookie but im learning). Right now im not getting any PHP errors on my page but the name isnt showing up where it is supposed to. Here is the code:
Login.php

<html>
<body>

<form action="Name.php" method="post">
Name: <input type="text" name="fname" /><br />
Age: <input type="text" name="age" /><br />
<input type="submit" name="submit" value="Submit" /><br />

</body>
</html>

and here is the source to name.php which is supposed to display your name(but isnt)

<?php
setcookie("user",$_POST["fname"],time()+3600);
?>

<html>
<body>

<?php
echo "Your name is " . $_COOKIE["user"];
?>

</body>
</html>

Can anyone tell me if this is possible or if i have made an error? And i tried using the $_GET command as well and it didnt work either.

please help me :)

P.S. Im 14 years old hehe :D
 
If I were you I'd use sessions, not cookies. It's simpler, and more secure.

E.g:

session_start();
$_SESSION['user'] = $_POST['fname'];

...

echo 'Name: ' . $_SESSION['user'];

You must remeber to call 'session_start' before using session variables.

--
Hope this helps
 
Back
Top