Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////// register.php
- $username = filter_var( $_POST[ 'username' ], FILTER_SANITIZE_STRING );
- $email = filter_var( $_POST[ 'email' ], FILTER_SANITIZE_STRING );
- $password = filter_var( $_POST[ 'password' ], FILTER_SANITIZE_STRING );
- $password2 = filter_var( $_POST[ 'password2' ], FILTER_SANITIZE_STRING );
- $tos = filter_var( $_POST[ 'tos' ], FILTER_SANITIZE_STRING );
- $session_key = randomstring( 64 );
- $ip = getIP();
- $registerUser = new register( $username, $email, $password, $password2, $tos, $ip, $session_key, $con );
- $registerUser -> test();
- if ( $registerUser -> isFailed() ) {
- echo $registerUser -> getErrors();
- $registerUser = null;
- } else {
- $registerUser -> register();
- echo 'true';
- }
- //////////////////////////// register_class.php
- <?php
- class register{
- private $username;
- private $email;
- private $password;
- private $password2;
- private $tos;
- private $ip;
- private $session_key;
- private $errorMSGS = array( 'error' => array() );
- private $registrationFailed;
- private $con;
- function __construct( $username, $email, $password, $password2, $tos, $ip, $session_key, $con ){
- $this -> username = $username;
- $this -> email = $email;
- $this -> password = $password;
- $this -> password = $password2;
- $this -> tos = $tos;
- $this -> ip = $ip;
- $this -> session_key = $session_key;
- $this -> con = $con;
- }
- public function isFailed() {
- if ( $this -> registrationFailed ) {
- return true;
- } else {
- return false;
- }
- }
- public function getErrors() {
- return json_encode( $this -> errorMSGS );
- }
- public function test() {
- $sql = $this -> con -> prepare( "Select username from forum_users where username = ?" );
- $sql -> bind_param( 's', $this -> username );
- $sql -> execute();
- $sql -> bind_result( $checkusername );
- $sql -> fetch();
- $sql -> close();
- $sql = $this -> con -> prepare( "Select email from forum_users where email = ?" );
- $sql -> bind_param( 's', $this -> email );
- $sql -> execute();
- $sql -> bind_result( $checkemail );
- $sql -> fetch();
- $sql -> close();
- if ( $checkusername != null ) {
- $this -> registrationFailed = true;
- array_push( $this -> errorMSGS[ 'error' ], "This username is already taken!");
- }
- if ( $checkemail != null ) {
- $this -> registrationFailed = true;
- array_push( $this -> errorMSGS[ 'error' ], "This email is already taken!" );
- }
- if ( !filter_var( $this -> email, FILTER_VALIDATE_EMAIL ) ) {
- $this -> registrationFailed = true;
- array_push( $this -> errorMSGS[ 'error' ], "Email is invalid!" );
- }
- if ( $this -> tos == 'false' ) {
- $this -> registrationFailed = true;
- array_push( $this -> errorMSGS[ 'error' ], "You have to agree to Terms of Service!" );
- }
- if ( strlen( $this -> username ) < 3 ) {
- $this -> registrationFailed = true;
- array_push( $this -> errorMSGS[ 'error' ], "Username must be at least 3 characters long!" );
- }
- if ( strlen( $this -> password ) < 8 ) {
- $this -> registrationFailed = true;
- array_push( $this -> errorMSGS[ 'error' ], "Password must be at least 8 characters long!" );
- }
- if ( $this -> password == $this -> password2) {
- $this -> registrationFailed = true;
- array_push( $this -> errorMSGS[ 'error' ], "Passwords do no match!" );
- }
- }
- public function register() {
- $uuid = randomstring( 32 );
- $salt = sprintf( "$2a$%02d$", 10 ) . randomstring( 55 );
- $encrypedpass = crypt( $this -> password, $salt );
- $rank = 'member';
- $sql = $this -> con -> prepare( "insert into forum_users ( uuid, username, email, password_hash, salt, display_name, rank ) values ( ?, ?, ?, ?, ?, ?, ? )" );
- $sql -> bind_param( 'sssssss', $uuid, $this -> username, $this -> email, $encrypedpass, $salt, $this -> username, $rank );
- $sql -> execute();
- $sql -> close();
- $sql = $this -> con -> prepare( "insert into forum_sessions ( session_key, ip, userUUID ) values ( ?, ?, ? )" );
- $sql -> bind_param( 'sss', $this -> session_key, $this -> ip, $uuid );
- $sql -> execute();
- $sql -> close();
- session_start(['cookie_lifetime' => 259200]);
- setcookie("forum", $this -> session_key, time() + ( 259200 ), '/');
- }
- private function randomstring( $size ){
- $list = array( 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' );
- $string = '';
- for( $x = 0; $x < $size; $x++ ){
- $string = $string . $list[ rand( 0, count( $list ) - 1 ) ];
- }
- return $string;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement