Jump to content


Photo

Login Utility not working


  • Please log in to reply
2 replies to this topic

#1 Cell0518

Cell0518
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts
  • LocationUSA / Japan (US Navy)

Posted 04 June 2006 - 04:09 PM

I'm piecing together a login utility that checks a MySQL database, and if you are found, it will set 2 session vars (a 3rd for user type, aka admin / regular, etc is commented). At this point, it keeps exiting the loop, not setting a session and never redirecting. The User not validated message keeps showing. I can't seem to figure out what I'm doing wrong. I've used a similar function before, but I can't figure what's wrong. Please help. [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

Thanks,
Chris
<!--HEADER GOES HERE-->
<?php
session_start();

if( $action=="login" && isset($_POST['username'])) {

    $username = $_POST['username'];

    $password = $_POST['password'];
    // MySQL Connection
    require "config.php";
    $conn = mysql_connect("$dblocation","$dbusername","$dbpassword");

       if (!$conn) die ("Could not connect MySQL"); {

       mysql_select_db($dbname,$conn) or die ("Could not open database");

       $query = "SELECT user_name, user_group FROM `db1` WHERE user_name = '$username' AND user_password = PASSWORD('$password')";

      $result = mysql_query($query);
    if (mysql_num_rows($result) == 1) {

    // the user name and password match,
    $_SESSION['user'] = $username;

    $_SESSION['loggedIn'] = true;
    /*while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    $userGroup = $row['user_group'];
    echo $userGroup;

    session_register("userGroup"); //is this really needed?

    $_SESSION['userGroup'] = $userGroup;    
    }*/
    header("Location: ".$url);

    }
    }

    // nothing became valid while we looped so verification failed

    echo "<b>Incorrect Username or Password</b><p />";
}
elseif($action=="logout") {
    session_start();

    session_unset();

    session_destroy();



    echo '<b>You have successfully logged out!</b><p />';

}
else {}

?>

<form action="admin.php?action=login" method="post">

    <p>Username: <input type="text" name="username"><br>

    <p>Password : <input type="password" name="password"><br>

    <input type="submit" value=" Login ">

</form>
<!-- REST OF FOOTER-->

Chris

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 June 2006 - 05:55 PM

Change this code:
$_SESSION['loggedIn'] = true;
    /*while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    $userGroup = $row['user_group'];
    echo $userGroup;

    session_register("userGroup"); //is this really needed?

    $_SESSION['userGroup'] = $userGroup;    
    }*/
    header("Location: ".$url);
to the following:
$_SESSION['loggedIn'] = true;
    $row = mysql_fetch_array($result);

    $_SESSION['userGroup'] = $row['user_group'];

    header("Location: ".$url);
Also I see yoou have the following:
<!--HEADER GOES HERE-->
<?php
Is that going to be HTML where <!--HEADER GOES HERE--> currently is? If it is then session_start will fail to work and your header redirect will fail too. As these function require no output before the use of those functtions.

#3 Cell0518

Cell0518
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts
  • LocationUSA / Japan (US Navy)

Posted 04 June 2006 - 10:48 PM

Wildteen88,

It worked, thanks for you help. :)

CL
Chris




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users