dasun101

Untitled

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