{"id":1202,"date":"2012-03-30T05:47:26","date_gmt":"2012-03-30T05:47:26","guid":{"rendered":"http:\/\/www.vishmax.com\/en\/?p=1202"},"modified":"2012-05-19T10:37:27","modified_gmt":"2012-05-19T10:37:27","slug":"php-user-authorization-class-with-database","status":"publish","type":"post","link":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/","title":{"rendered":"PHP User Authorization Class with Database"},"content":{"rendered":"<p>Here is an working example for user authorization class with database.<\/p>\n<pre lang=\"php\">\r\n\r\n<?php\r\nclass db { \r\n     \r\n    var $db_type; \r\n    var $db_server; \r\n    var $db_name; \r\n    var $db_user; \r\n    var $db_pass; \r\n    var $db_persistent; \r\n    var $dbh; \r\n     \r\n    function db() { \r\n         \r\n     \r\n        $this->db_type = 1; \r\n        $this->db_server = 'localhost'; \r\n        $this->db_name = 'db'; \r\n        $this->db_user = 'user'; \r\n        $this->db_pass = 'pass'; \r\n        $this->db_persistent = 0; \r\n                $this->db_connect(); \r\n         \r\n    } \/\/end constructor \r\n     \r\n    function db_connect () { \r\n         \r\n        \/\/ mySQL \r\n        if($this->db_type == 1) { \r\n            if ($this->db_persistent) \r\n                $this->dbh = @mysql_pconnect($this->db_server, $this->db_user, $this->db_pass); \r\n            else \r\n                $this->dbh = @mysql_connect($this->db_server, $this->db_user, $this->db_pass); \r\n\r\n            if (!$this->dbh) { \r\n                printf(\"Error: Connection to MySQL server '%s' failed.<BR>\\n\", $this->db_server); \r\n                return; \r\n            } \r\n\r\n            if (!@mysql_select_db($this->db_name, $this->dbh)) { \r\n                printf(\"Error: Connection to MySQL database '%s' failed.<BR>\\n>%s: %s<BR>\\n\", $this->db_name, @mysql_errno($this->dbh), @mysql_error($this->dbh)); \r\n                return; \r\n            } \r\n        } \r\n        \/\/end mySQL \r\n    } \/\/end db_connect() \r\n     \r\n    function db_query ($query) { \r\n         \r\n        \/\/ mySQL \r\n        if($this->db_type == 1) { \r\n            $result = mysql_query($query, $this->dbh) \r\n                or die (\"Error: A problem was encountered while executing this query.\"); \r\n             \r\n            return $result; \r\n        } \r\n        \/\/end mySQL \r\n    } \/\/end db_query() \r\n     \r\n    function db_numrows ($result) { \r\n         \r\n        switch($this->db_type) { \r\n            case 1: \/\/mySQL \r\n                return mysql_num_rows($result); \r\n         \r\n        } \/\/end switch \r\n    } \/\/ end db_numrows() \r\n     \r\n    function db_fetch_array (&$result) { \r\n         \r\n        switch($this->db_type) { \r\n            case 1: \/\/mySQL \r\n                return mysql_fetch_array($result); \r\n        } \/\/end switch \r\n    } \/\/end db_fetch_array() \r\n     \r\n             \r\n} \/\/end class db \r\n\r\nclass authenticate { \r\n     \r\n    var $db; \r\n    var $salt; \r\n     \r\n    function authenticate() { \r\n         \r\n         \r\n        $this->db = new db; \r\n        $this->salt = 'a552avf1ss'; \r\n         \r\n         \r\n    } \/\/end constructor \r\n\r\n     \r\n    function login($uname, $pword) { \r\n         \r\n        $query = \"SELECT username FROM users WHERE username = '\" . $uname . \"' AND password = '\" . crypt($pword, $this->salt) . \"'\"; \r\n\r\n        $result = $this->db->db_query($query); \r\n        if($this->db->db_numrows($result) > 0) { \r\n            $secret = crypt($uname,$this->salt); \r\n            setcookie(\"mysite\", \"$uname:$secret\");             \r\n            return 1; \r\n        } else { \r\n            return 0; \r\n        } \r\n    } \/\/end login() \r\n     \r\n    function createUser($uname,$pword,$email) { \r\n        srand(make_seed()); \r\n        $randval = rand(); \r\n        $query = \"INSERT authorize(username,password,accesslevel,email,id) \r\nVALUES ('\" . $uname . \"','\" . crypt($pword,$this->salt) . \"',0,'\" . $email .\"','\" . $randval . \"')\"; \r\n        $result = $this->db->db_query($query); \r\n        $message = \"This message has been sent to you because you requested a login for mysite.com.\\n\\n\"; \r\n        $message .= \"Please use the following URL to verify your email address and be added to the userlist.\\n\\n\"; \r\n        $message .= \"http:\/\/mysite.com\/newuser.php?email=\" . $email . \"&id=\" . $randval . \"\\n\\n\"; \r\n        $message .= \"Thanks for visiting our site!\\n\"; \r\n        mail($email, \"mysite.com - account confirmation\", $message, \"From: register@mysite.com\"); \r\n         \r\n    } \r\n     \r\n    function checkUsername($uname) { \r\n        $query = \"SELECT * FROM users where username='\" . $uname .\"'\"; \r\n        $result = $this->db->db_query($query); \r\n        if($this->db->db_numrows($result) > 0) {             \r\n            return 0; \r\n        } else { \r\n            return 1; \r\n        } \r\n    } \r\n     \r\n    function validateUser($email,$id) { \r\n        $query = \"SELECT * FROM authorize WHERE email='\" . $email . \"' AND id='\" . $id .\"'\"; \r\n        $result = $this->db->db_query($query); \r\n        if($this->db->db_numrows($result) > 0) { \r\n            $row = $this->db->db_fetch_array($result); \r\n            $query = \"INSERT users(user_id,username,password,accesslevel,email) VALUES ('','\" . $row['username'] . \"','\" . $row['password'] . \"',1,'\" . $row['email'] .\"')\"; \r\n            $result = $this->db->db_query($query); \r\n            $query = \"SELECT user_id FROM users WHERE username='\" . $row['username'] .\"'\"; \r\n            $result = $this->db->db_query($query); \r\n            $row = $this->db->db_fetch_array($result); \r\n            $query = \"DELETE FROM authorize WHERE id='\" . $id .\"'\"; \r\n            $result = $this->db->db_query($query); \r\n            return 1; \r\n        } else { \r\n            return 0; \r\n        } \r\n    } \r\n     \r\n    function logout() { \r\n         \r\n        setcookie(\"mysite\"); \r\n    } \/\/end logout() \r\n     \r\n    function checkLogin() { \r\n        global $HTTP_COOKIE_VARS; \r\n\r\n        $array = explode(\":\", $HTTP_COOKIE_VARS['mysite']); \r\n        if(crypt($array[0], $this->salt) == $array[1]) { \r\n            return 1; \r\n        } else { \r\n            return 0; \r\n        } \r\n    } \/\/end checkLogin() \r\n     \r\n    function getName() { \r\n        global $HTTP_COOKIE_VARS; \r\n        $array = explode(\":\", $HTTP_COOKIE_VARS['mysite']); \r\n        return $array[0]; \r\n    } \r\n     \r\n    function getLevel() { \r\n        $logged = $this->checkLogin(); \r\n        if($logged) { \r\n            $username = $this->getName(); \r\n            $query = \"SELECT accesslevel FROM users WHERE username='\" . $username . \"'\"; \r\n            $result = $this->db->db_query($query); \r\n            $row = $this->db->db_fetch_array($result); \r\n            return $row['accesslevel']; \r\n        } else { \r\n            return 0; \r\n        } \r\n    } \r\n     \r\n    function getID() { \r\n        $logged = $this->checkLogin(); \r\n        if($logged) { \r\n            $username = $this->getName(); \r\n            $query = \"SELECT user_id FROM users WHERE username='\" . $username . \"'\"; \r\n            $result = $this->db->db_query($query); \r\n            $row = $this->db->db_fetch_array($result); \r\n            return $row['user_id']; \r\n        } else { \r\n            return 0; \r\n        } \r\n    } \r\n     \r\n         \r\n} \/\/end class authenticate \r\n?> \r\n\r\n####table structures \r\nCREATE TABLE authorize ( \r\n  username varchar(15) NOT NULL default '', \r\n  password varchar(20) NOT NULL default '', \r\n  accesslevel tinyint(4) NOT NULL default '0', \r\n  email varchar(30) NOT NULL default '', \r\n  id varchar(30) NOT NULL default '', \r\n  PRIMARY KEY  (username) \r\n) TYPE=MyISAM; \r\nCREATE TABLE users ( \r\n  user_id int(10) unsigned NOT NULL auto_increment, \r\n  username varchar(15) NOT NULL default '', \r\n  password varchar(20) NOT NULL default '', \r\n  accesslevel tinyint(4) NOT NULL default '0', \r\n  email varchar(30) NOT NULL default '', \r\n  PRIMARY KEY  (username), \r\n  KEY user_id (user_id) \r\n) TYPE=MyISAM;\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is an working example for user authorization class with database.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[44],"tags":[72,48],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.2.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP User Authorization Class with Database - Vishmax.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP User Authorization Class with Database - Vishmax.com\" \/>\n<meta property=\"og:description\" content=\"Here is an working example for user authorization class with database.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/\" \/>\n<meta property=\"og:site_name\" content=\"Vishmax.com\" \/>\n<meta property=\"article:published_time\" content=\"2012-03-30T05:47:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-05-19T10:37:27+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93\"},\"headline\":\"PHP User Authorization Class with Database\",\"datePublished\":\"2012-03-30T05:47:26+00:00\",\"dateModified\":\"2012-05-19T10:37:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/\"},\"wordCount\":17,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\"},\"keywords\":[\"Code Library\",\"PHP\"],\"articleSection\":[\"Labs\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/\",\"url\":\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/\",\"name\":\"PHP User Authorization Class with Database - Vishmax.com\",\"isPartOf\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#website\"},\"datePublished\":\"2012-03-30T05:47:26+00:00\",\"dateModified\":\"2012-05-19T10:37:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.vishmax.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP User Authorization Class with Database\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#website\",\"url\":\"http:\/\/www.vishmax.com\/en\/\",\"name\":\"Vishmax.com\",\"description\":\"Software Company in Calicut for Web Design and eCommerce\",\"publisher\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.vishmax.com\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\",\"name\":\"Vishmax.com\",\"url\":\"http:\/\/www.vishmax.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png\",\"contentUrl\":\"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png\",\"width\":150,\"height\":50,\"caption\":\"Vishmax.com\"},\"image\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/www.vishmax.com\/en\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP User Authorization Class with Database - Vishmax.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/","og_locale":"en_US","og_type":"article","og_title":"PHP User Authorization Class with Database - Vishmax.com","og_description":"Here is an working example for user authorization class with database.","og_url":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/","og_site_name":"Vishmax.com","article_published_time":"2012-03-30T05:47:26+00:00","article_modified_time":"2012-05-19T10:37:27+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/#article","isPartOf":{"@id":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/"},"author":{"name":"admin","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93"},"headline":"PHP User Authorization Class with Database","datePublished":"2012-03-30T05:47:26+00:00","dateModified":"2012-05-19T10:37:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/"},"wordCount":17,"commentCount":0,"publisher":{"@id":"http:\/\/www.vishmax.com\/en\/#organization"},"keywords":["Code Library","PHP"],"articleSection":["Labs"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/","url":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/","name":"PHP User Authorization Class with Database - Vishmax.com","isPartOf":{"@id":"http:\/\/www.vishmax.com\/en\/#website"},"datePublished":"2012-03-30T05:47:26+00:00","dateModified":"2012-05-19T10:37:27+00:00","breadcrumb":{"@id":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vishmax.com\/en\/labs\/php-user-authorization-class-with-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.vishmax.com\/en\/"},{"@type":"ListItem","position":2,"name":"PHP User Authorization Class with Database"}]},{"@type":"WebSite","@id":"http:\/\/www.vishmax.com\/en\/#website","url":"http:\/\/www.vishmax.com\/en\/","name":"Vishmax.com","description":"Software Company in Calicut for Web Design and eCommerce","publisher":{"@id":"http:\/\/www.vishmax.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.vishmax.com\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/www.vishmax.com\/en\/#organization","name":"Vishmax.com","url":"http:\/\/www.vishmax.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png","contentUrl":"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png","width":150,"height":50,"caption":"Vishmax.com"},"image":{"@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g","caption":"admin"},"url":"https:\/\/www.vishmax.com\/en\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts\/1202"}],"collection":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/comments?post=1202"}],"version-history":[{"count":0,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts\/1202\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/media?parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/categories?post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/tags?post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}