Advertisement
Guest User

Simple signup/ login example by Miro Balearski

a guest
Feb 4th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1. <?php
  2.  
  3. /****
  4. Db schema
  5.  
  6. create table users (id int(10) not null primary key auto_increment,
  7.                    username varchar(255) not null unique,
  8.                    password varchar(255) not null,
  9.                    email varchar(255) not null default '');
  10.  
  11. ****/                  
  12.                    
  13.                    
  14. function get_dblink(){
  15.  
  16.     return  mysqli_connect("localhost","db_username","db_password","db_name");
  17. }
  18.  
  19. function register_form(){?>
  20.     <form method="post" >
  21.         Username :<input type = "text" name="username">
  22.         Passwod: <input type = "password" name="pass">
  23.         Retype Passwod:<input type = "password" name="pass2">
  24.         <input type="hidden" name="action" value="register">
  25.         <input type="submit" value="Login">
  26.     </form>
  27.     <?php
  28. }
  29.  
  30. function login_form(){
  31.    
  32.     ?>
  33.     <form method="post">
  34.         Username :<input type = "text" name="username">
  35.         Passwod: <input type = "password" name="pass">
  36.         <input type="hidden" name="action" value="login">      
  37.         <input type="submit" value="Login">
  38.     </form>
  39.     <?php
  40. }
  41.  
  42. function logout_form($username){ ?>
  43.     <form method="post">
  44.         <input type="hidden" name="action" value="logout">     
  45.         Logout <?=$username?> <input type="submit" value="Logout">
  46.     </form>
  47.     <?php
  48. }
  49.  
  50.  
  51. /**************************************************************/
  52.  
  53.  
  54. #Validators :
  55.  
  56. function validate_username($username){
  57.    
  58.     if(preg_match('/^[a-zA-Z0-9]{5,}$/', $username)) {
  59.         return true;
  60.     }else{
  61.         throw new Exception("The username should contain a-zA-Z0-9 and should be long 5 chars or more ") ; 
  62.     }
  63. }
  64.  
  65. function validate_password($pass){
  66.    
  67.     if(preg_match('/^[a-zA-Z0-9]{5,}$/', $pass)) {
  68.         return true;
  69.     }else{
  70.         throw new Exception("The password should contain a-zA-Z0-9 and should be long 5 chars or more ");
  71.     }
  72. }
  73. function validate_password_dont_match_the_username($pass, $username){
  74.    
  75.     if($pass===$username){
  76.         throw new Exception("The password can not be the same as the username");   
  77.     }else{
  78.         return true;
  79.     }
  80. }
  81.  
  82. function validate_registration_passwords_match($pass, $pass2){
  83.    
  84.     if($pass===$pass2){
  85.         return true;
  86.     }
  87.     throw new Exception("The passwords don't match");
  88. }
  89.  
  90.  
  91. # DB functions ################################################
  92.  
  93. function authenticate_user($username, $password){
  94.    
  95.     $username= mysqli_real_escape_string(get_dblink(),$username);
  96.    
  97.     $qry = "select id, username, password from users where username='$username' ";
  98.     $res= mysqli_query(get_dblink(), $qry);
  99.     $rr= mysqli_fetch_assoc($res);
  100.    
  101.     return password_verify($password, $rr['password']);
  102. }
  103.  
  104. # Register user
  105.  
  106. function add_user_to_the_database($username,$pass){
  107.    
  108.     $dblink=get_dblink();
  109.    
  110.     $hashed_pass = password_hash($pass, PASSWORD_BCRYPT);
  111.     $qry = "insert into users(username, password) values ('$username', '$hashed_pass')";
  112.  
  113.     mysqli_query( $dblink ,$qry);
  114.     if (mysqli_error($dblink)){
  115.         throw new Exception("Error inserting into the DB ". mysqli_error($dblink));
  116.     }  
  117. }
  118.  
  119.  
  120.  
  121.  
  122. function register_user($username, $pass, $pass2){
  123.    
  124.     try{
  125.         validate_username($username);
  126.         validate_password_dont_match_the_username($pass,$username);
  127.         validate_password($pass);
  128.         validate_registration_passwords_match($pass, $pass2);
  129.        
  130.             # and finally
  131.         add_user_to_the_database($username,$pass);
  132.  
  133.             # todo email validation
  134.             # or
  135.             # automaticaliy login the new user;
  136.        
  137.         login_user($username, $pass);
  138.    
  139.     } catch (Exception $e){
  140.        
  141.         print_r("<div style='color:red'>".$e->getMessage()."</div>");
  142.     }
  143. }
  144.  
  145. function get_user_id_by_username($username){
  146.    
  147.     $qry = "select id  from users where username='$username' ";
  148.     $res= mysqli_query(get_dblink(), $qry);
  149.     $rr= mysqli_fetch_assoc($res);
  150.     return $rr['id'];
  151. }
  152.  
  153. function login_user($username, $password){
  154.    
  155.     if(authenticate_user($username, $password)){
  156.         $_SESSION['valid_user_id'] = get_user_id_by_username($username);
  157.         $_SESSION['username']=$username;
  158.        
  159.     }
  160. }
  161.  
  162. function loggedin_user(){
  163.     return $_SESSION['valid_user_id'];
  164. }
  165.  
  166. function logout_user(){
  167.     session_destroy();
  168. }
  169.  
  170. /***************************************************/
  171.  
  172. session_start();
  173.  
  174. # handle http post requests
  175.  
  176. if(isset($_POST['action']))
  177.     switch($_POST['action']){
  178.        
  179.         case "login":
  180.             login_user($_POST['username'], $_POST['pass']);
  181.         break;
  182.         case "logout":
  183.             logout_user();
  184.         break;  
  185.         case "register":
  186.             register_user($_POST['username'], $_POST['pass'], $_POST['pass2']);
  187.         break;
  188.     }
  189.  
  190. # application
  191.  
  192. if(!loggedin_user()){
  193.    
  194.     # show_some_content_for_NOT_logged_in_users_here();
  195.    
  196.     echo "<h1>You are not logged in. Please Login or Register</h1>";    
  197.     echo "<br><H2>Register:</H2>";
  198.  
  199.     register_form();
  200.  
  201.     echo "<br><H2>Login:</H2>";
  202.     login_form();
  203.    
  204.    
  205.    
  206.     # not registered users exprience stops here
  207.     exit(0);
  208. }
  209.  
  210.  
  211.  
  212. # This content will be available only for logged in users
  213.  
  214. echo "<h1>You are logged in as \"$_SESSION[username]\" </h1>";  
  215. logout_form($_SESSION['username']);
  216.  
  217. # show_content_for_logged_in_users_only();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement