0

I'm currently trying to register a account with my website.

Description:

I'm trying to create a OOP based PHP registration for my website. The register.php file will submit a form, which triggers the PHP code. Now a new Register Class object will be created which extends the database class to register the user with the website.

My problem:

I receive the following error:

[Tue Sep 12 21:21:21.975670 2017] [proxy_fcgi:error] [pid 9111] [client xx.xx.xx.xx:33482] AH01071: Got error 'PHP message: PHP Warning:  mysqli::__construct(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in /var/www/vhosts/pr0b.com/httpdocs/paypal/classes/database.php on line 15\nPHP message: PHP Fatal error:  Uncaught Error: Call to a member function query() on boolean in /var/www/vhosts/pr0b.com/httpdocs/paypal/classes/database.php:30\nStack trace:\n#0 /var/www/vhosts/pr0b.com/httpdocs/paypal/classes/register.php(23): database->execute_query('SELECT * FROM u...')\n#1 /var/www/vhosts/pr0b.com/httpdocs/paypal/register.php(26): register->registerAccount()\n#2 {main}\n  thrown in /var/www/vhosts/pr0b.com/httpdocs/paypal/classes/database.php on line 30\n', referer: http://pr0b.com/paypal/register.php

Which does not make any sense to me, since I know the entered credentials inside the database class are correct.

Does anyone know what I'm doing wrong?

The PHP code:

Register.php:

<!doctype html>
<html>
<head>
<title>Title</title>
</head>
<body>
    <form method="post">
        <input type="text" name="username" placeholder="Username">
        <input type="password" name="password" placeholder="Password">
        <input type="text" name="email" placeholder="Email">
        <button type="submit">Register</button>
        <input type="button" onclick="location.href='index.php';" value="Or login" />
    </form>

    <?php
        if (!empty($_POST))
        {
            require_once('classes/database.php');
            require_once('classes/register.php');

            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];

            $register = new register($username, $password, $email);
            $register_account = $register->registerAccount();

            if($register_account == true)
            {
                echo 'Success, Your account: ' . $username . ' is registered. Please check your email to activate the account.';
            }
            else
            {
                echo 'Sorry the account: ' . $username . ' could not be registered. Please try again.';
            }
        }
    ?>
</body>
</html>

Register Class:

<?php
    class register extends database
    {
        function __construct($username, $password, $email)
        {
            $this->username = $username;
            $this->password = password_hash($password, PASSWORD_DEFAULT);
            $this->email = $email;
            $this->activation_id = $this->generateActivationId();
            $this->sender_email = 'support@pr0b.com';
            $this->activation_link = 'http://pr0b.com/paypal/activate.php?id=' . $this->activation_id;
        }

        function generateActivationId()
        {
            $generator = bin2hex(random_bytes(10));
            return $generator;
        }

        function registerAccount()
        {
            $this->connect();
            $user_lookup = $this->execute_query("SELECT * FROM users WHERE username = '" . $this->username . "'");

            if (mysqli_num_rows($user_lookup) > 0)
            {
                return false;
            }
            else
            {
                $this->execute_query("INSERT INTO users (username, password, email, activation_id) VALUES ('" . $this->username . "', '" . $this->password . "', '" . $this->email . "', '" . $this->activation_id . "')");
                $user_lookup_comfirm = $this->execute_query("SELECT * FROM users WHERE username = '" . $this->username . "'");

                if (mysqli_num_rows($user_lookup_comfirm) > 0)
                {
                    $this->sendRegisterEmail();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        function sendRegisterEmail()
        {
            $subject = 'Registration - Activate your account';
            $message = 'Thank you for registering. Please activate your account by visiting the following site: <a href="' . $this->activation_link . '">Website link</a>';
            $headers = 'From: ' . $this->sender_email . "\r\n" .
                'Reply-To: ' . $this->sender_email . "\r\n" .
                'X-Mailer: PHP/' . phpversion();

            mail($this->email, $subject, $message, $headers);
        }
    }
?>

Database Class:

<?php
    class database
    {
        function __construct()
        {
            $this->dBusername = 'xxx';
            $this->dBpassword = 'xxx';
            $this->dBhost = 'localhost';
            $this->dBdatabase = 'xxx';
            $this->dBcharset = 'utf8';
        }

        function connect()
        {
            $mysqli = new mysqli($this->dBhost, $this->dBusername, $this->dBpassword, $this->dBdatabase);

            if ($mysqli->connect_errno)
            {
                $this->_mysqli = false;
            }
            else
            {
                $mysqli->set_charset($this->dBcharset);
                $this->_mysqli = $mysqli;
            }
        }

        function execute_query($sql)
        {
            if($results = $this->_mysqli->query($sql))
            {
                return $results;
            }
            else
            {
                return false;
            }
        }
    }
?>
  • You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries. Specially since you're not escaping the user inputs at all! – M. Eriksson Sep 12 '17 at 19:34
  • Your error message says `Access denied for user ''@'localhost' (using password: NO)`. You indeed have not provided the correct credentials, while `(using password: NO)` is okay if you have not assigned a password, i do not think it is possible to have `''@'localhost'`. That means username is empty, you need atleast the username to connect to your database. – coderodour Sep 12 '17 at 19:36
  • 1
    Did you call parent::__construct in Register class. I don't think the DB constructor is being called and thus those variables are undefined. – pucky124 Sep 12 '17 at 19:43

0 Answers0