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!
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
<?
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;
}
}
//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 "
<?
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;
}
}
}
?>
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.
<?
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...
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()