Advertisement
valen1321

codigo

Apr 14th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.20 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. error_reporting(-1);
  4. ini_set('display_errors', 'On');
  5. require_once "config.php";
  6.  
  7. // Define variables and initialize with empty values
  8. $username = $name = $phone = $email = $password = $confirm_password = "";
  9. $username_err = $name_err = $phone_err = $email_err = $password_err = $confirm_password_err = "";
  10.  
  11. // Processing form data when form is submitted
  12. if($_SERVER["REQUEST_METHOD"] == "POST"){
  13.  
  14.     // Validate username
  15.     if(empty(trim($_POST["username"]))){
  16.         $username_err = "Please enter a username.";
  17.     } else{
  18.         // Prepare a select statement
  19.         $sql = "SELECT id FROM users WHERE username = ?";
  20.        
  21.         if($stmt = mysqli_prepare($link, $sql)){
  22.             // Bind variables to the prepared statement as parameters
  23.             mysqli_stmt_bind_param($stmt, "s", $param_username);
  24.            
  25.             // Set parameters
  26.             $param_username = trim($_POST["username"]);
  27.            
  28.             // Attempt to execute the prepared statement
  29.             if(mysqli_stmt_execute($stmt)){
  30.                 /* store result */
  31.                 mysqli_stmt_store_result($stmt);
  32.                
  33.                 if(mysqli_stmt_num_rows($stmt) == 1){
  34.                     $username_err = "This username is already taken.";
  35.                 } else{
  36.                     $username = trim($_POST["username"]);
  37.                 }
  38.             } else{
  39.                 echo "Oops! Something went wrong. Please try again lateru.";
  40.             }
  41.         }
  42.          
  43.         // Close statement
  44.         mysqli_stmt_close($stmt);
  45.     }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. ///////////////////////////////////////////////////////////////////////////
  54.     // Validate name
  55.     if(empty(trim($_POST["name"]))){
  56.         $name_err = "Please enter a name.";
  57.     } else{
  58.         // Prepare a select statement
  59.         $sql = "SELECT id FROM users WHERE name = ?";
  60.        
  61.         if($stmt = mysqli_prepare($link, $sql)){
  62.             // Bind variables to the prepared statement as parameters
  63.             mysqli_stmt_bind_param($stmt, "s", $param_name);
  64.            
  65.             // Set parameters
  66.             $param_name = trim($_POST["name"]);
  67.            
  68.             // Attempt to execute the prepared statement
  69.             if(mysqli_stmt_execute($stmt)){
  70.                 /* store result */
  71.                 mysqli_stmt_store_result($stmt);
  72.                
  73.                 if(mysqli_stmt_num_rows($stmt) == 1){
  74.                     $name_err = "This name is already taken.";
  75.                 } else{
  76.                     $name = trim($_POST["name"]);
  77.                 }
  78.             } else{
  79.                 echo "Oops! Something went wrong. Please try again latern.";
  80.             }
  81.         }
  82.          
  83.         // Close statement
  84.         mysqli_stmt_close($stmt);
  85.     }
  86.  
  87.  
  88. // Validate phone
  89.     if(empty(trim($_POST["phone"]))){
  90.         $phone_err = "Please enter a phone.";
  91.     } else{
  92.         // Prepare a select statement
  93.         $sql = "SELECT id FROM users WHERE phone = ?";
  94.        
  95.         if($stmt = mysqli_prepare($link, $sql)){
  96.             // Bind variables to the prepared statement as parameters
  97.             mysqli_stmt_bind_param($stmt, "s", $param_phone);
  98.            
  99.             // Set parameters
  100.             $param_phone = trim($_POST["phone"]);
  101.            
  102.             // Attempt to execute the prepared statement
  103.             if(mysqli_stmt_execute($stmt)){
  104.                 /* store result */
  105.                 mysqli_stmt_store_result($stmt);
  106.                
  107.                 if(mysqli_stmt_num_rows($stmt) == 1){
  108.                     $phone_err = "This phone is already taken.";
  109.                 } else{
  110.                     $phone = trim($_POST["phone"]);
  111.                 }
  112.             } else{
  113.                 echo "Oops! Something went wrong. Please try again laterp.";
  114.             }
  115.         }
  116.          
  117.         // Close statement
  118.         mysqli_stmt_close($stmt);
  119.     }
  120.  
  121.  
  122. // Validate email
  123.     if(empty(trim($_POST["email"]))){
  124.         $email_err = "Please enter a name.";
  125.     } else{
  126.         // Prepare a select statement
  127.         $sql = "SELECT id FROM users WHERE email = ?";
  128.        
  129.         if($stmt = mysqli_prepare($link, $sql)){
  130.             // Bind variables to the prepared statement as parameters
  131.             mysqli_stmt_bind_param($stmt, "s", $param_email);
  132.            
  133.             // Set parameters
  134.             $param_email = trim($_POST["email"]);
  135.            
  136.             // Attempt to execute the prepared statement
  137.             if(mysqli_stmt_execute($stmt)){
  138.                 /* store result */
  139.                 mysqli_stmt_store_result($stmt);
  140.                
  141.                 if(mysqli_stmt_num_rows($stmt) == 1){
  142.                     $email_err = "This email is already taken.";
  143.                 } else{
  144.                     $email = trim($_POST["email"]);
  145.                 }
  146.             } else{
  147.                 echo "Oops! Something went wrong. Please try again laterm.";
  148.                
  149.             }
  150.         }
  151.          
  152.         // Close statement
  153.         mysqli_stmt_close($stmt);
  154.     }
  155.  
  156.  
  157.  
  158.    
  159.     // Validate password
  160.     if(empty(trim($_POST["password"]))){
  161.         $password_err = "Please enter a password.";    
  162.     } elseif(strlen(trim($_POST["password"])) < 6){
  163.         $password_err = "Password must have atleast 6 characters.";
  164.     } else{
  165.         $password = trim($_POST["password"]);
  166.     }
  167.    
  168.     // Validate confirm password
  169.     if(empty(trim($_POST["confirm_password"]))){
  170.         $confirm_password_err = "Please confirm password.";    
  171.     } else{
  172.         $confirm_password = trim($_POST["confirm_password"]);
  173.         if(empty($password_err) && ($password != $confirm_password)){
  174.             $confirm_password_err = "Password did not match.";
  175.         }
  176.     }
  177.    
  178.     // Check input errors before inserting in database
  179.     if(empty($username_err) && empty($name_err) && empty($phone_err) && empty($email_err)  && empty($password_err)  && empty($confirm_password_err)){
  180.        
  181.         // Prepare an insert statement
  182.         $sql = "INSERT INTO users (username, name, phone, email, password) VALUES (?, ?, ?, ?, ?)";
  183.          
  184.         if($stmt = mysqli_prepare($link, $sql)){
  185.             // Bind variables to the prepared statement as parameters
  186.             mysqli_stmt_bind_param($stmt, "sssss", $param_username, $param_name, $param_phone, $param_email, $param_password);
  187.            
  188.             // Set parameters
  189.             $param_username = $username;
  190.             $param_name = $name;
  191.             $param_phone = $phone;
  192.             $param_email = $email;
  193.             $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  194.            
  195.             // Attempt to execute the prepared statement
  196.             if(mysqli_stmt_execute($stmt)){
  197.                 // Redirect to login page
  198.                 header("location: login.php");
  199.             } else{
  200.                 echo "Something went wrong. Please try again laterp.";
  201.  
  202.             }
  203.         }
  204.          
  205.         // Close statement
  206.         mysqli_stmt_close($stmt);
  207.     }
  208.    
  209.     // Close connection
  210.     mysqli_close($link);
  211. }
  212. ?>
  213.  
  214.  
  215.  
  216.  
  217. <!DOCTYPE html>
  218. <html lang="en">
  219. <head>
  220.     <meta charset="UTF-8">
  221.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  222.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  223.     <title>Sign Up Form by Colorlib</title>
  224.  
  225.     <!-- Font Icon -->
  226.     <link rel="stylesheet" href="fonts/material-icon/css/material-design-iconic-font.min.css">
  227.  
  228.     <!-- Main css -->
  229.     <link rel="stylesheet" href="css/style.css">
  230. </head>
  231. <body>
  232.  
  233.     <div class="main">
  234.  
  235.         <section class="signup">
  236.             <!-- <img src="images/signup-bg.jpg" alt=""> -->
  237.             <div class="container">
  238.                 <div class="signup-content">
  239.                    
  240.                     <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" id="signup-form" class="signup-form">
  241.                         <h2 class="form-title">Create account</h2>
  242.                         <div class="form-group">
  243.                             <input type="text" class="form-input" name="name" id="name" placeholder="Nombre" value="<?php echo $name; ?>">
  244.                         </div>
  245.                         <div class="form-group">
  246.                             <input type="email" class="form-input" name="email" id="email" placeholder="Correo electronico" value="<?php echo $email; ?>">
  247.                         </div>
  248.  
  249.                         <div class="form-group">
  250.                             <input type="phone" class="form-input" name="phone" id="phone" placeholder="Telefono" value="<?php echo $phone; ?>">
  251.                         </div>
  252.  
  253.  
  254.                         <div class="form-group">
  255.                             <input type="text" class="form-input" name="username" id="username" placeholder="Usuario" value="<?php echo $username; ?>">
  256.                         </div>
  257.  
  258.                         <div class="form-group">
  259.                             <input type="text" class="form-input" name="password" id="password" placeholder="Password" value="<?php echo $password; ?>">
  260.                             <span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
  261.                         </div>
  262.                         <div class="form-group">
  263.                             <input type="password" class="form-input" name="confirm_password" id="re_password" placeholder="Repeat your password" value="<?php echo $confirm_password; ?>">
  264.                         </div>
  265.                        
  266.                         <div class="form-group">
  267.                             <input type="submit" name="submit" id="submit" class="form-submit" value="Sign up"/>
  268.                         </div>
  269.                     </form>
  270.                     <p class="loginhere">
  271.                         Have already an account ? <a href="#" class="loginhere-link">Login here</a>
  272.                     </p>
  273.                 </div>
  274.             </div>
  275.         </section>
  276.  
  277.     </div>
  278.  
  279.     <!-- JS -->
  280.     <script src="vendor/jquery/jquery.min.js"></script>
  281.     <script src="js/main.js"></script>
  282. </body><!-- This templates was made by Colorlib (https://colorlib.com) -->
  283. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement