Troy297
04-15-2008, 01:53 AM
Right now I'm trying to build a code base for my newest project (Radio DJ Panel v4) and I want to do it using Classes, Objects, and with the OOP methodology. Problem is, I've never used any sort of Classes or OOP structure before....
So the issue that I'm having is with having the classes use each other. For instance, I have my DB class which deals with all MySQL related stuff and then I also have my Sessions class which, obviously, deals with all my Session/Cookie related stuff. What I'm trying to do is make my Session validation more secure so I am storing the Session ID's for any active Sessions in a DB and then referencing the DB whenever I create or use/interact with any existing Sessions.
I currently have it setup so that my "init.php" file requires all the different classes and then creates instances of each for use in my script. Now the issue I'm having is with how I would go about using the DB class inside the Session class. Should I create a new instance of the DB class inside the Session class to be able to use the DB class or how would you recommend doing it?
Here are some snippets from my code so far....
init.php
<?php
require('config.php');
require('classes/mysql.php');
require('classes/session.php');
$db = new mysql_db;
$ses = new session;
?>
session.php
<?php
class session{
public $session_id;
public $host;
public $browser;
public function __construct(){
$this->session_id = session_id();
$this->host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$this->browser = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
// This is where I need the DB query....
$num = $db->count("SELECT * FROM `sessions` WHERE `sessionid` = '".$this->session_id."' AND `host` = '".$this->host."' AND `browser` = '".$this->browser."'");
// The above code currently gives the following error:
// Fatal error: Call to a member function count() on a non-object....
if($num >= 1){
echo 'exists';
}else{
session_start();
}
}
}
?>
Note that these classes and whatnot are nowhere near finished yet but I just need some suggestions as to how I should do it.
Any help is greatly appreciated!
So the issue that I'm having is with having the classes use each other. For instance, I have my DB class which deals with all MySQL related stuff and then I also have my Sessions class which, obviously, deals with all my Session/Cookie related stuff. What I'm trying to do is make my Session validation more secure so I am storing the Session ID's for any active Sessions in a DB and then referencing the DB whenever I create or use/interact with any existing Sessions.
I currently have it setup so that my "init.php" file requires all the different classes and then creates instances of each for use in my script. Now the issue I'm having is with how I would go about using the DB class inside the Session class. Should I create a new instance of the DB class inside the Session class to be able to use the DB class or how would you recommend doing it?
Here are some snippets from my code so far....
init.php
<?php
require('config.php');
require('classes/mysql.php');
require('classes/session.php');
$db = new mysql_db;
$ses = new session;
?>
session.php
<?php
class session{
public $session_id;
public $host;
public $browser;
public function __construct(){
$this->session_id = session_id();
$this->host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$this->browser = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
// This is where I need the DB query....
$num = $db->count("SELECT * FROM `sessions` WHERE `sessionid` = '".$this->session_id."' AND `host` = '".$this->host."' AND `browser` = '".$this->browser."'");
// The above code currently gives the following error:
// Fatal error: Call to a member function count() on a non-object....
if($num >= 1){
echo 'exists';
}else{
session_start();
}
}
}
?>
Note that these classes and whatnot are nowhere near finished yet but I just need some suggestions as to how I should do it.
Any help is greatly appreciated!