Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------------------ //
- // https://pastebin.com/996xp0kj //
- // ------------------------------ //
- require '../php/session.php';
- require '../php/config.php';
- // LOGIN FORM
- if(isset($_POST['login'])){
- // INPUT CAPTURE
- $username = trim($_POST['login_username']);
- $userpass = $_POST['login_userpass'];
- // ERRORS ARRAY
- $errors = [];
- // Username Check
- if(!$username){
- $errors[] = "Empty Username";
- }elseif(!filter_var($username, FILTER_SANITIZE_STRING) || !preg_match("/^[\w]{4,16}$/", $username)){
- $errors[] = "Invalid Username";
- $username = "";
- }
- // Password Check
- if(!$userpass){
- $errors[] = "Empty Password";
- }
- // Verify Account Existence if both username AND verification exist
- if($username && $userpass){
- $sql = "SELECT user_id,user_name,user_pass,user_type FROM users WHERE user_name = ? AND user_verified = ?";
- $verify = 1;
- $stmt = prepareQuery($con, $sql, [$username, $verify]);
- $stmt -> store_result();
- $num = $stmt -> num_rows();
- // Check if account exists
- if($num === $verify){
- // Bind results to variables
- $stmt -> bind_result($uid, $uname, $uhash, $utype);
- while($stmt -> fetch()){
- $userid = $uid;
- $username = $uname;
- $userhash = $uhash;
- $usertype = $utype;
- }
- // Finally check the password
- if(password_verify($userpass, $userhash)){
- $stmt -> close();
- // LOGIN ACCOUNT
- $_SESSION['uID'] = $userid;
- $_SESSION['pID'] = $username;
- $_SESSION['TYPE'] = $usertype;
- $_SESSION['LOGIN'] = $date;
- $_SESSION['ERROR'] = "Successfully logged in!";
- // LOG ACTIVITY
- $sql = "UPDATE users SET user_status = ?, user_lastlogin = ? WHERE user_name = ?";
- $stmt = prepareQuery($con, $sql, [$verify, $date, $username]);
- redir("/cpanel.php");
- }
- }
- $errors[] = "Username or password does not match our records";
- $con -> close();
- }
- // CHECK FOR ERRORS
- if($errors){
- $_SESSION['ERROR'] = $errors;
- $mins = time() + 120;
- $path = "/login.php";
- setcookie("username", $username, $mins, $path);
- redir($path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement