class.database.php
PHP
<?php
/**
* Version 0.1
* - beta
*
*
*
*
*/
class database
{
# Create standard variables for class
private $identifier;
public function __construct ($hostname = NULL, $username = NULL, $password = NULL, $database = NULL)
{
# Connect to database
$this->identifier = mysql_connect($hostname, $username, $password);
if ( mysql_error ( ) )
{
# Error handler, when sql error
$RespondsHandler->sql('CONNECT_ERROR');
return FALSE;
}
else
{
# Select database from database
mysql_select_db($database, $this->identifier);
if ( mysql_error ( ) )
{
# Error handler, when sql error
$RespondsHandler->sql('SELECT_DB_ERROR');
return FALSE;
}
}
}
public function SqlQuery ( $SqlLine = NULL )
{
# when a empty line has been returned
if ( ! func_num_args() OR ! is_string($SqlLine))
{
$RespondsHandler->sql('NO_SQL_LINE');
return FALSE;
}
else
{
# The query itself
$Query = mysql_query ( $this->SqlLine );
if ( mysql_error ( ) )
{
# Error handler, when sql error
$RespondsHandler->sql('SQL_ERROR', mysql_error ( ) );
return FALSE;
}
else
{
# Sql succeed
return $Query;
}
}
}
function Value ( $SqlLine = NULL, $Value = NULL )
{
# When 1 or both lines are not filled return an error
if ( !$SqlLine OR !$Value )
{
# Error handler, when sql error
$RepsondsHandler->sql('SQL_LINE_OR_VALUE_ERROR');
return FALSE;
}
else
{
# Check if value is an array, if so run the if otherwise the else
if ( is_array ( $Value ) )
{
# Create the query to return results
$this->Result = $this->( $this->SqlLine );
# Reverze array's
$Value = array_flip($Value);
if ( is_string( $Value ) )
{
$assoc = mysql_fetch_assoc($query);
$array_return = array_intersect_key($assoc, $Value);
# Return the result
return $array_return;
}
}
else
{
# Create the query to return 1 assoc result
$Result = $this->sqlQuery( $Query );
$Fetch = mysql_fetch_assoc ( $Result );
if ( mysql_error ( ) )
{
# Error handler, when sql error
$RespondsHandler->sql('SQL_ERROR', mysql_error ( ) );
return FALSE;
}
else
{
# Return the result of the assoc
return $Fetch[$Value];
}
}
}
}
public function Rows ( $Query = NULL )
{
if ( ! $Query OR ! is_string($Query ) )
{
# Error handler, when sql error
$RespondsHandler->sql('NO_SQL_LINE');
return FALSE;
}
else
{
# Run the sql query, to return the row count
$Result = $this->SqlQuery( $Query );
$RowCount = mysql_num_rows ( $Result );
if ( mysql_error ( ) )
{
# Error handler, when sql error
$RespondsHandler->sql('SQL_ERROR', mysql_error ( ) );
return FALSE;
}
else
{
# Return the number count
return $RowCount;
}
}
}
function __destruct ( )
{
# Close the mysql connection
mysql_close( $this->identifier );
}
}
?>
Toon Meer
class.login.php
PHP
<?php
class Login
{
# Allows to use the database class
global $database;
#create protected variables
private $username;
private $password;
private $hash;
public function __construct ()
}
public function UserLogin ($name = NULL, $password = NULL)
{
if ( empty ( $name ) OR empty ( $password ) )
{
# Error handler, when sql error
$RespondsHandler->login('LOGIN_ERROR');
return FALSE;
}
else
{
# set the variables
$this->$username = $name;
$this->$password = $password;
if ( $rows('SELECT * FROM `users` WHERE
`username` = ' . addslashes($this->username) . ' AND
`passord` = ' . md5 ( addslashes ( $this->password ) ) . ' ') > 0 )
{
# Yeah it is registerd, create the session for the user
self::RegisterSession();
$RespondsHandler->login('LOGIN_SUCCESS');
return TRUE;
}
else
{
# Something bad happen, the user doesnt exist or the password is wrong
$RespondsHandler->login('LOGIN_ERROR');
return FALSE;
}
}
}
private function RegisterSession ( )
{
# Get user id, and store in session
$_SESSION['uid'] = $datbase->Value('SELECT * FROM `users` WHERE
`username` = ' . addslashes($this->username) . ' AND
`passord` = ' . md5 ( addslashes ( $this->password ) ) . ' ', 'id');
# Update time that the user is online, and hash for protection
$database->SqlQuery('UPDATE `users` set `time` = NOW(), `hash` = ' . $this->hash . ' WHERE
`username` = ' . addslashes($this->username) . ' AND
`passord` = ' . md5 ( addslashes ( $this->password ) ) . ' ');
return TRUE;
}
function __destruct ( )
{
# unset the variables, for security
unset $this->username;
unset $this->password;
unset $this->hash;
}
}
?>
Toon Meer
tips of iets :)?
thx:slotje: