Advertisement
Guest User

I made dis

a guest
Apr 22nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.62 KB | None | 0 0
  1. <?php
  2.  
  3. //|-------------------------------------------------|
  4. //|   Author of this script is: BitSec              |
  5. //|   Day of Publication: 4-3-2017 (mm-dd-yy)       |
  6. //|   if you would like to distribute this script   |
  7. //|   on other forums then leave this here!         |
  8. //|-------------------------------------------------|
  9.  
  10.  
  11. include 'connect.php';
  12.  
  13.  
  14. //some settings for easy management
  15. //for $usernamemax and min you need to add 1 to what you want the max length to be
  16. $usernamemaxchars = '26';
  17. $usernameminchars = '3';
  18. //with website name please include www. if your site is unaccessable without (you can test it by going to your website without www. if it works then www. is not needed)
  19. $websitename = 'localhost';
  20. $websitenorep = 'no-reply$localhost.com';
  21. //set the login script page link in here
  22. $loginpage = '';
  23. //set this to 1 if you dont want a email verification. (the next tutorial will be an tutorial about how verification script)
  24. // you will find a link at the bottom of the post when the tutorial is online.
  25. $ranksetting = 0;
  26.  
  27.  
  28. //check if the username is not empty/set
  29. if (isset($_POST['username'])) {
  30.     //check if the username is between the 3 and 25 chars
  31.     if ((strlen($_POST['username']) < $usernamemaxchars) && (strlen($_POST['username']) > $usernameminchars)) {
  32.         //check if the username contains any not allowed chars (allowed chars are a to z A to Z 0 to 9 and _ and .)
  33.         if (!preg_match("/[^a-zA-Z0-9_\-.]/", $_POST['username'])) {
  34.             //create a query that selects the username from the table users where the username is equal to the username entered by the user
  35.             $query = $conn->prepare('SELECT username FROM users WHERE username = :username;');
  36.             //bind the parameters (protection agains SQL injections)
  37.             $query->bindparam(':username', $_POST['username']);
  38.             //Run the query in the database
  39.             $query->execute();
  40.             //get number of results of the query
  41.             $result = $query->rowCount();
  42.                 //check if the results is empty because that means no other user has this username.
  43.                 if ($result == 0) {
  44.                     //set the variable username to the username the user has posted
  45.                     $username = $_POST['username'];
  46.             //if a check returns false then echo the error message and stop the php code from running
  47.             }else{echo "The username you`ve entered is already in use"; exit();}
  48.         }else{echo "username can only have letters, numbers, underscore and a dot"; exit();}
  49.     }else{echo "username needs to be at least 2chars long and max 25chars long"; exit();}
  50. }else{echo "you need to fill in a username"; exit();}
  51.  
  52.  
  53. //check if the user filled in an password and a retypepassword field
  54. if ((isset($_POST['password'])) && (isset($_POST['retypepassword']))) {
  55.     //check if the passwords match eachother (just a security on typos)
  56.     if ($_POST['password'] == $_POST['retypepassword']) {
  57.         //check if the password is at least 2 chars long (we dont want anyone to have an empty password)
  58.         if (strlen($_POST['password'] > $usernameminchars)) {
  59.             //when everything is good set the variable to the password the user has entered in the password field
  60.             $password = $_POST['password'];
  61.         //if something returns false then is is not good. So we stop the php code from running and we return an error message so the user knows whats wrong.
  62.         }else{echo "password needs to be at least 2 characters long"; exit();}
  63.     }else{echo "the passwords you`ve entered do not match"; exit();}
  64. }else{echo "you need to fill in a password and retype it"; exit();}
  65.  
  66.  
  67.  
  68. //check if the user filled in an email and if user has retyped the email to confirm it
  69. if ((isset($_POST['email'])) && (isset($_POST['retypeemail']))) {
  70.     //check if the email is the same as the retypedemail because we dont want to store a wrong email address.
  71.     if ($_POST['email'] == $_POST['retypeemail']) {
  72.         //check if the email is a valid email address
  73.         if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  74.             //crete a query that selects the email from the table users where the email equals the email entered by the user
  75.             $query = $conn->prepare('SELECT email FROM users WHERE email = :email;');
  76.             //bind parameters (protection against SQL inejctions)
  77.             $query->bindparam(':email', $_POST['email']);
  78.             //execute the query
  79.             $query->execute();
  80.             //run the query in the database
  81.             $result = $query->rowCount();
  82.                 //check if the results is empty because that means no other user has this email so we can use it :)
  83.                 if ($result == 0) {
  84.                     //set variable to the users email so we can use it later in the code.
  85.                     $email = $_POST['email'];
  86.             //if something returns false then is is not good. So we stop the php code from running and we return an error message so the user knows whats wrong.
  87.             }else{echo "the email you`ve entered is already in use"; exit();}
  88.         }else{echo "the email you`ve entered is not a valid email address"; exit();}
  89.     }else{echo "the email addresses you`ve entered do not match"; exit();}
  90. }else{echo "you need to fill in your email and retypeemail"; exit();}
  91.  
  92.  
  93.  
  94. //check if the refferal is empty or not (because a refferal isnt needed we dont show a message if the refferal is empty)
  95. if (isset($_POST['refferal'])) {
  96.     if ($_POST['refferal'] !== "") {
  97.     //check if the refferal is between the 3 and 25 chars
  98.     if ((strlen($_POST['refferal'] < $usernamemaxchars)) && (strlen($_POST['refferal'] > $usernameminchars))) {
  99.         //check if the refferal contains any not allowed chars (allowed chars are a to z A to Z 0 to 9 and _ and .)
  100.         if (!preg_match("/[^a-zA-Z0-9_\-.]/", $_POST['refferal'])) {
  101.             //create a query that selects the refferal from the table users where the refferal is equal to the refferal entered by the user
  102.             $query = $conn->prepare('SELECT username FROM users WHERE username = :refferal;');
  103.             //bind the parameters (protection agains SQL injections)
  104.             $query->bindparam(':refferal', $_POST['refferal']);
  105.             //Run the query in the database
  106.             $query->execute();
  107.             //get the number of results from the query
  108.             $result = $query->rowCount();
  109.                 //check if the results is empty becaase if the refferal doesnt exsist then we cant give him a refferal point (or an other reward for POSTting players to join)
  110.                 if ($result != 0) {
  111.                     //set the variable refferal to the refferal the user has posted
  112.                     $refferal = $_POST['refferal'];
  113.             //if a check returns false then echo the error message and stop the php code from running
  114.             }else{echo "The refferal you`ve entered does not exsist"; exit();}
  115.         }else{echo "refferal can only have letters, numbers, underscore and a dot"; exit();}
  116.     }else{echo "refferal name is to short or to long"; exit();}
  117. //we are not setting an error message here because the refferal is not needed to fill in
  118.     }
  119. }
  120.  
  121.  
  122. // A higher "cost" is more secure but consumes more processing power
  123. //if you would like to know more about password hashing then check the links below :)
  124. $cost = 10;
  125. // Create a random salt
  126. $salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
  127. // Here we are setting prefix information about the hash so PHP knows how to verify it later so we can check if the password is really the users password.
  128. // "$2a$" Means we are using the Blowfish algorithm. The following two digits are the cost parameters.
  129. $salt = sprintf("$2a$%02d$", $cost) . $salt;
  130. // Hash the password with the salt
  131. $password = crypt($password, $salt);
  132.  
  133.  
  134. //After we have checked all the Value that the user can post on errors we will continue to the real register script
  135. //insert our new user into our database so he can login the next time he visited
  136. //dont want a email verif system? Set the 0 to a 1 at the right from NULL and VALUES                    --------v--------
  137. $query = $conn->prepare('INSERT INTO users (rank, username, password, email, Date, Refferals) VALUES ('.$ranksetting.', :username, :password, :email, NOW(), 0');
  138. //bind some params (security against SQL injections) but i hope you know how you can bind params by now
  139. $query->bindparam(':username', $username);
  140. $query->bindparam(':password', $password);
  141. $query->bindparam(':email', $email);
  142. //Execute the query but i think you already know by now :)
  143. $query->execute();
  144.  
  145.  
  146. //here we are setting some sessions. we have two options to do here.
  147. //1. we can redirect them to their user profile without them having to login. OR
  148. //2. we can first send them a verification email wich will also use the sessions to redirect them to the user profile without having them to login
  149. $_SESSION['username'] = $username;
  150. //we dont need to encrypt ot secure the password because we already have that at lines 118 - 129
  151. $_SESSION['password'] = $password;
  152. //the rank in the database is zero but im also setting a session to zero so we dont have to use a database to check if they are allowed to login.
  153. //i will set the rank in the session and in the database to 1 once they have verified their email address. and then they can go to their homepage without having to login first.
  154. //Dont want a email verif system? just set the 0 to a 1
  155. $_SESSION['rank'] = $ranksetting;
  156.  
  157. if ($ranksetting == 0) {
  158. //dont want a email verif system? remove this block of code until 2 blank spaces
  159. // Hash the username with the salt
  160. $hash = crypt($username, $salt);
  161. //setting the actual link variable
  162. //set verif.php to whatever your verify system is. in my case its called verif.php
  163. if ($refferal > 0) {
  164.     $veriflink = $websitename.'/verify.php?v='.$hash.'&u='.$username.'&r='.$refferal;
  165. }else{
  166.     $veriflink = $websitename.'/verify.php?v='.$hash.'&u='.$username;
  167. }
  168. //so we will set some variables now to use for sending the email later. variables speak for themselves.
  169. $to = $email;
  170. //setting the subject (make sure the user knows where it came from)
  171. $subject = $websitename.' - Verification Email';
  172. //setting the message
  173. $message = '
  174. Hello '.$username.' you are receiving this message because you need to verify your email before you can login on our site.
  175. If you are not '.$username.' then just ignore or remove this message. Thanks!
  176. If you cannot click the link then just copy and paste it in your URL bar or Address bar
  177. Your verification link is: '.$veriflink;
  178. //setting some headers can containt (From, Reply-To, X-Mailer etc)
  179. $headers = 'From: '.$websitenorep;
  180. //So now we send an verification email for the user so he can verify himself.
  181. //we can later use the same system as the password check so we know the code is real.
  182. mail($to, $subject, $message, $headers);
  183. echo "Thank you for registering! please check your mailbox for the verification link";
  184. }else{
  185. //show congratz message or whatever you like.
  186. echo "You are now Member! You will be automaticly logged in less then 5 seconds!";
  187. //update the rank so we can log the user in without having to use the database.
  188. //because he/she has rank 0 by default so we want to change it to 1 wich equals Verified User (verif system also protects againts Account Duplicates)
  189. $_SESSION['rank'] = 1;
  190. //we are setting logged in to true because we want to log him in automaticly
  191. $_SESSION['LoggedIn'] = True;
  192.  
  193. header('location: '.$redirectpage);
  194. }
  195.  
  196. ?>
  197.  
  198.  
  199. Password Hashing Information:
  200. http://jeremykendall.net/2014/01/04/php-password-hashing-a-dead-simple-implementation/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement