Jump to content

Recommended Posts

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
[code]
<!--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-->
[/code]
Link to comment
https://forums.phpfreaks.com/topic/11165-login-utility-not-working/
Share on other sites

Change this code:
[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);[/code]
to the following:
[code]$_SESSION['loggedIn'] = true;
    $row = mysql_fetch_array($result);

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

    header("Location: ".$url);[/code]
Also I see yoou have the following:
[code]<!--HEADER GOES HERE-->
<?php[/code]
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.