Tuesday, June 4, 2013

PHP - Register User

In a-simple-cms, we don't have a page that allows to create new user. So we create this page now and use jQuery form validation plugin to validate the input before forwarding the request.

Now the admin login page is








We create a admin register link to the registration page










See the code of admin registration page
if (isset($_POST['name'], $_POST['pass1'])) {

    include_once('../includes/connection.php');
    include_once('../includes/chash.php');

    $chash = new CHash();

    $name = $_POST["name"];
    $password = $_POST["pass1"];
    $phash = $chash->generateHash($password);

    $query = $pdo->prepare("INSERT INTO user (user_name, user_password) VALUES (?, ?)");
    $query->bindValue(1, $name);
    $query->bindValue(2, $phash);

    if($query->execute()){
        echo "admin created!";
    } else {
        echo "creation failed.";
    }

} else {
    // display registration form
}

Include css, jQuery library, jQuery validation plug in and custom validation code.




Code of custom.js
$(document).ready(function() {
    $("#form").validate({
        rules: {
            name: {
                required: true,
                minlength: 5,
                maxlength: 20
            },

            pass1: {
                required: true,
                minlength: 8,
                maxlength: 30
            },

            pass2: {
                required: true,
                minlength: 8,
                maxlength: 30,
                equalTo: "#pass1"
            },

            username: {
                required: true
            },

            password: {
                required: true
            }
        }
    });
});


Registration form
Admin Registration

See the form check result










Create admin with name "admin"










Created






Admin entry created in database





Because user_name is primary key, so we cannot create another admin with "admin" again.






All right, now we can login with the admin created



No comments:

Post a Comment