#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Philippines
    Posts
    66
    Rep Power
    3

    OOP : Fatal Error


    Hello guys, every time I try to login I always get
    Code:
    Fatal error: Call to a member function login() on a non-object in *\phase2.php on line 31
    which the code is

    PHP Code:
    if($user->login($email,$user,$pass)) {
                        
    $user->redirect('home.php');
                    } else {
                        echo 
    '<font color="#F53939">Oops! Error</font><br />';
                    } 
    the user.inc.php code:

    PHP Code:
    public function login($uemail$uname$upass) {
                try {
                    
    $logProc $this->db->prepare("SELECT * FROM users WHERE username = :uname OR email = :uemail LIMIT 1");
                    
    $logProc->execute(array(':uname' => $uname':uemail' => $uemail));
                    
    $logRow $logProc->fetch(PDO::FETCH_ASSOC);
                    if(
    $logProc->rowCount() > 0) {
                        if(
    password_verify($upass$logRow['password'])) {
                            
    $_SESSION['user_session'] = $logRow['id'];
                            return 
    true;
                        } else {
                            return 
    false;
                        }
                    }
                } catch(
    PDOException $e) {
                    echo 
    $e->getMessage();
                }
            } 
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,976
    Rep Power
    9615
    $user is not a user (or whatever it's called) object. You probably didn't define it at all.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Philippines
    Posts
    66
    Rep Power
    3
    Originally Posted by requinix
    $user is not a user (or whatever it's called) object. You probably didn't define it at all.
    Oops yes I forgot to include on my post. This is the user I defined on my db.config.php

    PHP Code:
           //DB Connection here

        
    include_once 'user.inc.php';
        
    $user = new user($conn); 
  6. #4
  7. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,976
    Rep Power
    9615
    Corollary: it must be defined in the same scope it is used.

    I see your code is indented. Is it inside a function or class method? Variables defined outside of functions are not available inside of functions.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Philippines
    Posts
    66
    Rep Power
    3
    Originally Posted by requinix
    Corollary: it must be defined in the same scope it is used.

    I see your code is indented. Is it inside a function or class method? Variables defined outside of functions are not available inside of functions.
    This is my full code...

    user.inc.php
    PHP Code:
    <?php
        
    class user {

            function 
    __construct($conn) {
                
    $this->db $conn;
            }

            public function 
    randomPass() {
                
    $seed str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ' '0123456789');
                
    shuffle($seed);
                
    $rand '';
                foreach (
    array_rand($seed10) as $k$rand .= $seed[$k];
                return 
    $rand;
            }

            public function 
    emailCheck($uemail) {
                try {
                    
    $ecProc $this->db->prepare("SELECT * FROM users WHERE email = :uemail LIMIT 1");
                    
    $ecProc->execute(array(':uemail'=>$uemail));
                    
    $ecRow $ecProc->fetch(PDO::FETCH_ASSOC);
                    if(
    $ecProc->rowCount() > 0) {
                        return 
    true;
                    } else {
                        return 
    false;
                    }
                } catch(
    PDOException $e) {
                    echo 
    $e->getMessage();
                }
            }

            public function 
    emailRequest($uemail) {
                try {

                    
    $proc $this->db->prepare("SELECT * FROM users WHERE email = :uemail LIMIT 1");
                    
    $proc->execute(array(':uemail'=>$uemail));
                    
    $userRow $proc->fetch(PDO::FETCH_ASSOC);
                    if(
    $proc->rowCount() > 0) {
                        
    $seed str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ' '0123456789');
                        
    shuffle($seed);
                        
    $rand '';
                        foreach (
    array_rand($seed10) as $k$rand .= $seed[$k];
                        
    //password_hash($upass, PASSWORD_DEFAULT);
                        
    $change $this->db->prepare("UPDATE users SET password = :rand WHERE email = :uemail");
                        
    $change->bindparam(":rand"$rand);
                        
    $change->bindparam(":uemail"$uemail);
                        
    $change->execute(); 

                        return 
    true
                    } else {
                        return 
    false;
                    }
                } catch (
    PDOException $e) {
                    echo 
    $e->getMessage();
                }
            }

            public function 
    login($uemail) {
                return 
    true;
                try {
                    
    $logProc $this->db->prepare("SELECT * FROM users WHERE username = :uname AND email = :uemail LIMIT 1");
                    
    $logProc->execute(array(':uname' => $uname':uemail' => $uemail));
                    
    $logRow $logProc->fetch(PDO::FETCH_ASSOC);
                    if(
    $logProc->rowCount() > 0) {
                        if(
    password_verify($upass$logRow['password'])) {
                            
    $_SESSION['user_session'] = $logRow['id'];
                            return 
    true;
                        } else {
                            return 
    false;
                        }
                    }
                } catch(
    PDOException $e) {
                    echo 
    $e->getMessage();
                }
            }

            public function 
    is_loggedin() {
                if(isset(
    $_SESSION['user_session'])) {
                     return 
    true;
                }
            }

        }
    ?>
    db.inc.php
    PHP Code:
    <?php

        session_start
    ();

        
    $db_host "localhost";
        
    $db_user "root";
        
    $db_pass "";
        
    $db_name "examples";

        try {

            
    $conn = new PDO("mysql:host={$db_host};dbname={$db_name}"$db_user$db_pass);
            
    $conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);

        } catch(
    PDOException $e) {
            echo 
    $e->getMessage();
        }

        include_once 
    'user.inc.php';
        
    $user = new user($conn);
    ?>
    login2.php

    PHP Code:
    <?php
        
    require_once './_core/db.inc.php';
        if(!isset(
    $_GET['emailRequest'])) {
            
    header('Location: index.php');
            exit;
        } else {
            
    $emailRequest $_GET['emailRequest'];
            
    $user->emailCheck($emailRequest);
            if(
    $user->emailCheck($emailRequest) == false) {
                
    header('Location: index.php');
                exit;
            }
        }
    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title>Portal</title>
        <link href="./_css/portal.css" rel="stylesheet" />
    </head>
    <body>
        <div class="login-container">
            <!-- <center><font color="#197F67">Password Request Sent!</font></center><br /> -->
            <?php 
                
    if(isset($_POST['loginUser'])) {
                    
    $user $_POST['uname'];
                    
    $pass $_POST['upass'];
                    
    $email $_GET['emailRequest'];
                    echo 
    $user->login($email);
                    
    /*
                    if($user->login($email,$user,$pass)) {
                        $user->redirect('home.php');
                    } else {
                        echo '<font color="#F53939">Oops! Error</font><br />';
                    }

                    */
                
    }
            
    ?>
            <form method="post">
                <label id="label">Username</label>
                <input type="text" name="uname" placeholder="Username" required />
                <label id="label">Password</label>
                <input type="password" name="upass" placeholder="" required />
                <input type="submit" value="Login" name="loginUser" class="green" />
            </form>
        </div>
    </body>
    </html>
  10. #6
  11. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,976
    Rep Power
    9615
    PHP Code:
    $user $_POST['uname']; 

    Comments on this post

    • MonP agrees
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Philippines
    Posts
    66
    Rep Power
    3
    Originally Posted by requinix
    PHP Code:
    $user $_POST['uname']; 
    Thank you! Problem solved!

IMN logo majestic logo threadwatch logo seochat tools logo