Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* In response to a Yahoo Answer's Question */
- /* Check if Page has been POSTed to */
- if( $_SERVER['REQUEST_METHOD'] !== 'POST'){
- echo '<h1>Invalid Permission</h1><p>Please Fill out the form first.</p>';
- exit;
- }
- /* Functions to help with checks */
- function minLength($str, $len){
- return (strlen($str) >= $len);
- }
- function maxLength($str, $len){
- return (strlen($str) <= $len);
- }
- function pvar($name, $mysqlEscape=false){
- $v = (isset($_POST[$name]))? $_POST[$name] : '';
- return ($mysqlEscape)? mysql_real_escape_string(trim($v)) : trim($v);
- }
- function hasValue(){
- $args = func_get_args();
- foreach($args as $arg){
- if(!isset($args{0})){
- return false;
- }
- }
- return true;
- }
- /* Connect To the database */
- $connect = mysql_connect("localhost","root","") or die('Internal Error. Couldn\'t connect to database');
- mysql_select_db("thorbis", $connect) or die('Internal Error. Could\'t select database');
- /* Variable Definitions with the help of pvar() function */
- $fullname = strip_tags(pvar('fullname', true));
- $username = strtolower(strip_tags(pvar('username', true)));
- $password = strip_tags(pvar('password', true));
- $repeatpassword = strip_tags(pvar('repeatpassword', true));
- $email = strip_tags(pvar('email', true));
- $firstname = pvar('firstname', true);
- $lastname = pvar('lastname', true);
- $phone = pvar('phone', true);
- $address1 = pvar('address1', true);
- $address2 = pvar('address2', true);
- $country = pvar('counry', true);
- $state = pvar('state', true);
- $city = pvar('city', true);
- $zip = pvar('zip', true);
- $date = date("Y-m-d");
- $errorMsg = '';
- /* Check for Input Values */
- if (!hasValue($fullname, $username, $password, $repeatpassword, $email, $firstname, $lastname, $phone, $address1, $address2, $country, $state, $city, $zip)){
- $errorMsg .= 'All Fields are Required.<br>';
- }
- /* Check for Existing Username */
- $namecheck = mysql_query("SELECT username FROM users WHERE username='$username' LIMIT 1") or die(mysql_error($connect));
- if( mysql_num_rows($namecheck) > 0){
- $errorMsg .= "Username is already Registered! Please select another.<br>";
- }
- /* Check Passwords Match and Length */
- if( !minLength($password, 6)){
- $errorMsg .= 'Password needs to be a minimum of 6 characters in length.<br>';
- if ($password !== $repeatpassword){
- $errorMsg .= 'Password and Confirmation Password DO NOT MATCH.<br>';
- }else{
- $password = md5($password);
- }
- /* Username Length Checks and Fullname length checks */
- if( !maxLength($username, 25) || !maxLength($fullname, 25)){
- $errorMsg .= "Length of username or fullname is too long!<br>";
- }
- /* Output Error Message, if there is one. Otherwise, register User. */
- if( strlen($errorMsg) > 0){
- echo $errorMsg;
- }else{
- $result = mysql_query( "INSERT INTO users(fullname, username, password,...) VALUES('{$fullname}','{$username}','{$password}',...)");
- if($result){
- echo "You have been registered! <a href='index.php'>click here</a> to go login";
- }else{
- echo 'There has been an Internal Error. Please Try Again Later. ';
- /* echo '<br>' . mysql_error($connect); */
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement