Page 1 of 1

More class help =) Include file?

Posted: Sun Feb 06, 2011 11:46 pm
by psychotomus
As breaking away from structural coding and moving towards OOP. I am running into some awkward problem's I am not use to.

I am trying to avoid having to include my file in every function where its needed. Its a list of constant's which holds table name's and other information. How else can I do this besides place in every function?
Parse error: syntax error, unexpected T_INCLUDE, expecting T_FUNCTION in classes/user_class.php on line 9

Code: Select all

<?
class user
{
	//vars
	private $db;

	//includes 
	include "../includes/constants.php";
	
	
	public function __construct($dbLink)
	{
		$db = $dbLink;
		//see what we doing here. 
		//login, register, forgot password, or reset password
		switch ($_GET['action'])
		{
			case "login":
				userLogin();
				break;
		}
		
		unset($_SESSION['msg']);
	}
	//attempt to login user
	public function userLogin()
	{

		//check if username pass string test
		if(checkUser() == true && checkPass() == true)
		{
			//query to check if user name or email in use
			$pass = encrypt stuff i blocked out from u
			$rec = $db->fetch("SELECT userID, userActCode, userRank, COUNT(userID) as counts FROM " . USER_TABLE . " WHERE userHandle='" . $db->escape($_POST['textUser']) . "' AND userPass='" . $db->escape($pass) . "'");
			//user name and password match
			if($rec['counts'] == 1)
			{
				//user account activated
				if($rec['userActCode'] == 0)
				{
					$_SESSION['userID'] = $rec['userID'];
					$_SESSION['userRank'] = $rec['userRank'];
					
					//update last activitity
					$data = array("userLastActive" => time());
					$db->update(USER_TABLE, $data, "userID='" . $rec['userID'] . "'");
					
					header("Location: index.php");
				}
				else
				{
					//display error
					$_SESSION['msg'] = "You must first activate your account...";
				}
			}
			else
			{
				//display error
				$_SESSION['msg'] = "Incorrect User name and password combination...";
			}
		}
	}

	//check some password stuff out
	public function checkPass()
	{
		//check if username is between 3 and 25 characters
		if( (strlen($_POST['textPass']) >= USER_MIN_LENGTH) && (strlen($_POST['textPass']) <= USER_MAX_LENGTH) )
		{
			return true;
		}
		else
		{
			$_SESSION['msg'] .= "password must be between " . USER_MIN_LENGTH . " and " . USER_MAX_LENGTH . " characters long...";
			return false;
		}
	}
	
	//check some user name stuff out
	private function checkUser()
	{
		//check if password is between 3 and 25 characters
		if( (strlen($_POST['textUser']) >= USER_MIN_LENGTH) && (strlen($_POST['textUser']) <= USER_MAX_LENGTH) )
		{
			return true;
		}
		else
		{
			$_SESSION['msg'] .= "User name must be between " . USER_MIN_LENGTH . " and " . USER_MAX_LENGTH . " characters long...";
			return false;
		}
	}

Re: More class help =) Include file?

Posted: Sun Feb 06, 2011 11:58 pm
by califdon
I can't say that I've had the need to do this, but there is a discussion of using includes within a class definition at http://php.net/manual/en/language.oop5.autoload.php that might help you.

Re: More class help =) Include file?

Posted: Mon Feb 07, 2011 12:15 am
by psychotomus
I am unsure if its working or my constant's are just being weird..

constants.php

Code: Select all

//some consts for regging
define("USER_MAX_LENGTH", 20);
define("USER_MIN_LENGTH", 3);

my class:
It keeps outputing "User name must be between USER_MIN_LENGTH and USER_MAX_LENGTH characters long..."
It should be working, and even if it outputted a message, shouldn't it say "3 and 20 characters long "

Code: Select all

<?
class user
{
	//vars
	private $db;
	private $smarty;

	function __autoload() 
	{
		include "../includes/constants.php";
	}

	public function __construct($dbRef, $smartyRef)
	{
		$db = $dbRef;
		$smarty = $smartyRef;
		//see what we doing here. 
		//login, register, forgot password, or reset password
		switch ($_GET['action'])
		{
			case "login":
				$this->userLogin();
				break;
		}
		
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
	}
	//attempt to login user
	public function userLogin()
	{

		//check if username pass string test
		if($this->checkUser() == true && $this->checkPass() == true)
		{
			//query to check if user name or email in use
			$pass = sha1(strtoupper($_POST['textUser']) . $_POST['textPass']);
			$rec = $db->fetch("SELECT userID, userActCode, userRank, COUNT(userID) as counts FROM " . USER_TABLE . " WHERE userHandle='" . $db->escape($_POST['textUser']) . "' AND userPass='" . $db->escape($pass) . "'");
			//user name and password match
			if($rec['counts'] == 1)
			{
				//user account activated
				if($rec['userActCode'] == 0)
				{
					$_SESSION['userID'] = $rec['userID'];
					$_SESSION['userRank'] = $rec['userRank'];
					
					//update last activitity
					$data = array("userLastActive" => time());
					$db->update(USER_TABLE, $data, "userID='" . $rec['userID'] . "'");
					
					header("Location: index.php");
				}
				else
				{
					//display error
					$_SESSION['msg'] = "You must first activate your account...";
				}
			}
			else
			{
				//display error
				$_SESSION['msg'] = "Incorrect User name and password combination...";
			}
		}
	}
	
	
	//check some password stuff out
	public function checkPass()
	{
		//check if username is between 3 and 25 characters
		if( (strlen($_POST['textPass']) >= USER_MIN_LENGTH) && (strlen($_POST['textPass']) <= USER_MAX_LENGTH) )
		{
			return true;
		}
		else
		{
			$_SESSION['msg'] .= "password must be between " . USER_MIN_LENGTH . " and " . USER_MAX_LENGTH . " characters long...";
			return false;
		}
	}
	
	//check some user name stuff out
	private function checkUser()
	{
		echo USER_MIN_LENGTH;
		//check if username is between 3 and 25 characters
		if( (strlen($_POST['textUser']) >= USER_MIN_LENGTH) && (strlen($_POST['textUser']) <= USER_MAX_LENGTH) )
		{
			return true;
		}
		else
		{
			$_SESSION['msg'] .= "User name must be between " . USER_MIN_LENGTH . " and " . USER_MAX_LENGTH . " characters long...";
			return false;
		}
	}
}
?>

Re: More class help =) Include file?

Posted: Wed Feb 09, 2011 1:53 pm
by psychotomus
Anybody? =)

Re: More class help =) Include file?

Posted: Wed Feb 09, 2011 2:58 pm
by John Cartwright
Just put this outside your class, i.e.,

Code: Select all

include "../includes/constants.php";

class user {

   ...

}
Also, autoloading is not just a method you include in a class, and it will magically be called. You define a function or class to handle loading of a class that was not previously loaded. Have a good read on that documentation page again.

It's probably not what your looking for in this case.

Re: More class help =) Include file?

Posted: Wed Feb 09, 2011 3:19 pm
by psychotomus
sweet. works like a charm. now a new problem arises. ;]


I get an error
Fatal error: Call to a member function fetch() on a non-object in /home/quadsim/public_html/story/classes/user_class.php on line 62

Code: Select all

$rec = $db->fetch("SELECT userID, userActCode, userRank, COUNT(userID) as counts FROM " . USER_TABLE . " WHERE userHandle='" . $db->escape($_POST['textUser']) . "' AND userPass='" . $db->escape($pass) . "'");
With this code

Code: Select all

<?
include "includes/constants.php";
class user
{
	//vars
	private $db;
	private $smarty;


	public function __construct($dbRef, $smartyRef)
	{
		$db = $dbRef;
		$smarty = $smartyRef;

		//see what we doing here. 
		//login, register, forgot password, or reset password
		switch ($_GET['action'])
		{
		
			case "activate":
				$this->userActivate();
				break;

			case "forgot":
				$this->userResetPass();
				break;

			case "login":
				$this->userLogin();
				break;

			case "logout":
				session_destroy();
				$_SESSION['msg'] = "You are now logged out...";
				break;

			case "register":
				$this->userRegister();
				break;
				
			case "resend":
				$this->userResendCode();
				break;
		}
		
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
	}
	//attempt to login user
	public function userLogin()
	{

		//check if username pass string test
		if($this->checkUser() == true && $this->checkPass() == true)
		{
			//query to check if user name or email in use
			$pass = sha1(strtoupper($_POST['textUser']) . $_POST['textPass']);
			$rec = $db->fetch("SELECT userID, userActCode, userRank, COUNT(userID) as counts FROM " . USER_TABLE . " WHERE userHandle='" . $db->escape($_POST['textUser']) . "' AND userPass='" . $db->escape($pass) . "'");
			//user name and password match
			if($rec['counts'] == 1)
			{
				//user account activated
				if($rec['userActCode'] == 0)
				{
					$_SESSION['userID'] = $rec['userID'];
					$_SESSION['userRank'] = $rec['userRank'];
					
					//update last activitity
					$data = array("userLastActive" => time());
					$db->update(USER_TABLE, $data, "userID='" . $rec['userID'] . "'");
					
					header("Location: index.php");
				}
				else
				{
					//display error
					$_SESSION['msg'] = "You must first activate your account...";
				}
			}
			else
			{
				//display error
				$_SESSION['msg'] = "Incorrect User name and password combination...";
			}
		}
	}
I can avoid the error however by passing through my Db refrence "$this->userLogin($db)" and making my function userLogin($db).. I do not really wish to do this. I only wanted to pass it through in my class once. Theres a lot of functions and I dont want to change them all. I even tried $this->$db->fetch and $this->db->fetch will still don't work...

Re: More class help =) Include file?

Posted: Wed Feb 09, 2011 3:50 pm
by John Cartwright
You need to store your $db as a class member variable, i.e.,

Code: Select all

class user
{
   public function __construct($db) {
      $this->db = $db;
   }

   public function doSomething() {
      $this->db->someDbFunction();
   }
}

Re: More class help =) Include file?

Posted: Wed Feb 09, 2011 7:59 pm
by psychotomus
sweet. It works!!! Thanks a lot guys. =]

Re: More class help =) Include file?

Posted: Sun Feb 13, 2011 10:37 am
by psychotomus
I am starting to get the hang of this OOP stuff but I still have some wacky problems...

I am able to call my member class and it goes to my memberShowEditProfile page but the problem is in the class itself when I submit the update avatar form

this line returns "user table: " and USER_TABLE is always blank in the function memberEditProfile()

Code: Select all

echo "user table: " . USER_TABLE;
when calling the function memberShowEditProfile
USER_TABLE isn't blank and is actually set correctly as "user"

any ideas?

my index.php

Code: Select all

	
elseif($_GET['where'] == "member")
	{
		include "classes/member.php";
		if($_GET['action'] == "editprofile")
		{
			$member = new member($db, $smarty);
			$member->memberShowEditProfile();
			$page = "editprofile";
		}
	}
my member class

Code: Select all

<?
include "includes/constants.php";
class member
{
	//vars
	private $db;
	private $smarty;
	private $file_type;
	
	function __autoload() 
	{
		
	}

	public function __construct($dbRef, $smartyRef)
	{
		$this->db = $dbRef;
		$this->smarty = $smartyRef;
		//see what we doing here. 
		switch ($_GET['action'])
		{
			case "editprofile":
				$this->memberEditProfile();
				break;
		}
		
		$this->smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
	}
	
	function memberShowEditProfile()
	{
		$userid = $_SESSION['userID'];
		$rec = $this->db->fetch("SELECT userURLTitle, userURL, userText FROM " . USER_TABLE . " WHERE UserID='$userid'");

		//template vars
		$this->smarty->assign("memberTitle", $rec['userURLTitle']);
		$this->smarty->assign("memberURL", $rec['userURL']);
		$this->smarty->assign("memberBio", $rec['userText']);

	}

	function memberEditProfile()
	{
		if(isset($_POST['SubmitAvatar']))
		{
			if($this->uploadAvatar() == true)
			{
				//update user avatar
echo "user table: " . USER_TABLE;
				$data = array("userImage" => $this->file_type);
				$this->db->update(USER_TABLE, $data, "userID='" . $_SESSION['userID'] . "'");
				$_SESSION['userImage'] = $this->file_type;
				$_SESSION['msg'] = "Avatar updated...";
				return true;
			}
			else
			{
				return false;
			}
		}
	}