0

I have a simple login system on my project. I have a page after the user is logged in dashboard.php and a page for function Auth.php. On auth page I have this code to get user information stored into the database:

function getUserInformation($username,$useremail) {
        $db_handle = new DBController();
        $query = "SELECT * FROM cusers WHERE member_name = ? AND member_email = ?";
        $result = $db_handle->runQuery($query, 'ms', array($username, $useremail));
        return $result;
    }

Full Auth.php code is:

require "DBController.php";

class Auth {
    function getMemberByUsername($username) {
        $db_handle = new DBController();
        $query = "SELECT * FROM cusers WHERE member_name = ?";
        $result = $db_handle->runQuery($query, 's', array($username));
        return $result;
    }

    function getUserInformation($username,$useremail) {
        $db_handle = new DBController();
        $query = "SELECT * FROM cusers WHERE member_name = ? AND member_email = ?";
        $result = $db_handle->runQuery($query, 'ms', array($username, $useremail));
        return $result;
    }
    
    function getTokenByUsername($username,$expired) {
        $db_handle = new DBController();
        $query = "SELECT * FROM token_auth WHERE username = ? AND is_expired = ?";
        $result = $db_handle->runQuery($query, 'si', array($username, $expired));
        return $result;
    }
    
    function markAsExpired($tokenId) {
        $db_handle = new DBController();
        $query = "UPDATE token_auth SET is_expired = ? WHERE id = ?";
        $expired = 1;
        $result = $db_handle->update($query, 'ii', array($expired, $tokenId));
        return $result;
    }
    
    function insertToken($username, $random_password_hash, $random_selector_hash, $expiry_date) {
        $db_handle = new DBController();
        $query = "INSERT INTO token_auth (username, password_hash, selector_hash, expiry_date) VALUES (?, ?, ?,?)";
        $result = $db_handle->insert($query, 'ssss', array($username, $random_password_hash, $random_selector_hash, $expiry_date));
        return $result;
    }
    
    function update($query) {
        mysqli_query($this->conn,$query);
    }
}

On Dashboard.php page when put this line of code for display user information <?php require_once "Auth.php"; getUserInformation(); ?> I wanted to display Welcome, user_email. But i get this error Uncaught Error: Call to undefined function getUserInformation() How should i edit my code to display user information ( user email or user name ). Any example code will be good.

Thanks all

Marck
  • 3
  • 3
  • We can't conclude this based on the function code alone. Please show the contents of Auth so that we can see the scope to which this function belongs. – El_Vanja May 14 '21 at 21:14
  • Code is edited @El_Vanja – Marck May 14 '21 at 21:17
  • As I suspected, this function is part of a class. You can't access it directly like you would a regular, procedural-style function. You need to make an instance of the class and then call the function on it. See the manual on [OOP basics](https://www.php.net/manual/en/language.oop5.basic.php), which shows an example of how classes are used. – El_Vanja May 14 '21 at 21:17
  • Okay, i added this code ```$auth = new Auth(); $db_handle = new DBController();``` and get same error. Where i make mistake? Do i need to write ```$auth = new Auth(); $user = getUserInformation();``` or? @El_Vanja – Marck May 14 '21 at 21:21
  • You're still trying to call it procedurally. See example #2 in that link and how they call `$a->foo()`. – El_Vanja May 14 '21 at 21:23
  • You will, however, have to supply some parameters to it. Because you defined it as taking two, but you aren't passing any. – El_Vanja May 14 '21 at 21:24
  • Okay, I have a class named ```Auth``` and a function named ```getUserInformation``` on the dashboard.php page insert ```$auth = new getUserInformation(); $auth->getUserInformation();``` am i correct? @El_Vanja – Marck May 14 '21 at 21:27
  • No, `$auth = new Auth();` was right. Your class is not called `getUserInformation`, it's called `Auth`. As I said in the previous comment, you'll have to add parameters to your call: `$auth->getUserInformation(-- you need to place parameters in here --);`. – El_Vanja May 14 '21 at 21:33
  • I got you now I need to define the variable $username, should I need to get from the database desire row or? @El_Vanja – Marck May 14 '21 at 21:35
  • Wherever you get username and mail from (I assume user input), take those values and pass them as parameters. If your `DbController` is set up correctly, it should yield the desired row. – El_Vanja May 14 '21 at 21:40

1 Answers1

0

To access methods in a class, you first need to initialize that class somewhere.

$auth = new Auth;

Now you can access the public members and methods of that class.

$auth->getUserInformation('username', 'email');

You can learn more by reading up on PHPs classes and objects, or by viewing the example I gave on an 3v4l demo page.

Kim Hallberg
  • 1,165
  • 1
  • 9
  • 18