Jump to content

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

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.