...

View Full Version : Login with Remember Me feature not working



egbakaet
01-08-2006, 09:29 PM
Can somebody help me? I want this login script to work like this:

1. Checks to see if user and pass inputs are not empty
2. Checks to see if $_POST['remember'] is checked, if so, set 2 cookies: $_COOKIE['cookname'] and $_COOKIE['cookpass'].
3. Checks to see if $_COOKIE's are set, if so, $username1 and $password1 == those cookies, else, they equal the user's posted username and password.
4. Check to see if there's a row in the db where $username and $password are equal.
5. If not, redirect to login page.

Here's my code:


<?php
define("lockout", true);

error_reporting(E_ALL ^ E_NOTICE);

$ip=$_SERVER["REMOTE_ADDR"];
$ipcheck1="SELECT * FROM `banned` WHERE ip='$ip' AND type='website'";

if(mysql_num_rows(mysql_query($ipcheck1)) > 0) {
die("You have been banned from the website."); }

if(!isset($_SESSION['db_logged']) || $_SESSION['db_logged'] !== true) {

session_start();
session_set_cookie_params($loginlength);

if (isset($_POST['username']) && isset($_POST['password'])) {

if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])) {

$username = $_COOKIE['cookname'];
$password = $_COOKIE['cookpass'];

}

else {

$username = catch(username);
$password = catch(password);

}


$sql = "SELECT userid, username, password, position, dispname
FROM tbl_auth_user
WHERE username = '$username'
AND password = PASSWORD('$password')";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

while($r = mysql_fetch_array($result)) {
$userid1 = $r['userid'];
$username1 = $r['username'];
$position1 = $r['position'];
$dispname1 = $r['dispname'];
$password1 = $r['password'];
}


if (mysql_num_rows($result) == 1 && $position1 == 1) {

if(isset($_POST['remember'])){
setcookie("cookname", $_POST['username'], time()+60*60*24*100, "/");
setcookie("cookpass", $_POST['password'], time()+60*60*24*100, "/");
$_SESSION['db_logged'] = true;
}

$_SESSION['db_logged'] = true;
$_SESSION['userid'] = $userid1;
$_SESSION['position'] = $position1;
$_SESSION['username1'] = (isset($_COOKIE['cookname']) ? $_COOKIE['cookname'] : $username1);
$_SESSION['password'] = (isset($_COOKIE['cookpass']) ? $_COOKIE['cookpass'] : $password1);
$_SESSION['dispname'] = $dispname1;


header("Location: $_SERVER[HTTP_REFERER]");

$error = '';
exit;
}


elseif(mysql_num_rows($result) == 1 && $position1 == 2) {

if(isset($_POST['remember'])){
setcookie("cookname", $_POST['username'], time()+60*60*24*100, "/");
setcookie("cookpass", $_POST['password'], time()+60*60*24*100, "/");
$_SESSION['db_logged'] = true;
}

$_SESSION['db_logged'] = true;
$_SESSION['userid'] = $userid1;
$_SESSION['position'] = $position1;
$_SESSION['username1'] = (isset($_COOKIE['cookname']) ? $_COOKIE['cookname'] : $username1);
$_SESSION['password'] = (isset($_COOKIE['cookpass']) ? $_COOKIE['cookpass'] : $password1);
$_SESSION['dispname'] = $dispname1;



header("Location: $_SERVER[HTTP_REFERER]");

$error = '';
exit;
}


else

{
$error = '<font color="#CC0000">Wrong Username Or Password</font>';
}
mysql_close();
} elseif ((isset($_SESSION['db_logged']) || $_SESSION['db_logged'] == true) && $position1 == 1) { header("Location: index.php?page=admin&userid=$userid1"); }
elseif ((isset($_SESSION['db_logged']) || $_SESSION['db_logged'] == true) && $position1 == 2) { header("Location: index.php?page=admin&userid=$userid1"); }

} else {

header("Location: $_SERVER[HTTP_REFERER]");

}

?>

Any help would be greatly appreciated. Login is based on member position (or ranking) too. 1 = admin, 2 = member. Thanks.

Element
01-08-2006, 09:59 PM
try making the cookie one cookie for one by compiling the data..




$cookie_data = $name . "|". $pass;
$cookie_expiration_time = time()+60*60*24*100;
setcookie("userinfo", $cookie_data, $cookie_time);







if(isset($_COOKIE['userinfo'])) {
$cookie_data = explode("|", $_COOKIE['userinfo']);
$username = $cookie_data[0];
$password = $cookie_data[1];
}

egbakaet
01-08-2006, 10:47 PM
Forget my script. It's not working at all. Can anyone point me in the direction of a good php login tutorial/script with databases and remember me features?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum