Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database
- {
- private $user;
- private $pass;
- private $name;
- private $host;
- function __construct()
- {
- /* could also load a config file here to hold db data */
- $this->user = "username";
- $this->pass = "password";
- $this->name = "dbname";
- $this->host = "host:port";
- }
- protected function query($querystring,$returntype=1)
- {
- if($returntype == 1) /* return bool A.K.A. default return type */
- {
- if($handle = mysql_connect($this->host,$this->user,$this->pass))
- {
- if($tempo = mysql_query($querystring,$handle))
- {
- mysql_close($handle);
- $tempo = "";
- return true;
- } else {
- mysql_close($handle);
- $tempo = "";
- return false;
- }
- }
- } else if($returntype = 2) { /* return accociative array */
- if($handle = mysql_connect($this->host,$this->user,$this->pass)
- {
- if($tempo = mysql_query($querystring,$handle))
- {
- if(mysql_num_rows($tempo) == 0)
- {
- mysql_close($handle);
- return false;
- } else {
- // build custom accoc array for return
- }
- }
- }
- } else {
- /*just here for language spec*/
- }
- }
- protected function encode($data)
- {
- if($handle = mysql_connect($this->host,$this->user,$this->pass)
- {
- $data = mysql_real_escape_string($data,$handle);
- mysql_close($handle);
- return $data;
- } else {
- return false;
- }
- }
- protected function decode($data)
- {
- return stripslashes($data);
- }
- function __destruct()
- {
- /* some garbage cleanup could go here if ever needed for db */
- }
- }
- /* you can put Database class in another file to make it mor organized and use require_once or whatever here */
- class Login extends Database
- {
- private function rnger()
- {
- for($i=0;$i<33;$i++)
- {
- $sid .= rng(0,9);
- }
- return $sid;
- }
- public function stat()
- {
- if(isset($_COOKIE["user"]))
- {
- // check id $_COOKIE["user"] against member db session
- $sid = $this->db_filter($_COOKIE["user"]);
- if($this->query("SELECT * WHERE sid = $sid",1)) return true;
- }
- return false;
- }
- public function logout()
- {
- /* destroy cookie and db entry */
- if(isset($_COOKIE["user"]))
- {
- setcookie("user","",time()-3600);
- $this->db_query("UPDATE `member` SET `session` = '' WHERE `session` = sidhere;");
- }
- }
- /* checks login+makes cookie and db entry for session id */
- public function login($user,$pass,$path,$domain,$duration)
- {
- $user = $this->filter($user);
- $pass = $this->filter($pass);
- if($this->query("SELECT * FROM `member` WHERE `username` = \'$user\' AND `password` = \'".md5($pass)."\';",1))
- {
- $sid = $this->rnger();
- if(!setcookie("user",$sid,$duration,$path,$domain)) return false;
- if(!$this->query("UPDATE `member` SET `session` = \'$sid\' WHERE `username` = \'".stripslashes($user)."\'",1))
- {
- setcookie("user","",time()-3600);
- return false;
- }
- return true;
- }
- return false;
- }
- }
- ?>
- <?php
- /* Example check: */
- auth = new Login();
- if(auth->stat())
- {
- /* user is logged in */
- }
- ?>
- <?php
- auth = new Login();
- if($_POST["username"] && $_POST["password"] && $_POST["submit"])
- {
- /* expire in 1 hour */
- if(auth->login($_POST["username"],$_POST["password"],"/myname/www/","domain.com",time()+3600))
- {
- /* auth passed */
- } else {
- /* auth failed */
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement