...

View Full Version : Cookies trouble...



redfroc
11-30-2006, 12:47 PM
Hi...
I start to learn cookies.
First, what I have learn is by writing these code:


<?php
setcookie("uname", $name, time()+3600);
?>
<html>
<body>
<p>
A cookie was set on this page! The cookie will be active when
the client has sent the cookie back to the server.
</p>
</body>
</html>



<html>
<body>
<?php
if (isset($_COOKIE["uname"]))
echo "Welcome " . $_COOKIE["uname"] . "!<br />";
else
echo "You are not logged in!<br />";
?>
</body>
</html>

But, it always display "You are not logged in!".

Help me please... :(

Tyree
11-30-2006, 02:45 PM
I got it to work on mine...here's my code:


//At beginning of page
<?php
if (isset($name)){
setcookie("uname", $name, time()+3600);
}
?>
//In body of page...
<?php
if (isset($_COOKIE["uname"])) {
echo "Welcome " . $_COOKIE["uname"] . "!<br />";
}
else {
echo "You are not logged in!<br />";
}
?>
<form name="login" action="<?php print $PHP_SELF; ?>" method="post">
<input type="text" name="name" />
<input type="submit" value="submit" />
</form>

Yours may not all happen on the same page. But, the code should be similar.

redfroc
12-01-2006, 02:31 AM
thanks.. it works now.. :)
and how about the session? where's should I put the session syntax, so as long as user is logging in, the current page always active.

Fou-Lu
12-01-2006, 03:15 AM
Just put session_start() at the top of any of your pages you want the session to be accessible from. Do not use cookies to track a user, sesions are always a better idea, especially if you designed your system. Then just use $_SESSION to get and set your session variables.

Tyree
12-01-2006, 03:19 AM
You mean so that each page loaded "realizes" the user is still logged in?

Hmmm, I don't have a lot of experience with sessions. You can use cookies and sessions separately.

What I would do personally, given my limited knowledge of sessions, is to just include the issset syntax at the beginning of each page to determine if that cookie is active.

I have an include file that has all sorts of oft-used functions in it. So, I would put the code in there since I always have that file included on each page.

Sorry, I'm not much help on this part of it.

Fear not, someone with sessions experience can help you. :)

See, it happened already! That's why sessions were included in PHP after cookies...sessions are better! ;)

redfroc
12-02-2006, 11:41 AM
I have learn about session at php helps. but it can't help me so much.
I still don't understand completely.
May somebody could give an example to do with that.
I need some code to track user as long as logging in and page would be unavailable when user has logged out.

please help me... :(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum