Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:16+00:00 2026-05-27T14:52:16+00:00

I am trying to set up a PHP members area on my web server,

  • 0

I am trying to set up a PHP members area on my web server, which has hashed and salted passwords. I registered an account and checked the database – the user is created as expected – everything seemed fine. The problem is whenever I try to log in it says the login is incorrect (username or password). Of course this is being done in a test area, and the user name and password I registered with should work – but doesn’t.

auth.php: should authenticate the user and create a session if the details are valid
//login never works

<?php
session_start();

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

if (strlen($username)<3 || strlen($password)<3) {
    header('Location: log_in.php5?error=1');
    die();
}

$username = mysql_real_escape_string($username);

function validateUser() {
    session_regenerate_id ();
    $_SESSION['valid'] = 1;
    $_SESSION['userid'] = $username;
}

include "../db_login.php5";
mysql_select_db("DB_NAME", $con);

$query = "SELECT password, salt
        FROM users
        WHERE username = '" . $username . "';";

$result = mysql_query($query);

$userData = mysql_fetch_assoc($result);
$count = mysql_num_rows($userData);

if ($count < 1) {
    header('Location: log_in.php5?error=1');
    die();
}

$salt = "";
$hashed_pass = "";

for ($x=0; $x<1; $x++) {
  $salt = $userData['salt'];
  $hashed_pass = $userData['password'];
}

$hash = hash('sha256', $salt . hash('sha256', $password));

mysql_close($con);

if ($hash != $hashed_pass) {
    header('Location: log_in.php5?error=1');
    die();
} else {
    validateUser();
}

header('Location: members.php5');
?>

register.php: should take the username, hash the password with a salt, store all 3 to DB //works

<?php
//retrieve our data from POST
$username = $_POST['username'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($pass1 != $pass2)
    header('Location: sign_up.php5');
if(strlen($username) > 30)
    header('Location: sign_up.php5');

$hash = hash('sha256', $pass1);

function createSalt(){
    $string = md5(uniqid(rand(), true));
    return substr($string, 0, 3);
}

$salt = createSalt();
$hash = hash('sha256', $salt . $hash);

include "../db_register.php5";
mysql_select_db("DB_NAME", $con);

//sanitize username
$username = mysql_real_escape_string($username);
$query = "INSERT INTO users (username, password, salt)
        VALUES ( '$username' , '$hash' , '$salt' );";
mysql_query($query);
mysql_close($con);

header('Location: log_in.php5');
?>

The login.php file is a simple form which posts the data to auth.php;
the sign_up.php file is also a simple form which posts the user data to register.php.

What is wrong with my code that it wont allow me to log in?

EDIT:

So I’ve narrowed the problem down so the $username variable which seems to be blank from the start?

output:

Count: 
User: 
Query: SELECT * FROM `users` WHERE `username`='';

code for output:

$query = "SELECT * 
FROM `users` 
WHERE `username`='".$username."';";

$result = mysql_query($query);

$userData = mysql_fetch_assoc($result);
$count = mysql_num_rows($userData);

if ($count < 1) {
    echo "Count: ".$count."<BR/>"
    . "User: " .$username ."<BR/>"
    . "Query: " .$query;
    //header('Location: log_in.php5?error=1,count='.$count.'user='.$username);
    //die();
}

$count and $username are not printed in either Address nor the sql query

EDIT 2:

*Jared* eventually found the problems which were two in number:
1) $username = mysql_real_escape_string($username); <-- required a db connection $con in my case
2) $count = mysql_num_rows($userData); <-- required to pass the $result instead.

Thanks everyone!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:52:16+00:00Added an answer on May 27, 2026 at 2:52 pm

    What’s with this part?

    for ($x=0; $x<1; $x++) {
      $salt = $userData['salt'];
      $hashed_pass = $userData['password'];
    }
    

    Do a var_dump of $userData.

    EDIT

    function validateUser($username) {
        session_regenerate_id ();
        $_SESSION['valid'] = 1;
        $_SESSION['userid'] = $username;
    }
    

    and near the bottom:

    if ($hash != $hashed_pass) {
        header('Location: log_in.php5?error=1');
        die();
    } else {
        validateUser($username);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set up a PHP path to my Zend Framework. I
I'm trying to set up some exotic PHP code (I'm not an expert), and
I'm trying to set up a batch file to automatically deploy a php app
I'm painfully new to PHP, and was trying to set up phpBB on my
Trying to learn about php's arrays today. I have a set of arrays like
Basically am trying to set a time and a date in PHP then set
I'm trying to set up a quick PHP demo using the Youtube API that
I'm trying to set up a simple PHP contact form for a website and
I am trying to set text when loading a php file... I am doing
I am trying to set up a basic Facebook application using PHP and an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.