My OOP based login -- Help me start

Reply

Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #11
20 Days Ago
Here is my new class and login page I wrote at home. It have registration capabilities
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

index.php

 
0
  #12
20 Days Ago
  1. <?php
  2.  
  3. require('inc.class.php');
  4. //testing values
  5. $sms = new HtmlSms();
  6. $logger = new LoginRegister();
  7. if (!isset($_GET['do'])){
  8. $sms->login();
  9. }//end if
  10. else{
  11. if ($_GET['do']=='login'){
  12. $ret = $logger->Validate($_POST['username'], $_POST['password']);
  13. if($ret ==0){
  14. header("Location:members.php");
  15. die("No hacking here!");
  16. }//end if
  17. else{
  18. $sms->error($ret);
  19. }
  20.  
  21. }//end if
  22. else if($_GET['do']=='register'){
  23. if (isset($_POST['submit'])){
  24. $err = $logger->register($_POST);
  25. if($err !=0){
  26. $sms->error($err);
  27. }//end if
  28. else{
  29. $err = $logger->validateform();
  30. if($err !=0){
  31. $sms->error($err);
  32. }//end if
  33. else{
  34. echo "Successful registered!";
  35. }//end else
  36. }//end else
  37. }//end if
  38. else{
  39. $sms->register();
  40. }
  41. }//end elif
  42.  
  43. else if($_GET['do']=='logout'){
  44. $logger->logout();
  45. header("Location:index.php");
  46. }//end elif
  47. }
  48. ?>
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

inc.class.php

 
0
  #13
20 Days Ago
  1. <?php
  2. //start session
  3. session_start();
  4. class Database{
  5. //for db
  6. var $host;
  7. var $dbusername;
  8. var $dbpasswd;
  9. var $db;
  10. //for pagination
  11. //max page numbers
  12. var $page_count;
  13. //max contents per page
  14. var $per_page;
  15.  
  16. function __construct($usr, $pass, $host, $db){
  17. $this->host = $host;
  18. $this->dbusername = $usr;
  19. $this->dbpasswd = $pass;
  20. $this->db = $db;
  21.  
  22. }
  23.  
  24. // connect db
  25. function connect(){
  26. $conn = mysql_connect($this->host, $this->dbusername, $this->dbpasswd) or die (mysql_error());
  27. mysql_select_db($this->db, $conn) or die(mysql_error());
  28. return $conn;
  29. }
  30. }//end class db
  31.  
  32. class LoginRegister extends Database{
  33. //credentials - fname, lname, email, username, password
  34. var $firstname;
  35. var $lastname;
  36. var $email;
  37. var $username;
  38. var $password;
  39. var $date;
  40.  
  41. function __construct(){
  42. parent::__construct('root', 'jesus', 'localhost', 'testlogin');
  43. }
  44.  
  45. function Authenticate($user, $passwd){
  46. $username = mysql_real_escape_string($user);
  47. $password = $this->encrypt(mysql_real_escape_string($passwd));
  48. //connect
  49. $conn = $this->connect();
  50. $query = "SELECT * FROM users WHERE username = '$username'";
  51. $res = mysql_query($query);
  52. if (mysql_num_rows($res)==1){
  53. //user exists
  54. $row = mysql_fetch_assoc($res);
  55. $dbusername = $row['username'];
  56. $dbpassword = $row['password'];
  57. $fname = $row['firstname'];
  58. //password check
  59. if ($dbusername == $username && $dbpassword == $password){
  60. //correct passwd
  61. //call function to set something useful for sessions
  62. $_SESSION['username'] = $dbusername;
  63. $_SESSION['fname'] = $fname;
  64. $_SESSION['who'] = 1;
  65. }//end if
  66. else{
  67. //incorrect passwd
  68. return 2;
  69. }//end else
  70.  
  71. }//end if
  72. else{
  73. //user doesn't exists
  74. return 1;
  75. }//end else
  76.  
  77. }
  78.  
  79. function validate($usr, $pass){
  80. $usr = trim($usr);
  81. $pass = trim($pass);
  82. if (empty($usr)&&empty($pass)){
  83. return 5;
  84. }//end if
  85. else if (empty($usr)){
  86. return 3;
  87. }//end elif
  88. else if (empty($pass)){
  89. return 4;
  90. }//end elif
  91. else{
  92. //both field submitted -- Authenticate
  93. //connect to server
  94. $this->connect();
  95. $res = $this->Authenticate($usr, $pass);
  96. return $res;
  97. }//end else
  98. }
  99.  
  100. function logout(){
  101. session_destroy();
  102. }
  103.  
  104. function validateemail($email){
  105. $sanitized = filter_var($email, FILTER_SANITIZE_EMAIL);
  106. if (filter_var($sanitized, FILTER_VALIDATE_EMAIL)) {
  107. $this->email = $sanitized;
  108. return 0;
  109. }//end if
  110. else{
  111. return 10;
  112. }//end else
  113. }
  114.  
  115. function validateusername($usr){
  116. $this->connect();
  117. $usr = mysql_real_escape_string($usr);
  118. $res = mysql_query("SELECT username from users WHERE username = '$usr'") or die(mysql_error());
  119. if(mysql_num_rows($res)>0){
  120. return 11;
  121. }//end if
  122. else{
  123. return 0;
  124. }//end else
  125.  
  126.  
  127. }
  128.  
  129. //call this only after register/validateform is successful
  130. function registerme(){
  131. $conn = $this->connect();
  132. $query = "INSERT INTO users(firstname, lastname, email, username, password, date) VALUES('$this->firstname', '$this->lastname', '$this->email', '$this->username', '$this->password', ' $this->date') ";
  133. mysql_query( $query) or die(mysql_error());
  134.  
  135. }
  136.  
  137. function encrypt($pass){
  138. //do all encrypt stuffs here
  139. return sha1($pass);
  140. }
  141.  
  142. //call this only after register is successful
  143. function validateform(){
  144. $usr = $this->username;
  145. $fname = $this->firstname;
  146. $lname = $this->lastname;
  147. $email = $this->email;
  148. $uname = $this->username;
  149. $passwd = $this->password;
  150. if (strlen($fname )>25 or strlen($lname )>25 or strlen($uname )>25 or strlen($passwd )>25){
  151. return 8;
  152. }//end if
  153. else{
  154. if(strlen($passwd)<6){
  155. return 9;
  156. }//end if
  157. else if (($this->validateusername($usr))>0){
  158. return 11;
  159. }//end elif
  160. else if(($this->validateemail($email))==0){
  161. $this->password = $this->encrypt($passwd);
  162. $this->registerme();
  163. return 0;
  164. }//end if
  165. else{
  166. return 10;
  167. }//end else
  168. }//end else
  169.  
  170.  
  171. }
  172.  
  173. function register($arr){
  174. //array of fname, lname, email, username, password
  175. //form data
  176. $this->firstname = strip_tags($arr['fname']);
  177. $this->lastname = strip_tags($arr['lname']);
  178. $this->email = strip_tags($arr['email']);
  179. $this->username = strtolower(strip_tags($arr['username']));
  180. $password = strip_tags($arr['password']);
  181. $rpassword = strip_tags($arr['rpassword']);
  182. $this->date = Date("Y-m-d H:i:s");
  183.  
  184. if( $this->firstname && $this->lastname && $this->email && $this->username && $password && $rpassword){
  185. //echo " $date/$firstname $lastname /$email/$username/$password /$rpassword ";
  186. $this->$password = $password ;
  187. if( $this->encrypt($password) == $this->encrypt($rpassword)){
  188. $this->password = $password;
  189. return 0;
  190. }//end if
  191. else{
  192. return 7;
  193. }
  194.  
  195. }//end if
  196. else{
  197. //blank field(s)
  198. return 6;
  199. }
  200. }
  201.  
  202. }//end class
  203.  
  204.  
  205. class HtmlSms{
  206. /*
  207. error codes
  208. * 0 = successful
  209. * 1 = username wrong
  210. * 2 = password wrong
  211. * 3 = unsubmitted username
  212. * 4 = unsubmitted password
  213. * 5 = empty username and passwd
  214. //registration codes
  215. * 6 empty field
  216. * 7 password don't match
  217. * 8 one field is more than 25 characters
  218. * 9 password field is less than 6
  219. * 10 invalid email
  220. * 11 username already exists // forgot password?
  221.  
  222.  
  223. */
  224.  
  225. function login(){
  226. $html = <<<HTML
  227. <form method='POST' action = 'index.php?do=login'>
  228. <p>Username: <input name='username' type = 'text'></p>
  229. <p>Password: <input name='password' type = 'password'></p>
  230. <input value = 'login' type = 'submit'>
  231. </form>
  232. <a href='index.php?do=register'>Register</a>
  233. HTML;
  234. echo $html;
  235.  
  236. }
  237.  
  238.  
  239. function register(){
  240. echo "<h1>Register</h1>";
  241. $html = <<<HTML
  242. <form method='POST' action = 'index.php?do=register'>
  243. <html>
  244. <table cellpadding='5px'>
  245. <tr>
  246. <td> Your First Name</td>
  247. <td><input name='fname' type = 'text'> </td>
  248. </tr>
  249.  
  250. <tr>
  251. <td> Your Last Name</td>
  252. <td><input name='lname' type = 'text'> </td>
  253. </tr>
  254.  
  255. <tr>
  256. <td> Email address</td>
  257. <td><input name='email' type = 'text'> </td>
  258. </tr>
  259.  
  260. <tr>
  261. <td> Choose a User Name</td>
  262. <td><input name='username' type = 'text'> </td>
  263. </tr>
  264.  
  265. <tr>
  266. <td>Choose a password</td>
  267. <td><input name='password' type = 'password'> </td>
  268. </tr>
  269.  
  270. <tr>
  271. <td>Repeat your password</td>
  272. <td><input name='rpassword' type = 'password'> </td>
  273. </tr>
  274. </table>
  275. </html>
  276. <p> <input name = submit value = 'register' type = 'submit'></p>
  277. </form>
  278. HTML;
  279. echo $html;
  280. }
  281.  
  282. function error($ecode){
  283. switch($ecode){
  284. case 1:
  285. echo "<p style = 'color:red; font-weight:bold;'>Incorrect Username</p>";
  286. $this->login();
  287. break;
  288. case 2:
  289. echo "<p style = 'color:red; font-weight:bold;'>Incorrect Password</p>";
  290. $this->login();
  291. break;
  292. case 3:
  293. echo "<p style = 'color:red; font-weight:bold;'>Blank usernames are not allowed!</p>";
  294. $this->login();
  295. break;
  296. case 4:
  297. echo "<p style = 'color:red; font-weight:bold;'>Blank passwords are not allowed!</p>";
  298. $this->login();
  299. break;
  300. case 5:
  301. echo "<p style = 'color:red; font-weight:bold;'>Blank usernames and passwords are not allowed!</p>";
  302. $this->login();
  303. break;
  304. case 6:
  305. echo "<p style = 'color:red; font-weight:bold;'>Blank fields are not allowed!. Please fill <b>all</b> fields</p>";
  306. $this->register();
  307. break;
  308. case 7:
  309. echo "<p style = 'color:red; font-weight:bold;'>Passwords doesn't match! </p>";
  310. $this->register();
  311. break;
  312. case 8:
  313. echo "<p style = 'color:red; font-weight:bold;'>No Field can exceed 25 Characters!</p>";
  314. $this->register();
  315. break;
  316. case 9:
  317. echo "<p style = 'color:red; font-weight:bold;'>Your password must be between 6 and 25 characters</p>";
  318. $this->register();
  319. break;
  320. case 10:
  321. echo "<p style = 'color:red; font-weight:bold;'>Your Email is invalid</p>";
  322. $this->register();
  323. break;
  324. case 11:
  325. echo "<p style = 'color:red; font-weight:bold;'>The username is already taken, please choose another one!</p>";
  326. $this->register();
  327. break;
  328.  
  329. }//end switch
  330. }
  331.  
  332. }//end class htmlsms
  333.  
  334.  
  335. ?>
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

members.php

 
0
  #14
20 Days Ago
  1. <?php
  2. require('inc.class.php');
  3. if(isset($_SESSION['who'])&& ($_SESSION['who']=1)){
  4. $name = $_SESSION['fname'];
  5. echo "Welcome $name, this page you are viewing is for members, of which you are one! <br /><a href='index.php?do=logout'>logout</a>";
  6. }//end if
  7.  
  8. else{
  9. $sms = new HtmlSms();
  10. echo "<span style = 'color:red;'>Access denied! You aren't logged in<br />So please login or <a href='register.php'>Register</a></span>" ;
  11. $sms->login();
  12. }//end else
  13.  
  14.  
  15. ?>
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #15
20 Days Ago
my databse name was testlogin and table users. Here is a script to install table fields necessary

  1. <?php
  2. mysql_connect('localhost', 'root', 'jesus');
  3. mysql_select_db('testlogin');
  4. mysql_query("DROP TABLE IF EXISTS users ") or die(mysql_error());
  5. $query = " CREATE TABLE `testlogin`.`users` (`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `firstname` VARCHAR(25) NOT NULL, `lastname` VARCHAR(25) NOT NULL, `email` VARCHAR(25) NOT NULL, `username` VARCHAR(25) NOT NULL, `password` VARCHAR(100) NOT NULL, `date`DATETIME NOT NULL, UNIQUE (`email`, `username`)) ENGINE = MyISAM";
  6. mysql_query($query) or die(mysql_error());
  7. echo 'successful created users table';
  8.  
  9. ?>
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #16
20 Days Ago
my databse name was testlogin and table users. Here is a script to install table fields necessary

  1. <?php
  2. mysql_connect('localhost', 'root', 'jesus');
  3. mysql_select_db('testlogin');
  4. mysql_query("DROP TABLE IF EXISTS users ") or die(mysql_error());
  5. $query = " CREATE TABLE `testlogin`.`users` (`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `firstname` VARCHAR(25) NOT NULL, `lastname` VARCHAR(25) NOT NULL, `email` VARCHAR(25) NOT NULL, `username` VARCHAR(25) NOT NULL, `password` VARCHAR(100) NOT NULL, `date`DATETIME NOT NULL, UNIQUE (`email`, `username`)) ENGINE = MyISAM";
  6. mysql_query($query) or die(mysql_error());
  7. echo 'successful created users table';
  8.  
  9. ?>
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #17
20 Days Ago
I still need you experts to poke around above code and correct me anywhere due to security/coding habits/OOP stuffs et al and all you can suggest for the above code. I'm novice on PHP security issues and I

thanks
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC