Jump to content



Photo

Sessions Stored In Db Functions


  • Please log in to reply
6 replies to this topic

#1 bunder5

bunder5

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 1 March 2012 - 3:23 AM

I switched over to storing them into a db as done in the book but I notice that when my code has errors, often it leads to an error in the write_session() function. Calling on a function on a non-object, $dbc. The funny thing is that there is no code to write to the session but there is code to read session data.

It appears to be trying to write a session before opening the session and establishing a db connection. I confirmed it by putting code inside the write_session() function to open a new connection at each call.
  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,964 posts
  • LocationState College, PA (USA)

Posted 2 March 2012 - 7:18 PM

Um, is there a question here?
  • 0

#3 bunder5

bunder5

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 8 March 2012 - 4:00 AM

I altered your code to use PDO without any problems....except when one of my queries goes bad. Then I get two errors....

Fatal error: Call to undefined method PDOStatement::prepare() in \finalize.php on line 139
Fatal error: Call to a member function prepare() on a non-object in \db_sessions.inc.php on line 84

The first was from a failed query. The second is line #84 which is the write_session() function. The funny thing is that if I insert code to establish $dbc inside write_session() and rerun that would eliminate the second error.

Then I put simple echo statements in the open_session() and write_session() and whenever my query goes bad it fails to open_session() before write_session() which is why I get the second error. Do you know why this is happening?


Sorry for late response, I did not subscribe to topic.
  • 0

#4 Paul Swanson

Paul Swanson

    Excellent Advisor

  • Members
  • PipPipPip
  • 163 posts
  • LocationPortland, OR, USA

Posted 8 March 2012 - 2:09 PM

You could check for an error before running both open_session() and write_session().
if ( $dbc->errorCode() == '01' ) {
    // no error, proceed to open_session and write_session
} else {
    // an error happened, use errorInfo() to get details
}

More info in the PHP Manual.
  • 1

#5 bunder5

bunder5

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 20 March 2012 - 12:20 PM

That works but I don't get why sometimes it would be attempting to write to session before opening the session. I trapped it and it goes to write first and I have not done anything with sessions.
  • 0

#6 bunder5

bunder5

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 20 March 2012 - 5:04 PM

Actually $dbc->error_code() is making my pages timeout. I am using isset() to see if that works.
  • 0

#7 Paul Swanson

Paul Swanson

    Excellent Advisor

  • Members
  • PipPipPip
  • 163 posts
  • LocationPortland, OR, USA

Posted 21 March 2012 - 11:15 AM

I'm making the assumption that your database resource link is stored in $dbc. If you use a different variable name, you'll need to change that conditional. Be sure to check the PHP Manual page on the proper use of error_code(). The example block of code should only attempt to process your PDO statements if there were no errors in your last database operation.

I can't provide too much help here, since I've never used PDO myself. In fact, we have such an old version of PHP on our server, I've stuck to procedural coding, so I'm not very well versed in OOP.
  • 0