[Solved] Error Handling
Posted: Sun Mar 08, 2009 2:52 pm
I've created a User-Registration Class that add users when all the correct parameters are met or returns an error when only some of the parameters are correctly met. The way I have it now, it only spits a single error at a time (so say you left two of the fields blank, it would only tell you that you left the first one blank). I want it to return all the errors in a list, rather than one at a time. I thought I did it correctly but it always returns the 'Warning: Invalid argument supplied for foreach()' error. Here's how I have it now...
This is the class (declared as $user)
And then here is the registration page (known as register.php)
This is the class (declared as $user)
Code: Select all
<?php
function add($pseudonym, $passkey, $email){
$this->foreign();
$pseudonym = trim($pseudonym);
$email = trim($email);
if(empty($pseudonym)) $this->err[] = 'The pseudonym field is empty.';
if(empty($passkey)) $this->err[] = 'The passkey field is empty.';
if(empty($email)) $this->err[] = 'The E-Mail field is empty.';
if(!empty($pseudonym) && !empty($passkey) && !empty($email)){
if(!preg_match()) //Check if the Username contains invalid characters
$this->err[] = 'Your pseudonym contains invalid characters and/or consecutive spaces.';
if(strlen($passkey) < 4)
$this->err[] = 'Your passkey must be atleast 4 characters long.';
if(!preg_match()) //Make sure the E-Mail is atleast in a valid format
$this->err[] = 'The format of your E-Mail is invalid.';
$query = $this->sql->query(); //Check if the submitted Username or E-Mail exist
if($this->sql->numRows($query) != 0 && !empty($email))
$this->err[] = 'The Pseudonym and/or Email address you submitted are already taken.';
}
if(count($this->err)!=0)
return array($err, '', '');
$this->sql->query(); //Insert New User
return array('', '', 'You now have yourself an account which means you can <a href="'.sp.'login.php">Login</a>');
}
?>Code: Select all
<?php
if(isset($_POST['action'])){
if($_SESSION['lock'] != $_POST['key']) $errs[] = 'Are you a bot, or not?';
else list($errs, $war, $aff) = $user->add($_POST['pseudonym'], $_POST['passkey'], $_POST['email']);
$_SESSION['lock']='';
}
if(!isset($_SESSION['lock']) || $_SESSION['lock']==''){$lock=$data->keyGen('num', 6, 2, ':'); $_SESSION['lock']=$lock;}
oHeader('Register', 'document.register.pseudonym.focus();');
$mainb = '<h1>Memberships, Accounts, et Cetera.</h1>';
if(isset($_POST['action']))
foreach($errs as $err) $mainb .= '<p class="err">'. $err .'</p>';
$mainb .= '<form name="register" action="'.sp.'register.php" method="post"><label for="pseudonym">Pseudonym:</label> <input type="text" class="txt" name="pseudonym" value="' . stripslashes($_POST['pseudonym']) . '" id="pseudonym"><br>
<label for="passkey">Passkey:</label> <input type="password" class="txt" name="passkey" id="passkey"><br>
<label for="email">Email:</label> <input type="text" class="txt" name="email" value="' . stripslashes($_POST['email']) . '" id="email"><br>
<label for="rureal">Anti-Spam:</label><input type="text" class="txt" name="key" id="rureal">' . $_SESSION['lock'] . '<br>
<label for="foobar"> </label> <input type="submit" class="button" name="action" value="Register!" id="takeaction">
</form>';
$layout->output(array('tpl.mainb' => $mainb));
oFooter();
?>