Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | None | 0 0
  1. <?php
  2. class Database
  3. {
  4.     private $user;
  5.     private $pass;
  6.     private $name;
  7.     private $host;
  8.    
  9.     function __construct()
  10.     {
  11.         /* could also load a config file here to hold db data */
  12.         $this->user = "username";
  13.         $this->pass = "password";
  14.         $this->name = "dbname";
  15.         $this->host = "host:port";
  16.     }
  17.    
  18.     protected function query($querystring,$returntype=1)
  19.     {
  20.         if($returntype == 1) /* return bool A.K.A. default return type */
  21.         {
  22.             if($handle = mysql_connect($this->host,$this->user,$this->pass))
  23.             {
  24.                 if($tempo = mysql_query($querystring,$handle))
  25.                 {
  26.                     mysql_close($handle);
  27.                     $tempo = "";
  28.                     return true;
  29.                 } else {
  30.                     mysql_close($handle);
  31.                     $tempo = "";
  32.                     return false;
  33.                 }
  34.             }
  35.         } else if($returntype = 2) { /* return accociative array */
  36.             if($handle = mysql_connect($this->host,$this->user,$this->pass)
  37.             {
  38.                 if($tempo = mysql_query($querystring,$handle))
  39.                 {
  40.                     if(mysql_num_rows($tempo) == 0)
  41.                     {
  42.                         mysql_close($handle);
  43.                         return false;
  44.                     } else {
  45.                         // build custom accoc array for return
  46.                     }
  47.                 }
  48.             }
  49.         } else {
  50.             /*just here for language spec*/
  51.         }
  52.     }
  53.    
  54.     protected function encode($data)
  55.     {
  56.         if($handle = mysql_connect($this->host,$this->user,$this->pass)
  57.         {
  58.             $data = mysql_real_escape_string($data,$handle);
  59.             mysql_close($handle);
  60.             return $data;
  61.         } else {
  62.             return false;
  63.         }
  64.     }
  65.    
  66.     protected function decode($data)
  67.     {
  68.         return stripslashes($data);
  69.     }
  70.    
  71.     function __destruct()
  72.     {
  73.       /* some garbage cleanup could go here if ever needed for db */
  74.     }
  75. }
  76.  
  77. /* you can put Database class in another file to make it mor organized and use require_once or whatever here */
  78. class Login extends Database
  79. {  
  80.     private function rnger()
  81.     {
  82.         for($i=0;$i<33;$i++)
  83.         {
  84.             $sid .= rng(0,9);
  85.         }
  86.         return $sid;
  87.     }
  88.    
  89.     public function stat()
  90.     {
  91.         if(isset($_COOKIE["user"]))
  92.         {
  93.             // check id $_COOKIE["user"] against member db session
  94.             $sid = $this->db_filter($_COOKIE["user"]);
  95.             if($this->query("SELECT * WHERE sid = $sid",1)) return true;
  96.         }
  97.         return false;
  98.     }
  99.    
  100.     public function logout()
  101.     {
  102.         /* destroy cookie and db entry */
  103.         if(isset($_COOKIE["user"]))
  104.         {
  105.             setcookie("user","",time()-3600);
  106.             $this->db_query("UPDATE `member` SET `session` = '' WHERE `session` = sidhere;");
  107.         }
  108.     }
  109.    
  110.     /* checks login+makes cookie and db entry for session id */
  111.     public function login($user,$pass,$path,$domain,$duration)
  112.     {
  113.         $user = $this->filter($user);
  114.         $pass = $this->filter($pass);
  115.         if($this->query("SELECT * FROM `member` WHERE `username` = \'$user\' AND `password` = \'".md5($pass)."\';",1))
  116.         {
  117.             $sid = $this->rnger();
  118.             if(!setcookie("user",$sid,$duration,$path,$domain)) return false;
  119.             if(!$this->query("UPDATE `member` SET `session` = \'$sid\' WHERE `username` = \'".stripslashes($user)."\'",1))
  120.             {
  121.                 setcookie("user","",time()-3600);
  122.                 return false;
  123.             }
  124.             return true;
  125.         }
  126.         return false;
  127.     }
  128.        
  129. }
  130. ?>
  131.  
  132. <?php
  133. /* Example check: */
  134. auth = new Login();
  135. if(auth->stat())
  136. {
  137.     /* user is logged in */
  138. }
  139. ?>
  140.  
  141. <?php
  142. auth = new Login();
  143. if($_POST["username"] && $_POST["password"] && $_POST["submit"])
  144. {
  145.     /* expire in 1 hour */
  146.     if(auth->login($_POST["username"],$_POST["password"],"/myname/www/","domain.com",time()+3600))
  147.     {
  148.         /* auth passed */
  149.     } else {
  150.         /* auth failed */
  151.     }
  152. }
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement