Forum Moderators: coopster

Message Too Old, No Replies

Include cookie

         

AlexLee

10:00 am on Jan 31, 2005 (gmt 0)

10+ Year Member



How do I do an include for cookie?

I want it to recheck if user is logged in every page. If so, page continues to load content.

Else just display an "User not logged in" error and nothing else.


<?php
//This would be the include file.
if (isset($_COOKIE["username"]) && isset($_COOKIE["password"]))
{
//Continue to print content of page.
}
else
{
//Print error!
}
?>

Zipper

12:56 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



use a new variable to store the validation state.

e.g: your include file cookie.php


<?
if (isset($_COOKIE["username"]) && isset($_COOKIE["password"])){
$valid = 1;
}
?>

in other pages use it in the following way.

<?
include("cookie.php");
if(isset($valid)){
//Continue to print content of page.
} else {
//Print error!
}
?>

P.S: I hope you're password in the cookie is encrypted else it's not advisable.

AlexLee

3:28 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



I only did this...

setcookie("username", $username, time()+3600);
setcookie("password", $password, time()+3600);

How should I go about encrypting? And is it really necessary?

Zipper

4:20 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



assuming you're using this on the web, it's not a standard practice to store a raw password in a cookie as it's easily accessible by anyone using a public computer.

encryption can be done by a custom algorithm or built-in functions in php like for an example md5() [php.net]. However in this case, you will have to query the database on every page load to get the raw password and match it with the one stored in the cookie.

if you're not willing to query the db for every page load, you can drop cookies entirely and use sessions instead, which will use the server to store the data instead of the client machine.