Page 1 of 1

[Solved] Error Handling

Posted: Sun Mar 08, 2009 2:52 pm
by Goldeneye
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)

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>');
        }
?>
And then here is the registration page (known as register.php)

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">&nbsp;</label> <input type="submit" class="button" name="action" value="Register!" id="takeaction">
            </form>';
    $layout->output(array('tpl.mainb' => $mainb));
    oFooter();
?>

Re: Error Handling

Posted: Sun Mar 08, 2009 8:08 pm
by Benjamin
You aren't returning the error array.

Code: Select all

 
return array('', '', 'You now have yourself an account which means you can <a href="'.sp.'login.php">Login</a>');
 

Re: Error Handling

Posted: Sun Mar 08, 2009 9:03 pm
by Goldeneye
I actually was returning the error-array (if you were to look about 2 or 3 lines above the line you posted). The problem was that I was placing a null-variable (known as $err) in the array when I was assigning all the errors to the $this->err[] array. Anyways, thanks a lot for your input, it may not have been completely correct, but it did make me take a closer look at the $user->add function. Thanks! :)

Re: [Solved] Error Handling

Posted: Sun Mar 08, 2009 9:15 pm
by Benjamin
Ah ok, see, this is why braces are recommended. Had your if statement been using braces I would have noticed that. Glad it's fixed now.