Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:10:39+00:00 2026-05-29T05:10:39+00:00

My first real foray into using PHP OOP and PDO extensively. I have finally

  • 0

My first real foray into using PHP OOP and PDO extensively. I have finally gotten the script to work, but as you notice in order to do it I had to move the PDO connect into the login function – originally it was just in the __construct(). I do not want to have to open a new PDO connect and this is sloppy. How can I maintain the same connection throughout the whole class?

 <?php
class user{

public $id;
public $name;
public $email;
private $password;

public function __construct() {
    $DBH = new PDO("mysql:host=HOST;dbname=DB", "USER", "PASS");  
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

public function login($email,$password,$type){

    $DBH = new PDO("mysql:host=HOST;dbname=DB", "USER", "PASS");  
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $password_hash = sha1($password);
    try{
        if ($type != "actives") {
            throw new Exception("Type Handling Error");
        }
        $STH = $DBH->query("SELECT id, email, password FROM $type WHERE email='$email' AND password='$password_hash'");
        $STH->setFetchMode(PDO::FETCH_ASSOC);  
        $row_count = $STH->rowCount();  
        $row = $STH->fetch();

        if($row_count == 1){
            session_start();
            session_regenerate_id();
            $_SESSION['id'] == $row[id];
            return true;
        }
        else{
        return false;
        }
    }
    catch (Exception $e) {
        echo $e->getMessage();
    }

}

public function loggout(){
    session_destroy();
    setcookie(session_name(), session_id(), 1, '/');
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:10:40+00:00Added an answer on May 29, 2026 at 5:10 am

    Make the database handle a private member within the class:

    class user
    {    
        public $id;
        public $name;
        public $email;
        private $password;
        private $dbh;
    
        public function __construct(PDO $dbh)
        {
            $this->dbh = $dbh;  
        }
    
        public function login($email, $password, $type)
        {
            $dbh = $this->dbh;
            ...
        }
    

    Usage:

    $pdo = new PDO("mysql:host=HOST;dbname=DB", "USER", "PASS");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $user = new User($pdo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first real dive into JavaScript. Sure I've used it before, but
I'm creating my first real binary parser (a tiff reader) and have a question
First, why I ask: Now that reputation points on StackOverflow have real value (you
First off, I am using Windows XP. I have multiple hard drives and it
I'm picking up ObjC and Cocoa, which is also my first serious foray into
I am programming my first real PHP website and am wondering how to make
Preface: This is the first real swing program I have done. I have a
I'm building my first real CI app. I have to build a survey system--If
Hi I am currently learning .net and am building my first real website using
So, have the chance to do my first real database, just thinking to myself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.