Jump to content

Recommended Posts

I am trying to migrate to a newer version of PHP and am having a problem with the following code

 

function auth($username, $password) {

// hash password using md5 encryption
$hash_pass = md5($password);

// prepare SQL query
$username = mysqli_real_escape_string($username);

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


if ($result = mysqli_query($Connection, $query) or die (mysqli_error()." (query not executed)")) {
if (mysqli_num_rows ($Connection, $result) > 0) {
// record exits
if ($row = mysqli_fetch_assoc($result) or die (mysqli_error())) {

if ($hash_pass == $row['user_password']) {
// password is valid

// setup sesson
session_start();

$_SESSION['username'] = $username;
$_SESSION['CMS_AUTH'] = "YES";

return true;
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}
}

 

Can anybody please point me in a direction from here?

config.php

functions_security.php

login.php

With the mysqli_* functions most require the mysqli instance (in your case $Connection) to be passed to them when they are called. This you are doing. The problem is you are using mysqli instance ($Connection) inside your own function.

 

Functions have their own variable scope. Meaning, variables that are defined outside of them function is not available within the function. To get around this you need to pass $Connection as another argument to the auth function when you call it (Or be lazy and define it as global) .

 

This is how your function should be defined in functions_security.php

function auth($Connection, $username, $password) {

Then when calling the function in login.php

	$auth = auth($Connection, $username, $password);

Other changes you need to make in functions_security.php are

Line 11 needs to be changed to use mysqli_real_escape_string

	$username = mysqli_real_escape_string($Connection, $username);

mysqli_error requires the mysqli instance ($Connection) when using it. eg mysqli_error($Connection)

 

You should not use md5 for hashing passwords, instead you should use PHP's password_hash library for hashing/checking users passwords (if you are not using PHP5.5 then use this password compatibility library).

  • Like 1
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.