FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


using cookies in PHP





masterevil
Hey, i need to use cookies in php in order to make a good site.
setcookie("uname", $name, time()+604800, "/", ".frihost.net", "1");
It must be like that.

i have 2 files. first one:
hello.html
Code:
<html>
<body>
<?
if(isset($_COOKIE['uname']))
{
echo "<br>Hello, ".$_COOKIE['uname']."!";
}
else
{
echo "<br>save your name please.";
}
echo "<br>";
?>
<form action="./hello.php" method="post">
Name:
<input type="text" name="Name"><br>
<input name="Submit" type="submit" value="Submit">
<input name="Reset" type="reset" value="Reset">
</form>
</body>
</html>

and hello.php

Code:
<?
$name = $_POST['Name'];
setcookie("uname", $name, time()+604800, "/", ".frihost.net", "1");
echo "$name saved successfully<br><br>";

require("./hello.html");
?>

i uploaded it to my site. But it doesnt work. if(isset($_COOKIE['uname'])) is always false. How can i save my cookies. I want my site to see those cookies.
mathiaus
The 1 in the setcookie
Code:
setcookie("uname", $name, time()+604800, "/", ".frihost.net", "1");

means the cookie will only be set over a secure line. Either remove or change to 0 eg.
Code:
setcookie("uname", $name, time()+604800, "/", ".frihost.net", "0");

or
Code:
setcookie("uname", $name, time()+604800, "/", ".frihost.net");
masterevil
Thanks for helping.
http://masterevil.frihost.net/try/hello.html have a look at that.
I write "asd" first.
It tells me to save my name.
then i write "zxc"
and it prints "Hello asd"
Why does it follow my commands one step back?

And, what it secure line?
mathiaus
html files cant parse php so change it to something.php
You can't access a cookie as soon as its set. if you redirect back to the page it will work
Code:
header("Location: http://masterevil.frihost.net/try/hello.php");

a secure line is when you use https:// instead of http:// after having brought a secure certificate. It just means that data is encrypted and used mainly when your using card details
burningjacks
while were on the topic of cookies. if i use the SETCOOKIE() to remember my page. how exactly do i use it? i couldnt quite figure it out. im rather new to PHP.
thanks
KHO
burningjacks wrote:
while were on the topic of cookies. if i use the SETCOOKIE() to remember my page. how exactly do i use it? i couldnt quite figure it out. im rather new to PHP.
thanks

setcookie(name, value, expire, path, domain);
that is the basics for a cookie, so use something like
<?php
setcookie("uname", $name, time()+36000,"/", "0");
?>
to set a cookie with the name "uname" the value of "$name" which will expire in 36000 min.(l believe it is min. l don't really use cookies yet, so correct me if l am wrong on this part Neutral) that will be saved in / and will be unsecure so that everyone can access it.
mathiaus
its seconds not minutes so you have to use large numbers which is annoying Rolling Eyes
Otherwise you got cookies to a tee!
KHO
Thats what l thought, l knew it was one of the two Neutral but yah, thats all you really need Neutral not that difficult, l just learned it in about 30 seconds Neutral so hey, not all that bad for just barely looking at it Neutral
Related topics
HELP: setcookie() and header problems
Introduction To Cookies :
managing a bilingual site
php mail and BCC
php include path
INSTALL PHP
Anyone Using Simple PHP Blog
[PHP] phpbb registration - add to other db
[php] can't display rss feeds using rss2html.php
Rebuilt site using php, google seems confused
Im using apahe,php, and mysql and dreamweaver (HELP)
[VB.NET] Open a page using IE cookies
Using Etag as Session - Comments Needed Please
How do you Create a New Database using a php script?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.