Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2. require_once('functions.php');
  3. dbConnect('localhost', 'root', '', 'website');
  4. session_start();
  5.  
  6. if(isset($_POST['submit'])){
  7.     if(isset($_POST['username'], $_POST['password'])) {
  8.     $username = $_POST['username'];
  9.     $password = $_POST['password'];
  10.     $errors = array();
  11.    
  12.     if(empty($username)) {
  13.         $errors[] = "Please enter a username.";
  14.     }
  15.    
  16.     if(empty($password)) {
  17.         $errors[] = "Please enter a password.";
  18.     }
  19.    
  20.     if(strlen($username) <= 3) {
  21.         $errors[] = "Please make sure your username is 4 or more characters long.";
  22.     }
  23.    
  24.     if(strlen($password) <= 4) {
  25.         $errors[] = "Please make sure your password is 5 or more characters long.";
  26.     }
  27.    
  28.     if(count($errors)) {
  29.         echo "The following errors occured:";
  30.         foreach($errors AS $error) {
  31.             echo $error."<br/>";
  32.         }
  33.     }else {
  34.         $sql = "SELECT `username` FROM `users` WHERE `username` = '$username'";
  35.         $res = mysql_query($sql);
  36.         $count = mysql_fetch_row($res);
  37.         if($count  < 0) {
  38.             //Insert the user into the db as the given username is available.
  39.             $insertMember = "INSERT INTO `users` (username, password) VALUES ({$username}, {$password})";
  40.             $addMember = mysql_query($sql) or die(mysql_error());
  41.             header("Refrehs: 5;url=login.php");
  42.             echo "You have successfully registered, redirecting you to the login page.";
  43.         }  else {
  44.             echo "Sorry but the given username is not available.";
  45.         }
  46.     }
  47.   }
  48. }
  49.  
  50. ?>
  51.  
  52. <html>
  53.     <head>
  54.         <title> Register </title>
  55.     </head>
  56.     <body>
  57.         <form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
  58.             <p> Username: <input type="username" name="username" /> </p>
  59.             <p> Password: <input type="password" name="password" /> </p>
  60.             <input type="submit" name="submit" value="Register" />
  61.         </form>
  62.     </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement