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

  • Home
  • SEARCH
  • 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 6209391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:58:36+00:00 2026-05-24T05:58:36+00:00

Here is the basic structure of my User class: class User { private $_userId

  • 0

Here is the basic structure of my User class:

class User {

private $_userId = NULL;
private $_isAuthenticated = FALSE;
private $_code = NULL;
private $_pageLoads = 1;
private $_dbh = NULL;
const SECRET = 'shhhh its a secret';
const PAGE_LOADS = 5;
const HALF_SECOND_MS = 500000;

function __construct( PDO $dbh ) {
    $this->_dbh = $dbh;
}

public function login( $username, $password ) {}

public function isAuthenticated() {}

private function _authenticate( $username, $password ) {}

private function _challenge() {}

private function _generateCode() {}

private function _protectedVars() {}

function __sleep() {}

function __wakeup() {}

public function save() {
    return serialize( $this );
}
}

DESCRIPTION OF WHAT I’M TRYING TO DO

I’ve been for quite some time using the PHP built in mysql functions. I’ve decided for this project that I’d follow a more OOP approach and move to PDO. When I used to build a “secure” environment I would have a login form, check if the user existed (with the correct password) and then save the user id in a session variable (say: $_SESSION[‘user_id’]) and voila. In my secure env. I would simply check if user_id was set and I’d give them access.

Now I know this wasn’t a very good approach, and I know the code above will probably have its own problems (which I hope you guys will give me hell for it for educational purposes). Basically my idea was to create a User object where I’d pass my PDO object in the constructor. From this user I could login and once all execution was done I’d save the user in a session variable by serializing the class. Once a new page would load the wake up function would get called and I’d callenge the data that’s unserialized to make sure it wasn’t tampered with, in which case, I’d log off the user by turning the isAuthenticated flag off (that’s the code above).

QUESTION

When I try to serialize the object I get an error telling me that PDO objects cannot be serialized. I did a bit of research and see that I would have to implement the __sleep method and return an array of variables I’d want to save. However, when I wake up how do I reinitialize the PDO object within the user object? Is there a method where I can add arguments to the unserialize function which would pass it down to my __wakeup function? Am I completely on the wrong track and need to rethink this?

  • 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-24T05:58:36+00:00Added an answer on May 24, 2026 at 5:58 am

    You should be able to get your PDO object from something like MyApp::getConnection() and call it from your __wakeup method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the basic situation. Public Class MyEnumClass(of T) Public MyValue as T End
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's the basic idea: There is a java window (main) that opens another java
Here is my basic use case: After Labview receives a trigger from external hardware
I just moved over to the Visual Basic team here at work. What is
Just looking for the first step basic solution here that keeps the honest people
Now I must be missing something here, as this seems a very basic issue
The basics have already been answered here . But is there a pre-built PHP
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs

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.