Jump to content

Help with new user registration form


ankh2054
 Share

Recommended Posts

Hi all,

Wonder if anyone can help me troubleshoot the below. 

I am working on a user registration form and I am taking it step by step.  The form displays but once I press submit I get a server error.

Could anyone help me with troubleshooting this.

I created a custom error log file for the sire, but nothing is logged apart from the GET and POST request.

<?php

require_once("/usr/share/php/PHPMailer-master/PHPMailerAutoload.php");
include("./functions.php");
include("./head.inc");
include("./navbar.inc");

$out = "";
$errors = "";

$form=  "<div class='row omb_row-sm-offset-3'>
                <div class='col-xs-12 col-sm-6'>    
                    <form class='omb_loginForm' action='./' accept-charset='UTF-8' autocomplete='off' method='post'>
                      
                        <div class='input-group'>
                            <span class='input-group-addon'><i class='fa fa-user'></i></span>
                            <input type='text' class='form-control' name='username' placeholder='Username'>
                        </div>
                        <span class='help-block'></span>

                        <div class='input-group'>
                            <span class='input-group-addon'><i class='fa fa-user'></i></span>
                            <input type='text' class='form-control' name='full_name' placeholder='Full Name'>
                        </div>
                        <span class='help-block'></span>

                         <div class='input-group'>
                            <span class='input-group-addon'><i class='fa fa-envelope'></i></span>
                            <input type='text' class='form-control' name='email' placeholder='Email'>
                        </div>
                        <span class='help-block'></span>
                                            
                        <div class='input-group'>
                            <span class='input-group-addon'><i class='fa fa-lock'></i></span>
                            <input  type='password' class='form-control' name='password' placeholder='Password'>
                        </div>
                        <span class='help-block'></span>
                        
                        <div class='input-group'>
                            <span class='input-group-addon'><i class='fa fa-lock'></i></span>
                            <input  type='password' class='form-control' name='password_confirm' placeholder='Confirm Password'>
                        </div>
                        <span class='help-block'></span>
                        
                        <button class='btn btn-lg btn-primary btn-block' type='submit' name='submit' value='register'>Register</button>
                    </form>
                </div>
            </div>"
;

if($input->post->submit) {

   
    
    //instantiate variables taking in the form data

    $username = $sanitizer->username($input->post->username);
    $email = $sanitizer->email($input->post->email);
    $password = $input->post->password;
    $full_name = $sanitizer->text($input->post->full_name);
    
    
   /*
    * Create the activation code
    * You can add a random string onto the
    * the username variable to keep people
    * from cracking the hash
    * ex $activation = md5($username."processwire");
    */

    $p = new Password();
    $activation = $p->randomBase64String(100); // 100=length of string  
    $activation_code = $config->httpHost."/activation/?user=".$username."&hash=".$activation;

                
    //check for errors on the form

    //Check if all the fields have been inputted
    if (empty($input->post->username) || empty($input->post->email) || empty($input->post->password)) {
                    $errors .= "Please fill out all fields marked with a *";
                    $out .= $form;
                } 
    //check if a valid email address was provided
    elseif (!filter_var($input->post->email, FILTER_VALIDATE_EMAIL)) {
                    $errors .= "Please include a valid email address";
                    $out .= $form;
                }

    //Check to ensure both passwords match
    elseif($input->post->password !== $input->post->password_confirm) {
                    $errors .= "Please ensure both Passwords match";
                    $out .= $form;
                } 

    //Check to ensure password is complex enough

    elseif (!preg_match("/[0-9]/", $input->post->password) || strlen($input->post->password) < 6) {
                    $errors .= "Please ensure your password has at least one digit and is at least 6 characters long";
                    $out .= $form;
                }

    /*
     * this checks to makesure that no one has the username
     * I have a functions file that I import to the pages I
     * need it on (funcions.php)
     */

    elseif(username_validation($username) == 0) {
        $out .= $form;

        
    if(strlen($username) != 0){
            if(username_validation($username) == 0) {
                $username->error = " ";
                $errors .= "Sorry that username is already taken!";
            }
        }
    }

   //the registration was successful
    else {
        
        $out = "Thank you for registering!<br><br>";
        $out .= "An email has just been sent to " . $email . " with the url to activate your account<br><br>";
        $out .= "Click here to continue to login";

    /*
     * In this example I am using phpmailer to send the email
     * I prefer this, but you can also use the mail() function
     */
     
        
        $mail = new PHPMailer();

        $mail->IsHTML(true);
        $mail->IsSMTP();  // telling the class to use SMTP
        $mail->Host     = "smtp.smtp.com"; // SMTP server

        $mail->From     = "test@test.com";
        $mail->FromName = "Register @ Test";
        $mail->AddAddress($email);
        $mail->AddReplyTo("test@test.com","Register @ Test");

        $mail->Subject  = "Registration";
        $mail->Body     = "
        Hi " . $full_name. ", <br>" .
        "Thanks for registering at Your Site.  To activate your email address click the link below! <br><br>"
        . "Activation Link: <a href='http://".$activation_code."'>".$activation_code."</a>";

        $mail->send();

        //create the new user
        $new_user = new User();
        $new_user->of(false);
        $new_user->name = $username;
        $new_user->email = $email;
        $new_user->pass = $password;
        $new_user->addRole("guest");
        $new_user->user_full_name = $full_name;
        $new_user->user_activation = $activation;
        $new_user->save();
        $new_user->of(true);
    }
}
//form not submitted
else {
    $out .= $form;
}
?>

<div class="container">
    
     <div><?php echo $errors; ?></div>
  
    <?php echo $out; ?>

</div>

<?php include("./foot.inc"); ?>
<?php include ("./java.inc"); ?>
Link to comment
Share on other sites

Hi found the problem, the below works. WOW I'm so proud, now onto the next steps.

<?php

include("./functions.php");

$error = false;

// form was submitted so we process the form
if($input->post->submit) {

  

     //Sanatize and assign variables data before creating user.

    $username = $sanitizer->username($input->post->username);
    $email = $sanitizer->email($input->post->email);
    $password = $sanitizer->text($input->post->password);
    $full_name = $sanitizer->text($input->post->full_name);
    

   
        //create the new user
        $new_user = new User();
        $new_user->of(false);
        $new_user->name = $username;
        $new_user->email = $email;
        $new_user->pass = $password;
        $new_user->addRole("guest");
        $new_user->user_full_name = $full_name;
        $new_user->save();
        $new_user->of(true);
    }

?>

<?php include("./head.inc"); ?>
<?php include ("./navbar.inc"); ?>


<div class="container">

            <div class="row omb_row-sm-offset-3">
                <div class="col-xs-12 col-sm-6">    
                    <form class="omb_loginForm" action="./" accept-charset="UTF-8" autocomplete="off" method="post">
                      
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user"></i></span>
                            <input type="text" class="form-control" name="username" placeholder="Username">
                        </div>

                          <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user"></i></span>
                            <input type="text" class="form-control" name="full_name" placeholder="Full Name">
                        </div>
                         <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                            <input type="text" class="form-control" name="email" placeholder="Email">
                        </div>
                        <span class="help-block"></span>
                                            
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                            <input  type="password" class="form-control" name="password" placeholder="Password">
                        </div>
                        <span class="help-block"></span>

                   

                        <button class="btn btn-lg btn-primary btn-block" type="submit" name="submit" value="register">Register</button>
                    </form>
                </div>
            </div>
        </div>
<?php include("./foot.inc"); ?>
<?php include ("./java.inc"); ?>
Link to comment
Share on other sites

You don't want to sanitize the password - you may end up changing someone's password! Instead, validate it against some rules you've set (most likely using Regex) - e.g. password length, allowed characters, etc....and alert user if it doesn't validate...

Read more here:

https://processwire.com/talk/topic/3543-register-users-and-add-page-same-as-username/?p=35151

https://processwire.com/talk/topic/5629-why-no-password-sanitization/

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...