0

I am working on a university project which involves building a website that can access a database to run queries. The website in has a login system and, until tonight, was able to retrieve login details from a table called 'customers'. Now I have run into a strange problem whereby PHP gives the following error:

PHP Notice: Undefined index: customer_id in D:\Work\CEUR16-004\Project\www_root\includes\scripts\php\libCust\login_out.php on line 88

The table 'customers' exists, and the 'customer_id' field also exists, checked it in the SQL Server Management Studio. I get the same problem if I try calling any other fields from the 'customers' table, yet other tables that are called in other sections of the site work absolutely fine. I have tried var_dump and nothing gets returned, which doesn't make sense as no changes have been made to this class which would cause it to suddenly break.

Here is the code for the class:

<?php
// libCust/login_out.php
// This file contains the login/logout class(es) and functions required for the newsagents customers. Class(es) and functions related to the login/logout of admin accounts are located in libAdmin/login_out.php

class login_out
{
    // This class contains the functions required for customers to manage their account

    // Public Vars
    public $email;
    public $login_err;
    public static $user_id;
    // Protected Vars
    protected $dbcore_login_out;
    protected $session_id;
    // Private Vars
    private $password;
    private $err_handle;

    public function __construct($err_handle)
    {
        $this->err_handle = $err_handle;
        $this->dbcore_login_out = DBCore::DBInstance();
    }

    public function get_email()
    {
        return $this->email;
    }

    public function set_email($email_set)
    {
        $this->email = $email_set;
    }

    public function login_check($email_post, $password_post)
    {
        $this->email = trim($email_post);
        $this->password = trim($password_post);
        if($this->email == "" || $this->password == "")
        {
            $this->login_err = $this->err_handle->generate_error("login_form_empty_param", "user", "null");
            unset($this->email);
            unset($this->password);
            $_SESSION['login_err'] = $this->login_err;
            return 1;
        }
        elseif(filter_var($this->email, FILTER_VALIDATE_EMAIL) == "FALSE")
        {
            $this->login_err = $this->err_handle->generate_error("login_form_invalid_email", "user", "null");
            unset($this->email);
            unset($this->password);
            $_SESSION['login_err'] = $this->login_err;
            return 1;
        }
        else
        {
            try
            {
                $query = 'SELECT customer_password FROM customers WHERE customer_email = :customer_email';
                $stmt = $this->dbcore_login_out->dbc->prepare($query);
                $stmt->bindParam(':customer_email', $this->email);
                $stmt->execute();
                $user_fetch = $stmt->fetch();
                if(!($user_fetch))
                {
                    $this->login_err = $this->err_handle->generate_error("login_form_details_incorrect", "user", "null");
                    unset($this->email);
                    unset($this->password);
                    $_SESSION['login_err'] = $this->login_err;
                    return 1;
                }
                else
                {
                    if($user_fetch['customer_password'] !== $this->password)
                    {
                        $this->login_err = $this->err_handle->generate_error("login_form_details_incorrect", "user", "null");
                        unset($this->email);
                        unset($this->password);
                        $_SESSION['login_err'] = $this->login_err;
                        return 1;
                    }
                    else
                    {
                        session_destroy();
                        unset($this->password);
                        unset($_SESSION['login_err']);
                        self::$user_id = $user_fetch['customer_id'];
                        $session_prefix = preg_replace('/[^A-Za-z0-9]/', '', $this->email);
                        $this->session_id = session_create_id($session_prefix . "-");
                        session_id($this->session_id);
                        session_start();
                        $_SESSION['user_logged_in'] = "user_logged_in";
                        $_SESSION['customer_id'] = self::$user_id;
                    }
                }
            }
            catch(PDOException $e)
            {
                exit($this->err_handle->generate_error("login_db_error", "db", $e));
            }
        }
        return "success";
    }

    public function logout()
    {
        if(!(session_id()))
        {
            return "user_not_logged_in";
        }
        else
        {
            unset($this->email);
            unset($this->session_id);
            session_destroy();
            session_start();
            $_SESSION['user_logged_in'] = "";
        }
        return "user_logged_out";
    }
}
?>

Any help would be much appreciated.

tyrone 1988
  • 321
  • 2
  • 11
  • 1
    It exists in your database but you never select it – John Conde Jul 17 '17 at 22:26
  • 1
    It seems as it exist but you are not fetching it before using somewhere . – Muhammad Usman Jul 17 '17 at 22:27
  • It appears I need to be a bit more careful in the future, just realised I took out the line that gets the customer_id some time ago, and modified the SELECT statement to reflect that. I put that line back in tonight but forgot to modify the SELECT statement to fetch that field again. Cheers guys. – tyrone 1988 Jul 17 '17 at 22:37

0 Answers0