| | |
My OOP based login -- Help me start
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
php Syntax (Toggle Plain Text)
<?php require('inc.class.php'); //testing values $sms = new HtmlSms(); $logger = new LoginRegister(); if (!isset($_GET['do'])){ $sms->login(); }//end if else{ if ($_GET['do']=='login'){ $ret = $logger->Validate($_POST['username'], $_POST['password']); if($ret ==0){ header("Location:members.php"); die("No hacking here!"); }//end if else{ $sms->error($ret); } }//end if else if($_GET['do']=='register'){ if (isset($_POST['submit'])){ $err = $logger->register($_POST); if($err !=0){ $sms->error($err); }//end if else{ $err = $logger->validateform(); if($err !=0){ $sms->error($err); }//end if else{ echo "Successful registered!"; }//end else }//end else }//end if else{ $sms->register(); } }//end elif else if($_GET['do']=='logout'){ $logger->logout(); header("Location:index.php"); }//end elif } ?>
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
php Syntax (Toggle Plain Text)
<?php //start session session_start(); class Database{ //for db var $host; var $dbusername; var $dbpasswd; var $db; //for pagination //max page numbers var $page_count; //max contents per page var $per_page; function __construct($usr, $pass, $host, $db){ $this->host = $host; $this->dbusername = $usr; $this->dbpasswd = $pass; $this->db = $db; } // connect db function connect(){ $conn = mysql_connect($this->host, $this->dbusername, $this->dbpasswd) or die (mysql_error()); mysql_select_db($this->db, $conn) or die(mysql_error()); return $conn; } }//end class db class LoginRegister extends Database{ //credentials - fname, lname, email, username, password var $firstname; var $lastname; var $email; var $username; var $password; var $date; function __construct(){ parent::__construct('root', 'jesus', 'localhost', 'testlogin'); } function Authenticate($user, $passwd){ $username = mysql_real_escape_string($user); $password = $this->encrypt(mysql_real_escape_string($passwd)); //connect $conn = $this->connect(); $query = "SELECT * FROM users WHERE username = '$username'"; $res = mysql_query($query); if (mysql_num_rows($res)==1){ //user exists $row = mysql_fetch_assoc($res); $dbusername = $row['username']; $dbpassword = $row['password']; $fname = $row['firstname']; //password check if ($dbusername == $username && $dbpassword == $password){ //correct passwd //call function to set something useful for sessions $_SESSION['username'] = $dbusername; $_SESSION['fname'] = $fname; $_SESSION['who'] = 1; }//end if else{ //incorrect passwd return 2; }//end else }//end if else{ //user doesn't exists return 1; }//end else } function validate($usr, $pass){ $usr = trim($usr); $pass = trim($pass); if (empty($usr)&&empty($pass)){ return 5; }//end if else if (empty($usr)){ return 3; }//end elif else if (empty($pass)){ return 4; }//end elif else{ //both field submitted -- Authenticate //connect to server $this->connect(); $res = $this->Authenticate($usr, $pass); return $res; }//end else } function logout(){ session_destroy(); } function validateemail($email){ $sanitized = filter_var($email, FILTER_SANITIZE_EMAIL); if (filter_var($sanitized, FILTER_VALIDATE_EMAIL)) { $this->email = $sanitized; return 0; }//end if else{ return 10; }//end else } function validateusername($usr){ $this->connect(); $usr = mysql_real_escape_string($usr); $res = mysql_query("SELECT username from users WHERE username = '$usr'") or die(mysql_error()); if(mysql_num_rows($res)>0){ return 11; }//end if else{ return 0; }//end else } //call this only after register/validateform is successful function registerme(){ $conn = $this->connect(); $query = "INSERT INTO users(firstname, lastname, email, username, password, date) VALUES('$this->firstname', '$this->lastname', '$this->email', '$this->username', '$this->password', ' $this->date') "; mysql_query( $query) or die(mysql_error()); } function encrypt($pass){ //do all encrypt stuffs here return sha1($pass); } //call this only after register is successful function validateform(){ $usr = $this->username; $fname = $this->firstname; $lname = $this->lastname; $email = $this->email; $uname = $this->username; $passwd = $this->password; if (strlen($fname )>25 or strlen($lname )>25 or strlen($uname )>25 or strlen($passwd )>25){ return 8; }//end if else{ if(strlen($passwd)<6){ return 9; }//end if else if (($this->validateusername($usr))>0){ return 11; }//end elif else if(($this->validateemail($email))==0){ $this->password = $this->encrypt($passwd); $this->registerme(); return 0; }//end if else{ return 10; }//end else }//end else } function register($arr){ //array of fname, lname, email, username, password //form data $this->firstname = strip_tags($arr['fname']); $this->lastname = strip_tags($arr['lname']); $this->email = strip_tags($arr['email']); $this->username = strtolower(strip_tags($arr['username'])); $password = strip_tags($arr['password']); $rpassword = strip_tags($arr['rpassword']); $this->date = Date("Y-m-d H:i:s"); if( $this->firstname && $this->lastname && $this->email && $this->username && $password && $rpassword){ //echo " $date/$firstname $lastname /$email/$username/$password /$rpassword "; $this->$password = $password ; if( $this->encrypt($password) == $this->encrypt($rpassword)){ $this->password = $password; return 0; }//end if else{ return 7; } }//end if else{ //blank field(s) return 6; } } }//end class class HtmlSms{ /* error codes * 0 = successful * 1 = username wrong * 2 = password wrong * 3 = unsubmitted username * 4 = unsubmitted password * 5 = empty username and passwd //registration codes * 6 empty field * 7 password don't match * 8 one field is more than 25 characters * 9 password field is less than 6 * 10 invalid email * 11 username already exists // forgot password? */ function login(){ $html = <<<HTML <form method='POST' action = 'index.php?do=login'> <p>Username: <input name='username' type = 'text'></p> <p>Password: <input name='password' type = 'password'></p> <input value = 'login' type = 'submit'> </form> <a href='index.php?do=register'>Register</a> HTML; echo $html; } function register(){ echo "<h1>Register</h1>"; $html = <<<HTML <form method='POST' action = 'index.php?do=register'> <html> <table cellpadding='5px'> <tr> <td> Your First Name</td> <td><input name='fname' type = 'text'> </td> </tr> <tr> <td> Your Last Name</td> <td><input name='lname' type = 'text'> </td> </tr> <tr> <td> Email address</td> <td><input name='email' type = 'text'> </td> </tr> <tr> <td> Choose a User Name</td> <td><input name='username' type = 'text'> </td> </tr> <tr> <td>Choose a password</td> <td><input name='password' type = 'password'> </td> </tr> <tr> <td>Repeat your password</td> <td><input name='rpassword' type = 'password'> </td> </tr> </table> </html> <p> <input name = submit value = 'register' type = 'submit'></p> </form> HTML; echo $html; } function error($ecode){ switch($ecode){ case 1: echo "<p style = 'color:red; font-weight:bold;'>Incorrect Username</p>"; $this->login(); break; case 2: echo "<p style = 'color:red; font-weight:bold;'>Incorrect Password</p>"; $this->login(); break; case 3: echo "<p style = 'color:red; font-weight:bold;'>Blank usernames are not allowed!</p>"; $this->login(); break; case 4: echo "<p style = 'color:red; font-weight:bold;'>Blank passwords are not allowed!</p>"; $this->login(); break; case 5: echo "<p style = 'color:red; font-weight:bold;'>Blank usernames and passwords are not allowed!</p>"; $this->login(); break; case 6: echo "<p style = 'color:red; font-weight:bold;'>Blank fields are not allowed!. Please fill <b>all</b> fields</p>"; $this->register(); break; case 7: echo "<p style = 'color:red; font-weight:bold;'>Passwords doesn't match! </p>"; $this->register(); break; case 8: echo "<p style = 'color:red; font-weight:bold;'>No Field can exceed 25 Characters!</p>"; $this->register(); break; case 9: echo "<p style = 'color:red; font-weight:bold;'>Your password must be between 6 and 25 characters</p>"; $this->register(); break; case 10: echo "<p style = 'color:red; font-weight:bold;'>Your Email is invalid</p>"; $this->register(); break; case 11: echo "<p style = 'color:red; font-weight:bold;'>The username is already taken, please choose another one!</p>"; $this->register(); break; }//end switch } }//end class htmlsms ?>
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
php Syntax (Toggle Plain Text)
<?php require('inc.class.php'); if(isset($_SESSION['who'])&& ($_SESSION['who']=1)){ $name = $_SESSION['fname']; 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>"; }//end if else{ $sms = new HtmlSms(); echo "<span style = 'color:red;'>Access denied! You aren't logged in<br />So please login or <a href='register.php'>Register</a></span>" ; $sms->login(); }//end else ?>
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
0
#15 20 Days Ago
my databse name was testlogin and table users. Here is a script to install table fields necessary
php Syntax (Toggle Plain Text)
<?php mysql_connect('localhost', 'root', 'jesus'); mysql_select_db('testlogin'); mysql_query("DROP TABLE IF EXISTS users ") or die(mysql_error()); $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"; mysql_query($query) or die(mysql_error()); echo 'successful created users table'; ?>
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
0
#16 20 Days Ago
my databse name was testlogin and table users. Here is a script to install table fields necessary
php Syntax (Toggle Plain Text)
<?php mysql_connect('localhost', 'root', 'jesus'); mysql_select_db('testlogin'); mysql_query("DROP TABLE IF EXISTS users ") or die(mysql_error()); $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"; mysql_query($query) or die(mysql_error()); echo 'successful created users table'; ?>
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
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
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
![]() |
Other Threads in the PHP Forum
- Previous Thread: upload
- Next Thread: radio button validation
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions simple sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube






