This library registers a new user.
Questions:
Where should the DB class instantiation happen for user class? I tried instantiation in the constructor but that property doesn't seem to be available to the rest of the user class. As a work-around, I'm creating a new instance with every function that needs the DB class. It works, but this must be wrong.
Which methods should be public and which private/protected? I'm thinking
encryptUser()
should be private, but I'm not sure.Should my database functions to insert and query be combined into one function or is it fine having two? What would a combined function look like.?
Is there a better way to access my config variables or is putting global
$config
in the constructor?
<?PHP
error_reporting(E_ALL);
require_once 'config.php';
define('BASEPATH',realpath(dirname(__FILE__)) );
define('APPROOT', 'http://blahblah.com/' );
class User{
public $data;
public function __construct(){
$data=new DB;
}
public function createNew(){
//trim post values
if(!empty($_POST['email'])&& !empty($_POST['password'])){
$fname=trim($_POST['fname']);
$lname=trim($_POST['lname']);
$userEmail=trim($_POST['email']);
$password=trim($_POST['password']);
if(!$this->user_exists($userEmail)){
//salt, hash then insert new user into database
$crypt=$this->encrypt_user($password);
$mydata=new DB;
$sql="INSERT INTO users (fname,lname, password,salt, email)
VALUES (:fname,:lname, :password,:salt,:email)";
$query_params=array(':fname'=>$fname,':lname'=>$lname,
':password'=>$crypt['password'],':salt'=>$crypt['salt'],
':email'=>$userEmail);
if($mydata->dbInsert($sql,$query_params)){
echo '<br>INSERT SUCCESSFUL';
}else{die('Insert Failed');}
}
else{
echo 'A user with the email'.$userEmail.' already exists.
Please user another or click here to login';
}
}
}/*END CREATE USER*/
public function user_exists($email){
$mydb=new DB;
$query='SELECT email from users WHERE email=:email';
$params=array(':email'=>$email);
if($mydb->dbQuery($query,$params)){
return true;
}else{
return false;
}
}
public function encrypt_user($pass){
$salt=dechex( mt_rand(0,2147483678) ).dechex( mt_rand(0,2147483678) );
$password=hash('sha256',$pass.$salt);
for($round=0;$round<65336; $round++){
$pass_hash=hash('sha256',$password.$salt);
}
return array('salt'=>$salt,'password'=>$pass_hash);
}//*end encrypt_user()
}
//----------**END CLASS USER**------------
class DB {
private $db;
private $dbHost;
private $dbUser;
private $dbPass;
public function __construct(){
global $config;
$this->db=$config['dbName'];
$this->dbHost=$config['dbHost'];
$this->dbUser=$config['dbUser'];
$this->dbPass=$config['dbPass'];
}
public function connect(){
$dbConn=new PDO('mysql:dbname='.$this->db.';
host='.$this->dbHost.'',$this->dbUser,$this->dbPass);
return $dbConn;
}
public function dbQuery($query,$params){
if($dbConn=$this->connect()){
try{
$stmt=$dbConn->prepare($query);
$result=$stmt->execute($params);
var_dump($stmt);
$row=$stmt->fetch(PDO::FETCH_ASSOC);
}catch(PDOException $e){
die('Error: '.$e->getmessage());
}
if(!empty($row)){
return $row;
}else{
return false;
}
}
}//**END dbQUERY
public function dbInsert($query,$params){
if($connDB=$this->connect()){
try{
$stmt=$connDB->prepare($query);
$result=$stmt->execute($params);
return true;
}catch(PDOException $ex){
/*THIS COULD ALSO BE SENT TO A LOG FILE*/
$ex->getmessage();
}
}
}//**End dbInsert
}
//**-----------END CLASS DB-----------
class render {
public function regUser(){
if(!isset($_POST['submit']))
{ //SHOW FORM
?>
<div id="accform">
<form method="post" action="#" name="newacc">
<span>First Name</span><input type="text" name="fname" id="FName">
<span>Last Name</span><input type="text" name="lname" id="LName">
<span>Email</span><input type="text" name="email" id="<Mail">
<span>Password</span><input type="text" name="password" id="PWord">
<input type="submit" name="submit" value="Sign Up">
</form>
</div>
<?
} else{
//process information
$user= new user;
$user->createNew();
}
}//*END register function
}
//END OF RENDER CLASS
global
+class
= bad code. Likewise for$_POST
in a class: if a method needs data, or an instance of a class, then that method should take arguments. When writing an abstraction layer (iePDO
wrapper) ask yourself: What would make people want to use my class? In this case, not much. If you make somethingprivate
think about the implications when youextend
from that class. You are using an email address: validate the input!filter_var($email, FILTER_VALIDATE_EMAIL)
. Lastly: coding standards matter \$\endgroup\$