Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-25-2011, 11:09 AM   PM User | #1
HeathP123
New Coder

 
Join Date: Nov 2011
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
HeathP123 is an unknown quantity at this point
login code not working properly

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:
Code:
<?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:
Code:
<?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:
Code:
	<?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");
        }
        ?>
HeathP123 is offline   Reply With Quote
Old 11-25-2011, 03:36 PM   PM User | #2
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
At a glance looks like the first

PHP Code:
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)
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:59 AM.


Advertisement
Log in to turn off these ads.