Jump to content

Archived

This topic is now archived and is closed to further replies.

unenergizer

Need Help with my Login Script

Recommended Posts

Ok.. Im teaching my self on how to make different things in php. Well, I am having trouble with my script and I have no clue where to start looking for errors...

[code]<? //login.php
session_start();  // Start Session

include 'config.php';
include 'header.php';

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

mysql_query("SELECT * FROM user WHERE userid='$userid'");
$_SESSION['userid'] = $userid;

if((!$username) || (!$password)){
    echo "Please enter ALL of the information!<br />";
    include 'login_form.html';
    exit();
}

// Convert password to md5 hash
$password = md5($password);

$sql = mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password' AND userlevel='$userlevel'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
    while($row = mysql_fetch_array($sql)){
    foreach( $row AS $key => $val ){
        $key = stripslashes( $val );
    }
        // Register some session variables!
        session_register('username');
        $_SESSION['username'] = $username;
        session_register('password');
        $_SESSION['password'] = $password;
        session_register('userlevel');
        $_SESSION['userlevel'] = $userlevel;

        mysql_query("UPDATE user SET last_login=now() WHERE userid='$userid'");

        header("Location: members.php");
    }
} else {
    echo "You could not be logged in! The username and password do not match! Please try again!<br />";
    include 'login_form.html';
}
?>[/code]

I know that I am asking help with something very simple, but I am learning. And any help as to why my script doesn't work would greatly help me understand what is going on.

Thanks!
unenergizer

Share this post


Link to post
Share on other sites
Session Start needs to be the first thing.

[code]
<? //login.php

session_start();

include 'config.php';

[/code]

Start with that

Share this post


Link to post
Share on other sites
[!--quoteo(post=361668:date=Apr 4 2006, 02:12 PM:name=jvrothjr)--][div class=\'quotetop\']QUOTE(jvrothjr @ Apr 4 2006, 02:12 PM) [snapback]361668[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Session Start needs to be the first thing.

[code]
<? //login.php

session_start();

include 'config.php';

[/code]

Start with that
[/quote]

I know that... But everytime i try and log in, im getting my error message saying that the username and password is wrong. Did i write this incorrectly or something? Because im stupmted... There are not any syntax errors, I just cant log in. And all my myql tables names and everything else are 100% correct as i've looked those over time and time agin.....

Can anyone suggest or does anyone know what is wrong with my code???

thank you...
unenergizer

Share this post


Link to post
Share on other sites
Try getting the md5 of the password you are sending to the table and get what is stored in the table and double check to make sure they are the same thing. If you used different encryption then they are not going to match and your login will of course fail. Check that then you can worry about the code being wrong.

Share this post


Link to post
Share on other sites
i can't see you define $userid (first query) and $userlevel (second query) anywhere ?

Share this post


Link to post
Share on other sites

×

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.