Using xampp for local development, I was able to build a very simple authentication for accessing a webpage. Everything worked fine on my local machine but I ran into some issues while testing it on my live server.
Instead of creating the database in phpMyAdmin, I had to use the MySQL Databases tool in my cPanel. No big deal, I guess, just create the myLogin database, then go into phpMyAdmin to add the myUsers table with ID, username, and password, and finally insert the user 'admin' with a hashed password.
Issue 1: I guess it only makes sense, but the following resulted in an error:
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'myLogin';
Failed to connect to MySQL: Access denied for user 'root'@'localhost' (using password: NO)
So, I opened mySQL Database again and added a user with a password and attached it to the myLogin database. I edited the code in the auth.php file to reflect the changes and... no error! However, no nothing.
Issue 2: After I enter the password and hit enter the browser just goes to the auth.php file and does nothing. No echoes, no redirect upon success, nothing. Below is the entire auth.php file:
<?php
session_start();
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'myUser';
$DATABASE_PASS = 'myPass';
$DATABASE_NAME = 'myLogin';
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if ( !isset($_POST['uName'], $_POST['pWord']) ) {
die ('Please fill both the username and password field!');
}
if ($stmt = $con->prepare('SELECT id, password FROM myUsers WHERE username = ?')) {
$stmt->bind_param('s', $_POST['uName']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
if (password_verify($_POST['pWord'], $password)) {
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['uName'];
$_SESSION['id'] = $id;
header('Location: ../wica.php');
} else {
echo 'Incorrect password!';
}
} else {
echo 'Incorrect username!';
}
$stmt->close();
}
?>
As I mentioned, I am a complete PHP newbie, this has all been cobbled together thanks to various tutorials. I don't know why it won't even echo "Incorrect password!" when I type the wrong password. It just sits on auth.php and does nothing and I'm clueless. This is my first time using mySQL on the remote server. Could there be critical settings that aren't the same as my xampp setup? Did I miss an important step somewhere?
Also, it seems a little insecure to have the DATABASE_USER and DATABASE_PASS just sitting in the auth.php file for anyone to see. Was I not supposed to do this? What are the secure alternatives, or are there any?