Hi
I am trying out a script for a login but it appears the prepared statement is not prepared correctly. The thing is it worked fine on my local machine but fails when uploaded to live server. I have added some echoes to assist me in debugging and changed the database credentials. Maybe I messed the syntax up somehow but Ive checked and rechecked and cannot see why it is failing. It connects OK but fails at the point it prepares the statement to authenticate the user / pass. Can anyone see what is wrong, thanks.
Here is the on-screen result of my echoes:-
PHP version is: 8.0.11
About to start session
Session started
Connecting
Connected
Proceeding to authenticate submitted credentials
Preparing statement
Statement not prepared ok
and here is my code:-
<?php
echo 'PHP version is: ' . phpversion().'<br>';
echo 'About to start session<br>';
session_start();
echo 'Session started<br>';
// Change this to your connection info.
$DATABASE_HOST = 'myhost';
$DATABASE_USER = 'myuser';
$DATABASE_PASS = 'mypass';
$DATABASE_NAME = 'mydbname';
// Try and connect using the info above.
echo 'Connecting<br>';
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
else {
echo 'Connected<br>';
}
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
// Could not get the data that should have been sent.
exit('Please fill both the username and password fields!');
}
echo 'Proceeding to authenticate submitted credentials<br>';
// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
echo 'Preparing statement<br>';
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
echo 'Binding parameters<br>';
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// Account exists, now we verify the password.
// Note: remember to use password_hash in your registration file to store the hashed passwords.
if (password_verify($_POST['password'], $password)) {
// Verification success! User has logged-in!
// Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
// Original simple welcome with session name
// echo 'Welcome ' . $_SESSION['name'] . '!';
header('Location: home.php');
} else {
// Incorrect password
echo 'Incorrect username and/or password!';
}
} else {
// Incorrect username
echo 'Incorrect username and/or password!';
}
$stmt->close();
}
else {
echo 'Statement not prepared ok';
}
?>