dasun101

Untitled

Dec 20th, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // variable declaration
  5. $username = "";
  6. $email    = "";
  7. $errors = array();
  8. $_SESSION['success'] = "";
  9.  
  10. include 'dbCon.php';
  11.  
  12. // register
  13. if (isset($_POST['reg_user'])) {
  14.     // receive all input values from the form
  15.     $username = mysqli_real_escape_string($db, $_POST['username']);
  16.     $email = mysqli_real_escape_string($db, $_POST['email']);
  17.     $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  18.     $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
  19.  
  20.     // form validation: ensure that the form is correctly filled
  21.     if (empty($username)) { array_push($errors, "Username is required"); }
  22.     if (empty($email)) { array_push($errors, "Email is required"); }
  23.     if (empty($password_1)) { array_push($errors, "Password is required"); }
  24.  
  25.     if ($password_1 != $password_2) {
  26.         array_push($errors, "The two passwords do not match");
  27.     }
  28.  
  29.     // register user if there are no errors in the form
  30.     if (count($errors) == 0) {
  31.         $password = md5($password_1);//encrypt the password before saving in the database
  32.         $query = "INSERT INTO users (username, email, password,freeSpace)
  33.                   VALUES('$username', '$email', '$password',1024)";
  34.         mysqli_query($db, $query);
  35.  
  36.         $_SESSION['username'] = $username;
  37.         $_SESSION['success'] = "You are now logged in";
  38.         $_SESSION['userLevel'] = "u";
  39.         $_SESSION['freeSpace'] = "1024";
  40.  
  41.         //create a space for user in server
  42.         $dirBaseName = $_SESSION['username'];
  43.         $path = "uploads/".$dirBaseName;
  44.         if (!mkdir($path, 0777, true)) {
  45.             die('Failed to create folders...');
  46.         }
  47.  
  48.         header('location: index.php');
  49.    
  50.     }
  51.  
  52. }
  53.  
  54.  
  55. // login
  56.  
  57. if (isset($_POST['login_user'])) {
  58.     $username = mysqli_real_escape_string($db, $_POST['username']);
  59.     $password = mysqli_real_escape_string($db, $_POST['password']);
  60.  
  61.     if (empty($username)) {
  62.         array_push($errors, "Username is required");
  63.     }
  64.     if (empty($password)) {
  65.         array_push($errors, "Password is required");
  66.     }
  67.  
  68.     if (count($errors) == 0) {
  69.         $password = md5($password);
  70.         $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  71.         $results = mysqli_query($db, $query);
  72.  
  73.         if (mysqli_num_rows($results) == 1) {
  74.             $_SESSION['username'] = $username;
  75.             $_SESSION['success'] = "You are now logged in";
  76.             $followingdata = $results->fetch_array(MYSQLI_ASSOC);
  77.             $_SESSION['userLevel'] = $followingdata['userLevel'];
  78.             $_SESSION['freeSpace'] = $followingdata['freeSpace'];
  79.             header('location: index.php');
  80.         }else {
  81.             array_push($errors, "Wrong username/password combination");
  82.         }
  83.     }
  84. }
  85.  
  86.  
  87.  
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment