Advertisement
MrElliwood

Untitled

Oct 28th, 2017
86
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. class Config{
  3.     public static $dbhost = "localhost";
  4.     public static $dbuser = "root";
  5.     public static $dbpass = "";
  6.     public static $dbdata = "Skyhigh";
  7. }
  8. global $con;
  9. $con = new mysqli(Config::$dbhost, Config::$dbuser, Config::$dbpass, Config::$dbdata);
  10. if($con->connect_errno)
  11. {
  12.     printf("Connect failed: %s\n", $con->connect_error);
  13.     die("ERROR");
  14. }
  15.  
  16. $action = $_GET['action'];
  17.  
  18. class User{
  19.     private $con;
  20.     private $username;
  21.     private $password;
  22.     private $newpassword;
  23.     public function __construct($user, $pass, $newpass) {
  24.        $username = $user;
  25.        $password = $pass;
  26.        $newpassword = $newpass;
  27.    }
  28.    public function exists(){
  29.        $result = $con->query("SELECT ID FROM users WHERE username = " . $username);
  30.        if ($result->num_rows == 0 )
  31.        {
  32.            return 1;
  33.        } else if($result->num_rows == 1)
  34.        {
  35.            return 0;
  36.        } else
  37.        {
  38.            return -1;
  39.        }
  40.    }
  41.    public function login()
  42.    {
  43.        $result = $con->query("SELECT passhash FROM users WHERE username = " . $username);
  44.        while($row = $result->fetch_object()){
  45.             $hash =$row->passhash;
  46.        }
  47.        if (password_verify($password, $hash))
  48.        {
  49.            return 1;
  50.        }else
  51.        {
  52.            return 0;
  53.        }
  54.    }
  55.    public function register()
  56.    {
  57.        if(($password == $newpassword))
  58.        {
  59.            $hash = password_hash($password, PASSWORD_DEFAULT);
  60.        }
  61.        if($query = $con->query("INSERT INTO users (username,passhash) VALUES ('$username','$hash')"))
  62.         {
  63.             echo "1";
  64.         }
  65.         else
  66.         {
  67.             echo "0";
  68.         }
  69.    }
  70. }
  71.  
  72. $name = $con->real_escape_string($_GET['username']);
  73. $pass1 = $con->real_escape_string($_GET['password']);
  74. $pass2 = $con->real_escape_string($_GET['newpassword']);
  75.  
  76.  
  77. $user = new User($username, $pass1, $pass2);
  78.  
  79. switch ($action)
  80. {
  81.     case "register":
  82.         if (!$user->exists())
  83.             echo $user->register();
  84.         break;
  85.     case "login":
  86.         if($user->exists())
  87.             echo $user->login();
  88.         break;
  89.     default:
  90.         echo "Please enter an action.";
  91.        
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement