Simple Cookie code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Leito
Forum Newbie
Posts: 2
Joined: Sat Jan 03, 2009 11:52 am

Simple Cookie code

Post by Leito »

Hai,
Ive only just started learning PHP and i want to make a very simple login.
i know this has almost no security and i should never use it how ever its so i can learn some PHP.

So ive got a login script, once im logged it it says redirecting. During those 5 seconds of redirecting a cookie is created. In the next page and in all the pages i want a code which checks up on that cookie and if it is non existant then you are "logged out" and redirected back to the login page. and even if you try again going on that page you will be put back to the login.
I have all the codes except for the one where it checks for the cookies. I have posted on Yahoo! answers and this is what i got:
<?php
if(isset($_COOKIE["Name"]))
{
header("location: somewhere.php");
}

?>

don't forget that all of this comes before the header, so it has to be the first thing sent or it doesn't work. The other alternative would be to buffer your headers, but it's probably just easier to make sure this is stated first.
So i have done as this person said and this is what i have done:
http://lite-hosting.net/Login%20code/test.php
(The first page is after the login code so you are being redirected and the cookie is being created so it lasts for 1 hour. This cookie is called TestCookie)
The next page which you are redirected onto has a cookie called "cake" <=== yes very pro i know :oops:

so i should be redirected, Right?
well nothing happens :(
Please help


Thanks a lot and have a happy 2009
Leito
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Re: Simple Cookie code

Post by mikelbring »

One thing, try to use sessions over cookies. I only use cookies for static data.
Leito
Forum Newbie
Posts: 2
Joined: Sat Jan 03, 2009 11:52 am

Re: Simple Cookie code

Post by Leito »

So how would I do that? (please remember this is my first code so I'd like to learn from it and not stare at at for one year and not understand a thing ;) )
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple Cookie code

Post by califdon »

Post Reply