Page 1 of 1

Remember Me + Cookies

Posted: Fri Apr 29, 2005 4:44 pm
by bob_the _builder
Hi,

I am trying to add remember me option to a membership system ..

I have managed to get the cookie writen fine using:

Code: Select all

<?

include 'db.php';

$alias = $_POST['alias'];
$password = $_POST['password'];

if ((!$alias) || (!$password)) {
	echo "<center><font class=\"txt\">Please enter ALL of the information!</font></center><br><br>";
	include 'login_form.php';
	return;
}

$sql = mysql_query("SELECT * FROM users WHERE alias='$alias' AND password='$password' AND activated='1'");
$login_check = mysql_num_rows($sql);

if ($login_check > 0) {
	while($row = mysql_fetch_array($sql)){
	foreach( $row AS $key => $val ){
		$$key = stripslashes( $val );
}
		
		session_register('userid');
		$_SESSION['userid'] = $userid;
		session_register('alias');
		$_SESSION['alias'] = $alias;
		session_register('special_user');
		$_SESSION['user_level'] = $user_level;
		
if (isset($_POST['rememberme'])){ 
      setcookie("alias", $alias, time()+60*60*24*100, "/"); 
      setcookie("password", $password, time()+60*60*24*100, "/"); 
} 

		mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");
		
		header("Location: index.php?pages=login_success");

}

} else {
	
		echo "<center><font class=\"txt\">You could not be logged in, Either the username and password do not match or you have not validated your membership.<br><br>Please try again!<br><br></font></center>";
		include 'login_form.php';

}

?>
and a remberme check box in the login form.

I am having trouble with incorporating it into the site so if a user has a cookie set, when they come to the site (index) they are automatically logged in and see all the their options depending on there user rights.

I am using the header footer layout on the site, no mater what I try when you goto the site the index page just hangs.

Thanks

Posted: Fri Apr 29, 2005 4:57 pm
by shiznatix
makes likes 5 and 6

Code: Select all

if (isset($_COOKIE['alias']))
{
    $alias = $_COOKIE['alias'];
}
else
{
$alias = $_POST['alias'];
}
if (isset($_COOKIE['password']))
{
    $password = $_COOKIE['password'];
}
else
{
$password = $_POST['password'];
}

Posted: Fri Apr 29, 2005 4:57 pm
by ol4pr0
Just check if the cookie is set.. thats all than do whatever redirect.. or ect..

Code: Select all

function _check_( &$arr, $name, $def=null ) {
	return isset( $arr[$name] ) ? $arr[$name] : $def;
}
$alias = trim( _check_( $_REQUEST, 'alias', "" ) );
$password = trim( _check_( $_REQUEST, 'password', "" ) );

Posted: Fri Apr 29, 2005 5:17 pm
by bob_the _builder
Hi,

Dont quite understand :oops:

Where would it be defined, right now I use header / footer design. There is a login link that takes them to the login page ../index.php?page=login, there they proceed to login.

Once they check the remember me checkbox I want it so when they load the index page .. they are automatically logged in and see the links = to their user rights, without having to goto the ../index.php?page=login page.

Where would the cookie be defined to allow the above.

I dont want to do a redirect .. I just want it so when they come back to the site (index) they are allready logged in. Much like a forum, when we get here we can post view links available to our user rights etc.

Lost :oops: dont quite understand reading the cookie.

Thanks

Posted: Fri Apr 29, 2005 5:25 pm
by ol4pr0
well what shiznatix said above.

it will check wether there is a cookie yes or no and if there is a post.
So anybody with a cookie loading up that page will automaticly be logged in.

since in that code it will check if $alias var isset in a cookie or in a post.

my function does basicly the same but.. with less lines.
it will check if such a var is set at all. ifso.. $alias will be set to whatever alias. Same goes for the password.

Just try both.. mess around ( set a cookie and see what the results are )

echo things out.. like

Code: Select all

echo $alias;
for example

Posted: Fri Apr 29, 2005 5:55 pm
by bob_the _builder
Hi,

Thanks .. I think I have it sorted.

A small issue I have noticed

I have a default action of:

default:
require 'news/public_news.php';

If at the top of public news I add:

Code: Select all

if (($_COOKIE['alias']) && ($_COOKIE['password'])) {

header("Location: ../index.php?pages=checkuser");

} else {

Once the checkuser page which is needed to register session variables, last login etc etc.. has done the checking, it has another header location that after a manual login would send them to:

Code: Select all

header("Location: ../index.php?pages=login_success");
which is the members news page.

The problem is that everytime a member clicks on the public news link they will be redirected back to the checkuser page and onto the members news page again. How can I stop this loop?

Thanks

Posted: Sat Apr 30, 2005 12:11 am
by ol4pr0
Hopeless and usefull loops are stopped by reviewing your code carefully, and change them accordingly :).

Make nice functions and or classes. Whatever makes it go for you

Posted: Sat Apr 30, 2005 12:33 am
by bob_the _builder
Hi,

What I have got so far:

Code: Select all

<?php

if ($_SESSION['alias']) {

?>


show page

<?php

} elseif (($_COOKIE['alias']) && ($_COOKIE['password'])) {

header("Location: ../index.php?pages=checkuser");

}

?>

Which almost works fine, except for the fact that if the user is a guest the page needs to display as well.

How does on acheive that?

Also how it is layed out I assume if the user is logged in allready its not parsing through the checkuser.php page again?

Thanks