Advertisement
Guest User

xd

a guest
Jan 14th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4.     if(isset($_GET['logout'])){
  5.         unset($_SESSION['logged']);
  6.         redirect();
  7.        
  8.        
  9.        
  10.        
  11.     }else if(isset($_SESSION['logged'])){
  12.         echo "<a href='?logout'>Logout</a>";
  13.        
  14.        
  15.        
  16.        
  17.        
  18.     }else if(isset($_POST['username']) && isset($_POST['password'])){
  19.         //connect database
  20.         db::connect() and die("Can't connect database!");
  21.        
  22.         db::query("SELECT * FROM db.users");
  23.        
  24.         $connected = false;
  25.        
  26.         if(db::$hasResults){
  27.             while($row = db::$result->fetch_assoc()){
  28.                 if($row['username'] == $_POST['username'] && $row['password'] == $_POST['password']){
  29.                     $connected = true;
  30.                     break;
  31.                 }
  32.             }
  33.         }
  34.        
  35.         if($connected){
  36.             //if logged create the session and redirect
  37.             $_SESSION['logged'] = true;
  38.         }
  39.        
  40.         //close the connection
  41.         db::close();
  42.        
  43.         //redirect to lose the post refresh
  44.         redirect();
  45.        
  46.        
  47.        
  48.        
  49.        
  50.        
  51.     }else{
  52.     //show login
  53.         echo "
  54.             <form action='' method='POST'>
  55.                 <input type='text' name='username' placeholder='Username...'><br>
  56.                 <input type='password' name='password' placeholder='Password...'><br>
  57.                 <input type='submit' value='Submit'>
  58.             </form>
  59.         ";
  60.     }
  61.    
  62.    
  63.    
  64.     //redirect to self if url its empty
  65.     function redirect($url="top.location.href"){
  66.         echo "<script> top.location.href = ".$url.".split('?')[0]; </script>";
  67.         exit;
  68.     }
  69.    
  70.    
  71.    
  72.    
  73.    
  74.    
  75.    
  76.    
  77.    
  78.    
  79.    
  80.    
  81.    
  82.    
  83.    
  84.    
  85.    
  86.    
  87.     class db{
  88.         static $o;
  89.    
  90.         static $hostname = "localhost";
  91.         static $username = "root";
  92.         static $password = "";
  93.         static $error = false;
  94.         static $hasResults = false;
  95.        
  96.         static $result;
  97.        
  98.         static function connect($hostname="localhost",$username="root",$password=""){
  99.             self::$hostname = $hostname;
  100.             self::$username = $username;
  101.             self::$password = $password;
  102.             self::$o = new mysqli($hostname, $username, $password);
  103.             self::$error = self::$o->connect_error;
  104.             return self::$error;
  105.         }
  106.         static function close(){
  107.             self::$o->close();
  108.         }
  109.        
  110.         static function query($sql){
  111.             self::$result = self::$o->query($sql);
  112.             if(self::$result->num_rows > 0){
  113.                 self::$hasResults = true;
  114.             }else{
  115.                 self::$hasResults = true;
  116.             }
  117.            
  118.             return self::$hasResults;
  119.         }
  120.        
  121.         static function error(){
  122.             return self::$error;
  123.         }
  124.        
  125.     }
  126.  
  127. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement