Jump to content

Building a class


corillo181

Recommended Posts

hey i'm building a class for a upload form and i want to know whats the best way to handle error in a class,

like if someone enters a bad email or a bad password whats the best way to stop the class from keep going

 

<?php
class singUp{

    private $email;
    private $password;
    private $password2;

function __construct($email,$password,$password2){
 $this->email = $email;
 $this->password = $password;
 $this->password2 = $password2;
}

function checkEmail(){	 
 $check="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})";
  if(!eregi($check,$this->email)){ 
	return "Please check your email";
}	

function checkPassword(){
 if(($this->password || $this->password2)==''){
  return "passwords empty";

 }elseif($this->password != $this->password2){
  return "passwords do not match";
  
  }elseif(strlen($this->password)<6){
   return "Sorry your password is too short";
    }
   }
}
}

if(isset($_POST['Submit'])){

$singup =new singUp($_POST['email'],$_POST['password'],$_POST['password2']); 

echo $singup->checkEmail();
echo $singup->checkPassword();
}

?>

Link to comment
Share on other sites

how about this ?

 

sorry for formatting  (edited direct)

 

<?php
class singUp{

    private $email;
    private $password;
    private $password2;
    var $errors = array();

function __construct($email,$password,$password2){
 $this->email = $email;
 $this->password = $password;
 $this->password2 = $password2;
}

function checkEmail(){	 
 $check="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})";
  if(!eregi($check,$this->email)){ 
	$errors[] = "Please check your email";
	return false;
	}
}	

function checkPassword(){
 if(($this->password || $this->password2)=='')
 {
  $errors[] = "passwords empty";
return false;

 }elseif($this->password != $this->password2){
  $errors[] = "passwords do not match";
return false;  
  }elseif(strlen($this->password)<6){
   $errors[] = "Sorry your password is too short";
return false;
    }
   }
}
}

if(isset($_POST['Submit'])){

$singup =new singUp($_POST['email'],$_POST['password'],$_POST['password2']); 

$X = $singup->checkEmail();
//X= isemail valid
$X =$singup->checkPassword();
//X= password valid

//Show Errors
print_r($singup->errors);


}

?>

Link to comment
Share on other sites

but the array comes up empty.

 

but i made some modification thanx to your code less code same work.

 

<?php
class singUp{

    private $email;
    private $password;
    private $password2;
private $error;
public function __construct($email,$password,$password2){
 $this->email = $email;
 $this->password = addslashes(htmlspecialchars($password));
 $this->password2 = addslashes(htmlspecialchars($password2));
}

public function checkEmail(){	 
 $check="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})";
  if(!eregi($check,$this->email)){ 
	$this->error.="Please check your email<br />";
return false;		
  }
}

public function checkPassword(){
 if(($this->password || $this->password2)==''){
  $this->error.="passwords empty<br />";
return false;

 }elseif($this->password != $this->password2){
  $this->error.="passwords do not match<br />";
return false;

  }elseif(strlen($this->password)<6){
   $this->error.="Sorry your password is too short<br />";
return false;
   }
  }
   
 public function errorMessage(){
 return $this->error;
 }
}

if(isset($_POST['Submit'])){

$singup =new singUp($_POST['email'],$_POST['password'],$_POST['password2']); 

echo $singup->checkEmail();
echo $singup->checkPassword();
echo $singup->errorMessage();
}

?>

Link to comment
Share on other sites

any one knows why this code is not working i get

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\include\db.php on line 44

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\include\db.php on line 44

 

but if i put the code simple with out a class it does work.

<?php
class DB{
 private $user;
 private $pass;
 private $host;
 private $database;
 public $error= array();

  public function __construct($username,$password,$host){
	 $this->user = $username;
 $this->pass = $password;
 $this->host = $host;
 $this->database = $database;

}

  public function DBconnect(){
  $connection = mysql_connect($this->host,$this->user,$this->pass)or die(mysql_error());

  if(!$connection){
  $this->error[]=$connection;
   }
}

public function DBselect($database){ 
  $this->database=$database;
     if (!mysql_select_db($this->database)) {
echo 'Selection of database: '.$this->database.' failed.';
return false;
      }
}
  
public function errorMess(){
  foreach($this->error as $E){
 echo $E.'<br />';
  }
}
}

$db=new DB('username','password','localhost');
$db->DBselect('test_com');

$query = "SELECT username FROM tra_users";
mysql_query($query);
?>

Link to comment
Share on other sites

my last question with this class is how do i make the query execute if there is no error there current method i'm using execute the curry no matter if there are errors.


<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/include/header.php');
class singUp{

    static private $email;
    static private $password;
    static private $password2;
static public $error = array();

public function __construct($email,$password,$password2){
 $this->email = $email;
 $this->password = $password;
 $this->password2 = $password2;

}

// EMAIL CHECK	
public function checkEmail(){	 
 $check="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})";
 $checkemail=mysql_query("SELECT email FROM tra_users WHERE email='$this->email'")or die(mysql_error());
 $countemail=mysql_num_rows($checkemail);
 if(empty($this->email)){
  $this->error['email']="You did not enter a email";
return false;
}elseif($countemail>0){
 $this->error['email']="Sorry that email is already in use*";
return false;
 }elseif(!eregi($check,$this->email)){ 
	$this->error['email']="Please check your email";
return false;		
  }else{
   return $this->email;
  }
}

//PASSWORD CHECK	
public function checkPassword(){
 if(($this->password=='' || $this->password2)==''){
  $this->error['password']="passwords empty";
return false;

 }elseif($this->password != $this->password2){
  $this->error['password']="passwords do not match";
return false;

  }elseif(strlen($this->password)<6 && strlen($this->password2)<6){
   $this->error['password']="Sorry your password is too short";
return false;
   }else{
   return $this->password;
   }
  }
  
//ERROR MASSAGE HANDLE
 public function errorMessage(){
  if(!empty($this->error)){
 foreach($this->error as $E){
 echo $E.'<br />';
   }
  }
 }

// WORKING ON EMAIL	 
 public function email(){
  $sent=mail($this->email,$subject,$mess,$header);

 }

//GENERATE CODE FOR ACTIVATION CHECK	 
 public function getCode(){
  $rand = mt_rand(0, 32);
      $code = md5($rand . time());
	return $code;
 }

//COUNTS HOW MANY ERROR IN THE CODE
 public function errorCount(){
  return count($this->error);
  } 
  
}

if(isset($_POST['Submit'])){

$singup =new singUp($_POST['email'],$_POST['password'],$_POST['password2']); 

$email =$singup->checkEmail();
$password =$singup->checkPassword();
$code = $singup->getCode();


// NEED HELP MAKE THIS PART OF MY CODE WORK
if(!$singup->errorCount()==0){

$query = "INSERT INTO tra_users(password,email,code,signed_up)VALUES('$password','$email','$code',now())";
echo $db->query($query);
}
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.