Genesis730 Posted June 29, 2011 Share Posted June 29, 2011 So I have this site I'm trying to do and thought it would be a lot shorter and easier to modify if you only had to use one function to check for errors for a particular type of input (username, email, password, name, etc.) So instead of copying each function and having it in 5 places, only use one function... Now I do have it working, I think... However it's not idiot-proof reason being if they input a : then it messes up. So my question is this, does anybody know if there is a better approach at this or can anyone improve on my code to make it function the way i'd like it to??? Thanks in advance *** INDEX.PHP *** <?PHP session_start(); // Start our session include("display_forms.php"); // Include display_forms.php to display the login form include("functions.php"); if($_SERVER["REQUEST_METHOD"] == "POST"){ // TEST IF form was submitted and method is POST (login form) $username = $_POST['quickUser']; $password = $_POST['quickPass']; $rememberMe = $_POST['rememberMe']; if(checkLogin($username, $password, $rememberMe)){ // Functions.php -> checkLogin function // Login Successful! Set session variables $_SESSION['loggedIn'] = true; $_SESSION['username'] = $username; } else { // Something went wrong, display errors } } if($_SESSION['loggedIn']){ echo "<br /><div align='right'>Welcome back ".$username."</div>"; echo "[ <a href='functions.php'>Logout</a> ]"; } else { form_quickLogin(); } ?> *** DISPLAY_FORMS.PHP *** <?PHP function form_quickLogin() { echo "<form action='index.php' method='POST'> <table cellspacing='0' cellpadding='0' border='0' align='right'> <tr> <td align='right'>Username </td><td align='left'><input type='text' class='input' name='quickUser' value='$username' maxlength='30'></td> </tr><tr> <td colspan='2' align='right'>"; if (isset($error[username])){ $error = $functions->$error['username']; echo "<font color='#FF0000'>$error</font>"; } else { echo " "; } echo "</td> </tr><tr> <td align='right'>Password </td><td align='left'><input type='password' class='input' name='quickPass' value='$password' maxlength='30'></td> </tr><tr> <td colspan='2' align='right'>"; if (isset($_SESSION['$empty']) && in_array('quickPass', $_SESSION['$empty'])){ echo "<font color='#FF0000'>Please Enter Password</font>"; } else { echo " "; } echo "</td> </tr>"; if(REMEMBER_ME) { echo "<tr><td colspan='2' align='right'><br />Remember Me<input type='checkbox' class='checkbox' name='rememberMe' $rememberMe><input type='hidden' name='quickLogin' value='1'> <input type='submit' class='submit' value='Login'></td> </tr><tr> <td colspan='2' align='right'><br />[ <a href='forgotpass.php'>Forgot Pass</a> ] - [ <a href='register.php'>Register</a> ]</td> </tr> </table></form>"; } else { echo "<tr><td colspan='2' align='right'><br /><input type='hidden' name='quickLogin' value='1'> <input type='submit' name='quickLogin' class='submit' value='Login'></td> </tr><tr> <td colspan='2' align='right'><br />[ <a href='forgotpass.php'>Forgot Pass</a> ] - [ <a href='register.php'>Register</a> ]</td> </tr> </table></form>"; } } ?> *** FUNCTIONS.PHP *** <?PHP session_start(); include("errors.php"); function checkLogin($username,$password,$rememberMe){ $username = "username:".$username; $password = "password:".$password; errorCheck($username,$password,$rememberMe); } function logout() { unset($_SESSION['loggedIn']); unset($_SESSION['username']); unset($_SESSION); session_destroy(); return; } ?> *** ERRORS.PHP *** <?PHP function errorCheck() { $error = array(); $numargs = func_num_args(); $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { $explode = explode(":", $arg_list[$i]); $field = $explode[0]; $value = $explode[1]; if($field == "username"){ checkUsername($value); } if($field == "password"){ checkUsername($value); } if($field == "email"){ checkUsername($value); } } } function checkUsername($username) { $field = "username"; if(!isset($username)) { // Username not entered $error[username] = "Username Blank"; // Resulting error message return $error[username]; } elseif(strlen(trim($username)) < 5) { // Username minimum length $error['username'] = "Username Too Short"; // Resulting error message return false; } elseif(strlen(trim($username)) > 32) { // Username maximum length $error['username'] = "Username Too Long"; // Resulting error message return false; } else { return true; } } function checkPassword($password) { $field = "password"; if(!isset($password)) { // Password not entered $error[password] = "Password Blank"; // Resulting error message return false; } elseif(strlen(trim($password)) < 5) { // Password minimum length $error['password'] = "Password Too Short"; // Resulting error message return false; } elseif(strlen(trim($password)) > 32) { // Password maximum length $error['password'] = "Password Too Long"; // Resulting error message return false; } else { return true; } } function checkEmail($email) { $field = "emailaddress"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240746-error-checking/ Share on other sites More sharing options...
xyph Posted June 29, 2011 Share Posted June 29, 2011 It's not recommended, but for something small n simple like this you could use a static class. <?php class error { private static $errors = array(); public static function add() { $argCount = func_num_args(); self::$errors[] = func_get_args(); } public static function get() { return self::$errors; } public static function exists() { return !empty(self::$errors); } } $arg = FALSE; if( $arg == FALSE ) { error::add('argument','argument'); } error::add('even','more','arguments'); if( error::exists() ) print_r( error::get() ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/240746-error-checking/#findComment-1236610 Share on other sites More sharing options...
Genesis730 Posted June 30, 2011 Author Share Posted June 30, 2011 How could I implement that into my code and be able to return the error string(s) onto the prior page with the form... Quote Link to comment https://forums.phpfreaks.com/topic/240746-error-checking/#findComment-1236626 Share on other sites More sharing options...
vbconz Posted June 30, 2011 Share Posted June 30, 2011 How could I implement that into my code and be able to return the error string(s) onto the prior page with the form... Hi Genesis730. Two things: 1 - There is a solution further down the page 2 - There is a logic error in your code. $explode = explode(":", $arg_list[$i]); $field = $explode[0]; $value = $explode[1]; if($field == "username"){ checkUsername($value); } if($field == "password"){ checkUsername($value); // ******** } if($field == "email"){ checkUsername($value); //******** } } I think the code should look like: if($field == "username"){ checkUsername($value); } if($field == "password"){ checkPassword($value); //***** changed here } if($field == "email"){ checkEmail($value); //***** changed here } } 1 - A solution - Now i am still coming to grips with php but to get around the issue you have with full colons ( : ) either a) Dont use explode - Parse along the argument and as soon as you get to the first : in the string you know you have the field name and the values are everything to the right of the first full colon e.g. $numargs = func_num_args(); $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { $colonPosition = strpos ( arg_List[i], ':') ; $field = substr( arg_List[i], 0, $colonPosition -1); //functionally a left$ $value = substr( arg_List[i], $colonPosition +1); // functionally a mid$ .... } b) Make your seperator between field and value really out there ie e.g. $password = "password~~!~".$password; ... $explode = explode("~~!~", $arg_list[$i]); c) Change your code so instead of calling three or four functions deep (which is harder to debug and maintain and read etc) call it all in oe line from your php page. At present <?PHP calls checkLogin checkLogin calls errorCheck errorcheck calls checkUserName and checkPassword and checkEmail. but you could change your php page to <?php> .... $username = $_POST['quickUser']; $password = $_POST['quickPass']; $rememberMe = $_POST['rememberMe']; if(checkUserName && CheckPassword && checkEmail )) = true { // Functions.php -> checkLogin function // Login Successful! Set session variables $_SESSION['loggedIn'] = true; At present if you add another parameter to check you need to change / maintain three or four functions. Here you only have to maintain the php and add another checkAAAAAA function as required. If you do use the above then you will nedd to get checkEmail to return true as at present it is just blank Quote Link to comment https://forums.phpfreaks.com/topic/240746-error-checking/#findComment-1236635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.