Jump to content

[SOLVED] Login hashing


aashcool198

Recommended Posts

I have written a code to register users.It uses md5 before saving the password.

 

$password = MD5($_POST['password']);

 

But when user logs in i need to compare the password in database with password he writes. so i convert the password entered by user in hash

 

$password = md5 ($_POST['password']);

 

 

and now i compare this with passwords in database.

But despite entering the correct password its not logging in.

 

Please help!

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/159655-solved-login-hashing/
Share on other sites

<?php

 

// Open a connection to the DB

$conn = mysql_connect('localhost', 'root', '') or die(mysql_error());

mysql_select_db('Sumeru Skills', $conn);

 

// Start the session (DON'T FORGET!!)

session_start();

 

// Check if user wants to login (GET info)

if(isset($_GET['try'])) {

 

// That's nice, user wants to login. But lets check if user has filled in all information

If(empty($_POST['username']) OR empty($_POST['password'])) {

 

// User hasn't filled it all in!

echo 'Please fill in all the required fields!';

 

} else {

 

// User filled it all in!

 

// Make variables save with addslashes and md5

$username = $_POST['username'];

$password = md5 ($_POST['password']);

 

// Search for a combination

$query = mysql_query("SELECT login_id FROM login

  WHERE username = '" . $username . "'

  AND password = '" . $password . "'

  ") or die(mysql_error());

 

// Save result

list($user_id) = mysql_fetch_array($query);

 

// If the user_id is empty no combination was found

if(empty($user_id)) {

 

echo 'No combination of username and password found.';

 

} else {

 

// the user_id variable doesn't seem to be empty, so a combination was found!

 

// Create new session, store the user id

$_SESSION['user_id'] = $user_id;

 

// Redirect to userpanel.php

header('location: userpanel.php');

 

}

 

}

 

}

 

?>

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

 

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

<br>

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

<br>

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

 

</form>

 

 

Link to comment
https://forums.phpfreaks.com/topic/159655-solved-login-hashing/#findComment-842085
Share on other sites

Made a few changes, quick edits as its late and i'm heading to bed. It may or may not work as i can't test it.

 

Changed AND from the query to && and added brackets around both WHERE clauses

removed a space between md5 and (

 

<?php

// Open a connection to the DB
$conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('Sumeru Skills', $conn);

// Start the session (DON'T FORGET!!)
session_start();

// Check if user wants to login (GET info)
if(isset($_GET['try'])) {

   // That's nice, user wants to login. But lets check if user has filled in all information
   if(empty($_POST['username']) || empty($_POST['password'])) {
   
      // User hasn't filled it all in!
      echo 'Please fill in all the required fields!';
   
   } else {

      // User filled it all in!

      // Make variables save with addslashes and md5
      $username = $_POST['username'];
      $password = md5($_POST['password']);

      // Search for a combination
      $query = mysql_query("SELECT login_id FROM login
                  WHERE (username = '" . $username . "' && password = '" . $password . "')
                 ") or die(mysql_error());

      // Save result
      list($user_id) = mysql_fetch_array($query);

      // If the user_id is empty no combination was found
      if(empty($user_id)) {

         echo 'No combination of username and password found.';

      } else {
      
         // the user_id variable doesn't seem to be empty, so a combination was found!

         // Create new session, store the user id
         $_SESSION['user_id'] = $user_id;

         // Redirect to userpanel.php
         header('location: userpanel.php');
      }      
   }
}

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

Username: <input type="text" name="username"><br>
<br>
Password: <input type="password" name="password"><br>
<br>
<input type="submit" value="Login!">

</form>

Link to comment
https://forums.phpfreaks.com/topic/159655-solved-login-hashing/#findComment-842088
Share on other sites

Archived

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

×
×
  • 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.