Hey guys. 🙂
I have a login class that has to connect to the database to verify a username and password. In this class I include and use my mySQL class.
Is that ok coding practice?
I am wondering because the login class cannot stand on it's own and is totaly dependent on my mySQL class to be able to work, but I cannot think of another way to do it, without totaly skipping my mySQL class and just directly accesing mySQL thru the mySQL API.
Here is what my two classes look like. Any input is appreciated!
Thanks!
mySQL Wrapper class
class mySQLConTran{
var $con;
var $numRows;
var $result;
var $errorArray = array();
var $lastError;
// Open Database connection ---------
function dbOpen($name,$host,$user,$password){
$this->con = @mysql_connect($host, $user, $password);
$sel = @mysql_select_db($name, $this->con);
if(!$this->con || !$sel) return false;
}//end dbOpen
// Close Database Connection --------
function dbClose(){
@mysql_close($this->con);
}//end dbClose
// INSERT, UPDATE, DELETE, CREATE, DROP etc... --------
function dbCommand($sql){
$this->result = @mysql_query($sql);
if(!$this->result){
$this->lastError = mysql_error();
$this->errorArray[] = mysql_error();
return false;
}else{
return true;
}//end if
}//end dbIUD
// SELECT FROM Database --------
function dbSelect($sql){
$this->result = @mysql_query($sql);
$this->numRows = @mysql_num_rows($this->result);
if(!$this->result){
$this->lastError = mysql_error();
$this->errorArray[] = mysql_error();
return false;
}else{
return true;
}//end if
}//end dbSelect
// Get first row of dbSelect --------
function getFirstRow(){
$firstRow = @mysql_fetch_array($this->result);
return $firstRow;
}//end getFirstRow
// Get last row of dbSelect --------
function getLastRow(){
@mysql_data_seek($this->result,$this->numRows-1);
$lastRow = @mysql_fetch_array($this->result);
return $lastRow;
}//end getLastRow
// Get any row of dbSelect index starts with 0--------
function getAnyRow($rowNum){
if($rowNum > $this->numRows || $rowNum < 0){
return false;
}else{
@mysql_data_seek($this->result,$rowNum);
$anyRow = @mysql_fetch_array($this->result);
return $anyRow;
}//end if
}//end getLastRow
// Get number of rows from dbSelect --------
function getNumRows(){
return $this->numRows;
}//end getNumRows
// Get number of mysql_errors --------
function getErrorCount(){
return count($this->errorArray);
}//end getErrorCount
// Get error array --------
function getErrorArray(){
return $this->errorArray;
}//end getErrorArray
// Get Last mysql_error --------
function getLastError(){
return $this->lastError;
}//end getLastError
}//end mySQLConTran Class -------------------------------
Log In Class
require("mysql_con_tran.class.php");
class logIn{
//try to authenticate the username and password
function tryLogIn($dbName, $dbHostName, $dbUser, $dbPass, $user, $pass, $failMessage,
$failRedirect, $authRedirect, $sql, $rowField, $sessionName){
$status = $this->authUser($dbName, $dbHostName, $dbUser, $dbPass, $user, $pass, $sql, $rowField, $sessionName);
if (!$status) {//if username and password are not correct send to failed page with error mesage
header("Location: ".$failRedirect."?msg=".$failMessage);
}else {//else goto authorized log in page.
header ("Location: ".$authRedirect);
}//end if
}//end function tryLogin
//check if username and password are in database
function authUser($dbName, $dbHostName, $dbUser, $dbPass, $user, $pass, $sql, $rowField, $sessionName){
//connect to database
$db = new mySQLConTran();
$db->dbOpen($dbName, $dbHostName, $dbUser, $dbPass);
//see if SQL is valid, if not return false
$validate = $db->dbCommand($sql);
if(!$validate) return false;
//close database connection
$con->dbClose();
if ($db->getNumRows() == 0){//if no rows returned return false
return false;
}else {//return valid login and start session
session_start();
$row = $db->getFirstRow();
$_SESSION[$sessionName] = $row[$rowField];
return true;
}//end if
}//end function AuthUser
}//end class log in