...

View Full Version : login code not working properly



HeathP123
11-25-2011, 12:09 PM
I had my login working fine until I tried to add a remember me...now the page is loading my incorrect login page even when i enter a correct user/login...here is my code:

Login.php:

<?php

include 'functions.php';


$username = $_POST['username'];
$password = $_POST['password'];
$rememberme = $_POST['rememberme'];

if (loggedin())
{
header("Location: members.php");
exit();

}


if ($username&&$password)
{

$login = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($login);

if ($numrows!=0)
{

while ($row = mysql_fetch_assoc($login))
{
$dbpassword = $row['password'];
$dbusername = $row['username'];
if ($username==$dbusername&&$password==$dbpassword)
{
$loginok = TRUE;
header("Location: members.php");
exit();

if ($loginok==TRUE)
{
if ($rememberme=="on")
setcookie("username", $username, time()+7200);
else if ($rememberme=="")
$_SESSION['username']=$username;

header("Location: members.php");
exit();
}
}
else
$loginok = FALSE;
header("Location: incorrect.html");

}
}
else
header("Location: incorrect.html");
}
else
header("Location: incorrect.html");


?>



functions.php:

<?php

session_start();

mysql_connect("localhost","root","*****") or die ("Couldn't connect!");
mysql_select_db("phplogin") or die ("Couldn't find db");

function loggedin()
{
if (isset($_SESSION['username'])||isset($_COOKIE['username']))

{
$loggedin = TRUE;
return $loggedin;

}

}
?>



members.php:

<?php
session_start();
include 'functions.php';

if (!loggedin())
{
header("Location: login.php");
exit();

if ($_SESSION['username'])
{
echo "HELLO, ".$_SESSION['username']."!";
}
else header ("Location: index.html");
}
?>

timgolding
11-25-2011, 04:36 PM
At a glance looks like the first



header("Location: members.php");
exit();


happens before the login session has been set. Try removing this and only have it inside the if ($loginok==TRUE)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum