6 Replies - 318 Views - Last Post: 26 February 2018 - 11:11 AM Rate Topic: -----

#1 hcis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-December 17

password hint in php

Posted 23 February 2018 - 11:31 PM

Hi there! :)

I'm writing a code on how to get the password hint if user entered invalid credentials. But i don't know how to fetch it in the database. Here's my code:
<?php
	include 'db/config.php';
    session_start();
	if(ISSET($_POST['login'])){
		$username = $_POST['username'];
		$password = $_POST['password'];
		
        $conn = connection();
		$query = $conn->query("SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password' && is_deleted = 0") or die(mysqli_error($conn));
		$fetch = $query->fetch_array();
		$valid = $query->num_rows;
		if($valid > 0)
        {
            if($fetch['permission'] == 'admin'){
                $conn->query("INSERT INTO `userlog`(`user_id`, `username`, `action`, `from_section`)
                            VALUES ('$fetch[user_id]','$fetch[username]','Login as Admin','Login')")or die($conn->error);
                $_SESSION['admin_id'] = $fetch['user_id'];
                $_SESSION['username'] = $fetch['username'];
                echo "<script>window.location = 'home.php'</script>";
            }
            elseif($fetch['permission'] == 'user'){
                $conn->query("INSERT INTO `userlog`(`user_id`, `username`, `action`, `from_section`)
                            VALUES ('$fetch[user_id]','$fetch[username]','Login as User','Login')") or die($conn->error);
                $_SESSION['user_id'] = $fetch['user_id'];
                $_SESSION['username'] = $fetch['username'];
				echo "<script>alert('Your hint to your email is  and your password hint is ')</script>";
                echo "<script>window.location = 'home.php'</script>";
            }
		}else{
				echo "<script>alert('Your hint to your username is  and your password hint is  ')</script>";
				echo "<script>window.location = 'index.php'</script>";
			 }

    }else{
				echo "<script>alert('Account Does Not Exist!')</script>";
				echo "<script>window.location = 'index.php'</script>";
         }
			


I don't know what to put in this script
echo "<script>alert('Your hint to your username is _________  and your password hint is ___________')</script>"; 
to get the data...

db: user
tables: username, password, userHint, userPass

Please help me. Got any other way to show the hints? Please teach me. Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: password hint in php

#2 RamonRobben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 87
  • View blog
  • Posts: 559
  • Joined: 19-May 14

Re: password hint in php

Posted 24 February 2018 - 05:13 AM

You have to make a query that selects the userHint, userPass where the username is the one the user filled in and where the password is the one that the user filled in. so something like this:
SELECT userHint, userPass FROM user WHERE username == :username && password == :password


where :username and :password are the things the user filled in.
After that fetch the results and see if its not empty and then show it.
You can use the fetch like $fetch['userHint']; and $fetch['userPass']; to put onto those lines.

echo "<script>alert('Your hint to your username is ".$fetch['userHint'];."  and your password hint is ".$fetch['userPass'];."')</script>";


I'm not sure if this works but I think it somthing like this would do.

This post has been edited by RamonRobben: 24 February 2018 - 05:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2481
  • View blog
  • Posts: 9,958
  • Joined: 03-December 12

Re: password hint in php

Posted 24 February 2018 - 06:34 AM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.
Was This Post Helpful? 1
  • +
  • -

#4 hcis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-December 17

Re: password hint in php

Posted 25 February 2018 - 12:43 PM

View PostRamonRobben, on 24 February 2018 - 05:13 AM, said:

You have to make a query that selects the userHint, userPass where the username is the one the user filled in and where the password is the one that the user filled in. so something like this:
SELECT userHint, userPass FROM user WHERE username == :username && password == :password


where :username and :password are the things the user filled in.
After that fetch the results and see if its not empty and then show it.
You can use the fetch like $fetch['userHint']; and $fetch['userPass']; to put onto those lines.

echo "<script>alert('Your hint to your username is ".$fetch['userHint'];."  and your password hint is ".$fetch['userPass'];."')</script>";


I'm not sure if this works but I think it somthing like this would do.



it's not working :(
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2481
  • View blog
  • Posts: 9,958
  • Joined: 03-December 12

Re: password hint in php

Posted 25 February 2018 - 12:54 PM

echo "<script>alert('Your hint to your username is ".$fetch['userHint'];."  and your password hint is ".$fetch['userPass'];."')</script>";


I'll break this down.
$fetch['userHint']; // the semi-colon ends the statement, so nothing that follows will work, and will likely throw an error. The same would go for the next variable.

echo "<script>alert('Your hint to your username is {$fetch['userHint']} and your password hint is {$fetch['userPass']}')</script>";


This does away with those issues.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3777
  • View blog
  • Posts: 13,680
  • Joined: 08-August 08

Re: password hint in php

Posted 25 February 2018 - 12:58 PM

What's your site's URL? Bobby Tables want's to access it.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1994
  • View blog
  • Posts: 6,072
  • Joined: 15-January 14

Re: password hint in php

Posted 26 February 2018 - 11:11 AM

You're just asking about a basic select query - select data from the database and display it. What about that don't you know how to do? It's a pretty basic part.

One question you need to answer for yourself is how you figure out which user they are. If they don't know their username and need a hint, what else do they type in to look them up? Are you keeping the email address also? If they click on the link to recover their username, what do you ask them to type? For the password hint you can ask for their username, that's fine, but if they don't know their username you need something else they can type in to look themselves up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1