Aaron Rotem Aaron Rotem - 5 months ago 18
MySQL Question

Login script issue with database

I wrote a login script for a website that I am building using resources I have found online. When I ran my code on a local server it worked fine but now that I am actually running it online on a real server it doesn't work. I think I have narrowed down my error but with being new to PHP and not having prior experience with MySql I can't really fix my problem. This is the file for the login script:

//login file
<?php
class Login{
private $db_connection = null;

public function __construct(){
session_start();
$this->dologinWithPostData();
}

private function dologinWithPostData(){

$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (!$this->db_connection()->connect_errno) {

// escape the POST stuff
$email = $_POST['email'];
// database query, getting all the info of the selected user (allows login via email address in the
// username field)
$sql = "SELECT email, password
FROM users
WHERE email = '" . $email ."'";

$result_of_login_check = $this->db_connection->query($sql);//This is 0

// if this user exists
if ($result_of_login_check->num_rows == 1) {
// get result row (as an object)
$result_row = $result_of_login_check->fetch_object();
// using PHP 5.5's password_verify() function to check if the provided password fits
// the hash of that user's password
if ($_POST['password'] == $result_row->password) {
// write user data into PHP SESSION (a file on your server)
$_SESSION['email'] = $result_row->email;
$_SESSION['user_login_status'] = 1;
} else {
$this->errors[] = "Wrong password. Try again.";
$_SESSION['user_login_status'] = 0;
}
} else {
$this->errors[] = "This user does not exist.";
}
} else {
$this->errors[] = "Database connection problem.";
}
}

print_r($this->errors);
}

public function isUserLoggedIn()
{
if (isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] == 1) {
return true;
}
// default return
return false;
}
}

?>


I run it in another file that is essentially the following:

//Run file
require_once("dbconfig.php");
include_once("login.php");
$login = new Login();
if($login->isUserLoggedIn() == true){
//go to another page }


The variables used to access the database are instantiated in
dbconfig.php
and are correct. With this code I get an error that says the page is not working and is unable to handle the request. When I comment out the line

if (!$this->db_connection()->connect_errno) {


and the else statement following it, the output is "This user does not exist". So I think the error has something to do with
$this->db_connection()->connect_errno)
. If you can find where I went wrong or have any advice on how to rewrite the script to make it better, it is greatly appreciated.

Answer

This is a database establishing error your live remote server database configuration is different.Please verify you dbconfig.php file make sure database name, host , port , username , password are well defined with your live database

Comments