sessions and OOP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sequalit
Forum Commoner
Posts: 75
Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas

sessions and OOP

Post by Sequalit »

When creating a OO project that holds members and stuff (like a weblog), when should i deal with the sessions?

Should I make a whole new class dealing with my sessions or should I store my session data in my member class (dealing with login and logout functions)?"????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

session's are their own class, because they can store any information, not just user information. There's also being able to store sessions in various places too, such as in the database versus in the file system..
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Another method would be to store your member information in the session, or to session_register it.
Sequalit
Forum Commoner
Posts: 75
Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas

Post by Sequalit »

Aye, well I was programming my member class and come across a problem... how should I store my sessions... or whens hould I impliment my sessions...

I got this far then stopped confused as to what would be a good idea...

Code: Select all

class Member extends core{
	session_start();
	$_SESSION['auth'];
	$_SESSION['id'];
	$_SESSION['restrictions'];
	
	var $dbid;
	var $dbusername;
	var $dbpassword;
	var $dbrestrict;
	
	function login($username,$password){
		$this->query("SELECT * FROM godschildren WHERE username='$username'");
		if($this->getRows() == 0){
			return 0;
		}
		$this->Assoc();
		$this->setVars($this->getData());
		if($this->dbpassword == $password){
			$_SESSION['auth'] = 1;
			$_SESSION['restrictions'] = $this->dbrestrict;
			$_SESSION['id'] = $this->dbid;
			return 1;
		}
	}
	function logout(){
		$this->dbid = "";
		$this->dbusername = "";
		$this->dbpassword = "";
		$this->dbrestrict = "";
		$_SESSION['auth'] = 0;
		$_SESSION['restrictions'] = "";
		$_SESSION['id'] = "";
	}
	function setVars($data){
		$this->dbid = $data['id'];
		$this->dbusername = $data['username'];
		$this->dbpassword = $data['password'];
		$this->dbrestrict = $data['restrictions'];
	}
	function isLoggedIn(){
		if($_SESSION['auth'] == 1){
			return 1;
		}else{return 0;}
	}
}
instead should i just make a seperate session class that deals with the session data?
Post Reply