Greetings,
I'm currently working on a project to help myslef understand the rules and syntax of PHP (php4 for now) OOP.
To arrive at the point, I've got two files, each containing a class. One class defines my database, and the other defines my session/user functions. It looks something like this:
<?php
/*********************
Session Management 2
**********************/
include("db.php");
//creating an instance of my db handlng class
$database = new DB;
class Session {
var $username;
var $password;
var $ctime;
function Session () {
$this->ctime = time();
$this->startSession();
//$database = new DB; -this didn't work
}
function isOnline() {
if(isset($_SESSION['username'])){
$username = $_SESSION['username'];
$pre_q = "SELECT * from ".TBL_ACTIVE_USERS." where username = '$username'";
$checkOnline = mysql_query($pre_q, $database->connection);
if (mysql_num_rows($checkOnline) != 0) {
return true; // is online...
}
}
else {
return false; // is not online...
}
}
function startSession() {
session_start();
$navigator_user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
if (stristr($navigator_user_agent, "msie")) header("Cache-control: private"); //ya msie fix
$this->setSessionVars();
}
function setSessionVars() {
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
if ($this->isOnline()) {
$_SESSION['timestamp'] = $this->ctime;
$_SESSION['userip'] = $_SERVER['REMOTE_ADDR'];
$database->updateActiveUser($_SESSION['username'], $_SESSION['timestamp'], $_SERVER['PHP_SELF']);
}
else {
$_SESSION['timestamp'] = $this->ctime;
$_SESSION['sessionlevel'] = GUEST_LEVEL; // sets guest level access
$_SESSION['sessionuser'] = GUEST_NAME; //sets guests level name
$database->updateActiveGuest($_SESSION['ip'], $_SESSION['timestamp'], SID, $_SERVER['PHP_SELF']);//please**note** this line
}
}
//more code after this
I'm getting an error when I call this function from the database. I know I'm probably doing something very wrong, and I'm open to input.
I suppose my main issue is this... I'm getting a Fatal error: Call to a member function on a non-object at about this line. (see "note")
Thank you for your kind consideration of my code
🙂
Edit: I hope I'm posting this in the correct forum. I haven't been using php that long (or any programming language for that matter) and just assumed this is where I should post.