SRL Archive - General

learning teh PHP, cookie help


Awkwardsaw

2009-07-13 05:01:06

alright, im learning the basics of php, and im trying out cookies ;)

so far, i have this:
<?php
setcookie("fname", name, 60);
?>


<html>
<body>
<center>
<title>loltest</title>


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

</form>

<?php
if (isset($_COOKIE["fname"]))
echo "Hello" . $_COOKIE["fname"] . "!<br />";
else
echo "Hello Guest!"; ?>
</center>
</body>
</html>

the problem is it either doesnt save the cookie, or it doesnt load it correctly

it should be self explanitory, a form tells you to put in your name, and it saves it to a cookie. then it should echo your name, when ever you go to the page( i believe ). i also read a tut somewhere saying that saving the cookie should be above all HTML tabs, it seems to work because i got an error when i didnt. im a noob at this so it should be very obvious :p.

and if your wondering, the web page is here

thanks

Da 0wner

2009-07-13 05:19:32


setcookie("fname", name, 60);
Shouldn't it be

setcookie("fname", $_POST['name'], 60);

Awkwardsaw

2009-07-13 05:20:47

thats not what the tut's said :p (2 of them)

i'll try it out

e: nope :p

Craig`

2009-07-13 22:36:44

Meh clean up the code please?

[php]
// begin: phpcookieform.php

<?php
setcookie('fname', name, 60);
?>

// end: phpcookieform.php

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>loltest</title>

<style type="text/css">
body {
margin: 0 auto;
}

label {
display: block;
}
</style>
</head>

<body>
<form action="phpcookieform.php" method="post">
<fieldset>
<legend>phpcookieform</legend>

<label for="name">
<span>Name: </span>
<input id="name" name="name" type="text" />
</label>

<label for="age">
<span>Age: </span>
<input id="age" name="age" type="text" />
</label>

<input type="submit" />
</fieldset>
</form>

<?php
if (isset($_COOKIE['fname'])) {
echo '<p>Hello'.$_COOKIE['fname'].'!</p>';
} else {
echo 'Hello guest!';
}
?>
</body>
</html>
[/php]Also, I believe that the problem is that you specified no type attribute for name/age.

Note, my code is xhtml 1.1, and thus a xml declaration is at the top of the file - turn short tags off!
^ Also, xhtml is not for presentation, so I fixed that up with css ;]

Shuttleu

2009-07-13 23:27:49

the last parameter is the expirey time
and i think that is in seconds
so if you want it for 1 hour then put 60*60
(i think)

~shut

Da 0wner

2009-07-13 23:35:17

1 hour = 3600.

It is in seconds Shut :).

Shuttleu

2009-07-14 01:34:16

From: Da 0wner
1 hour = 3600.

It is in seconds Shut :).


so i was right
and if you put 60*60 then it will be easier to read :)

~shut

Awkwardsaw

2009-07-14 17:13:54

no, it was only suposed to be for 1 min ;)

and math, xhtml is something i still have to learn. and maby css :p

Floor66

2009-07-14 17:23:00

[php]
<?php
if ($_POST["go"])
{
setcookie("fname", $_POST["name"], 60);
}
?>

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>loltest</title>
<style type="text/css">
body {
margin: 0 auto;
}

label {
display: block;
}
</style>
</head>

<body>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<legend>phpcookieform</legend>

<label for="name">
<span>Name: </span>
<input id="name" name="name" type="text" />
</label>

<label for="age">
<span>Age: </span>
<input id="age" name="age" type="text" />
</label>

<input type="submit" name="go" />
</fieldset>
</form>

<?php
if (isset($_COOKIE["fname"]))
{
echo "<p>Hello ". $_COOKIE['fname'] ."!</p>";
}
else
{
echo "Hello guest!";
}
?>
</body>
</html>
[/php]

Long time no PHP >.>

Da 0wner

2009-07-14 17:32:23

Floris,
Parse error: syntax error, unexpected T_STRING in /home/nsyedcom/public_html/awkwardsaw/phpcookieform.php on line 8.

Awkwardsaw

2009-07-14 21:14:01

how would you do it without xhtml? =\

Da 0wner

2009-07-14 21:15:47

XHTML is not even required lol. You can use noob HTML when just testing :p.

Floor66

2009-07-14 21:23:32

True that.
setcookie is just broken. I tried a simple:
[php]
<?php
setcookie("foo", "bar", 3600);
if (isset($_COOKIE["foo"]))
{
echo $_COOKIE["foo"];
}
else
{
echo "Epic failz.";
}
?>
[/php]

Run it?

senrath

2009-07-14 21:33:57

setcookie works just fine. The problem is the cookie you're trying to set has an expiry time set in the past. Expiry time is set as a number of seconds since the epoch. Change this:
[php]<?php
setcookie("fname", name, 60);
?>
[/php]
To this:
[php]<?php
setcookie("fname", name, time() + 60);
?>
[/php]

Awkwardsaw

2009-07-14 21:34:39

i will try when im at the right cpu, unless Da 0wner wants to :p

Da 0wner

2009-07-14 21:37:43

Sure, I'll do it. Btw, I love you senrath. That seems like it should work :).

Floor66

2009-07-14 21:40:07

I think senrath might be true… *goes to php.net*

EDiT:
f*** me lol:
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

senrath

2009-07-14 21:42:26

Glad I could help.

Da 0wner

2009-07-14 21:46:06

Senrath, http://awkwardsaw.da0wner.info/phpcookieform.php.

It still doesn't work :(.

From: Floor66
I think senrath might be true… *goes to php.net*

EDiT:
f*** me lol:
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).


So if it is 0, it's basically a session variable..?

Floor66

2009-07-14 21:49:21

That's what php.net says :P

Awkwardsaw

2009-07-14 22:06:49

da owner, yeah it works =p

you just need to add a space to the greeting. which is no problem

and thanks to every one

Da 0wner

2009-07-15 09:03:41

Oh, it's been working I guess.

I just had to clear my cache :p.

Craig`

2009-07-15 10:28:55

just to mention: XHTML is the way foward ;]

Floor66

2009-07-15 11:02:37

Yeah I always make my things verified.

Nadeem

2009-07-19 20:16:29

You should use sessions, they are more safer for quick browsing. Cookies are better when you want to save information onto the user's computer to regain when they visit the website again.

[PHP]
<?php
if (isset($_POST["savecookie"])) //
setcookie("fname", $_POST["name"], 60);
?>


<html>
<body>
<center>
<title>loltest</title>


<form action="phpcookieform.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" name="savecookie" />
</form>

<?php
if ($_COOKIE["fname"]) //
echo "Hello" . $_COOKIE["fname"] . "!<br />";
else
echo "Hello Guest!";
?>
</center>
</body>
</html>
[/PHP]



~NS

Awkwardsaw

2009-07-19 20:55:28

i know what cookies do :p this was just a thing for me to understand how you do it

Da 0wner

2009-07-19 20:56:59

Sessions <> Cookies -.-. Well sessions are deleted when your browser is closed. Cookies can achieve the same with a 0 expiry time.