| | |
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
![]() |
Hi all, after thinking for sometimes, I thought it will be grat opportunity to learn if I will start from scratch and build my own register/login system. Here is the thread that I will be posting the progress and I hope you guys will help me.
The code below is what I have so far. Just put two scripts in the same directory and that is! I hope you will help me
Thanks!
class.php
index.php
The code below is what I have so far. Just put two scripts in the same directory and that is! I hope you will help me

Thanks!
class.php
php Syntax (Toggle Plain Text)
<?php //php login sytem class LoginRegister{ function __construct(){ } function displogin($status){ if ($status == "login"){ // post login page $enc = base64_encode('login'); $html = <<<LOGIN <form action = $_SERVER[PHP_SELF]?do=$enc, method = POST> <p>Username: <input type=text name = username /></p> <p>Password: <input type=password name = password /></p> <input type=submit value=Login /> </form> LOGIN; echo $html; }//end if else if ($status == "register"){ //post register page $enc = base64_encode('register'); $html = <<<LOGIN <form action = $_SERVER[PHP_SELF]?do=$enc, method = POST> <p>Username: <input type=text name = username /></p> <p>Password: <input type=password name = password /></p> <input type=submit value=Register /> </form> LOGIN; echo $html; }// end elese if } function auth($username, $password){ $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password' "; $res = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($res)==1){ echo "sucessful logged in as ". $username; }//end if else{ echo "<p style = 'color:red; font-weight:bold;'>Username or password not correct. <br /> New? Register!</p>"; $this->displogin('register'); }// end else } function checkempty($username, $password, $mode){ if (empty($username) or empty($password)){ echo "<p style = 'color:red; font-weight:bold;'>Empty Values are not allowed</p>"; $this->displogin('login'); }//end if else{ //do checking switch($mode){ case 'login': $this->auth($username, $password); case 'register': $this->adduser($username, $password); default: echo "<p style = 'color:red; font-weight:bold;'>Wrong Values are not allowed</p>"; $this->displogin('login'); }//end switch }//end else } function login($uname, $passwd){ //username $username = stripslashes($uname); $username = mysql_real_escape_string($uname); //passsword $password = stripslashes($passwd); $password = mysql_real_escape_string($passwd); //check for empty variables $this->checkempty($username, $password, 'login'); } function register($uname, $passwd){ //username $username = stripslashes($uname); $username = mysql_real_escape_string($uname); //passsword $password = stripslashes($passwd); $password = mysql_real_escape_string($passwd); //check for empty variables $this->checkempty($username, $password, 'register'); } function adduser($username, $password){ $sql = "INSERT INTO users(username, password) VALUES('$username', '$password')"; //redirect to login page echo "<p style = 'color:green; font-weight:bold;'>Thanks for registering. You can now login</p>"; $this->displogin('login'); mysql_query($sql) or die(mysql_error()); } }//end class ?>
index.php
php Syntax (Toggle Plain Text)
<?php require "class.php"; $obj = new LoginRegister(); $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("admin", $conn)or die(mysql_error()); if ((isset($_GET['do']))){ if (($_GET['do'])==(base64_encode('login'))){ $obj->login($_POST['username'], $_POST['password']); }//end middle first if else if(($_GET['do'])== (base64_encode('register'))){ $obj->register($_POST['username'], $_POST['password']); } else{ echo "<p style = 'color:red; font-weight:bold;'>Please Login</p>"; $obj->displogin('login'); //debug echo base64_encode('login').'<br />'; echo $_GET['do']; }//end else middle }//end last if else{ echo "<p style = 'color:green; font-weight:bold;'>Please Login</p>"; $obj->displogin('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
#3 Nov 1st, 2009
Hey.
I would recommend trying to keep logic classes clear of output. One of the fundamental ideas behind OOP is code re-usability, and by infusing the front-end code into the logic you are limiting the uses of the class to a single front-end.
What if, down the line, you need to provide XML output for use login, rather than HTML? Like say, if you decided to implement client-side login via AJAX.
For example, this class would only be useful by a HTML page:
While this one could be used to generate any output:
See what I mean? ;-)
I would recommend trying to keep logic classes clear of output. One of the fundamental ideas behind OOP is code re-usability, and by infusing the front-end code into the logic you are limiting the uses of the class to a single front-end.
What if, down the line, you need to provide XML output for use login, rather than HTML? Like say, if you decided to implement client-side login via AJAX.
For example, this class would only be useful by a HTML page:
php Syntax (Toggle Plain Text)
<?php class Member { public function authenticate($name, $password) { if(/* authentic */1) { echo '<span style="color: green;">Success!</span>'; } else { echo '<span style="color: red;">I\'m afraid I can\'t do that, {$name}.</span>'; } } } ?>
html Syntax (Toggle Plain Text)
<html> <body> <div> <?php $member = new Member(); $member->authenticate('input', 'more input'); ?> </div> </body> </html>
While this one could be used to generate any output:
php Syntax (Toggle Plain Text)
<?php class Member { public function authenticate($name, $password) { if(/* authentic */1) { return true; } else { return false; } } } ?>
html Syntax (Toggle Plain Text)
<html> <body> <div> <?php $member = new Member(); if($member->authenticate('input', 'more input')) { echo '<span style="color: green;">Success!</span>'; } else { echo '<span style="color: red;">I\'m afraid I can\'t do that, {$name}.</span>'; } ?> </div> </body> </html>
xml Syntax (Toggle Plain Text)
<root> <?php $member = new Member(); $result = $member->authenticate('input', 'more input'); ?> <result message="<?php echo $result ? "success" : "failure"; ?> "/> </root>
See what I mean? ;-)
Last edited by Atli; Nov 1st, 2009 at 1:03 am. Reason: Perfectionist xD
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
0
#4 Nov 1st, 2009
I see,
Can you help me point out method names that you see will prevent reuse? I see that I should change all login prompts may be to use XML (is that what you said?). But I'm afraid I might forget some. So pointing methods that need to be removed/changed for sake of re-use will be great help to me.
Another thing is, I have not learned Ajax.
All I have is the basics of both JavaScript and XML, but not anything Ajaxed.
Anyway, is ajax "long curve" to learn? I would be happy to learn and implement that.
I appreciate your reply, which is great
Can you help me point out method names that you see will prevent reuse? I see that I should change all login prompts may be to use XML (is that what you said?). But I'm afraid I might forget some. So pointing methods that need to be removed/changed for sake of re-use will be great help to me.
Another thing is, I have not learned Ajax.
All I have is the basics of both JavaScript and XML, but not anything Ajaxed.
Anyway, is ajax "long curve" to learn? I would be happy to learn and implement that.
I appreciate your reply, which is great
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
#5 Nov 1st, 2009
•
•
•
•
I see,
Can you help me point out method names that you see will prevent reuse? I see that I should change all login prompts may be to use XML (is that what you said?). But I'm afraid I might forget some. So pointing methods that need to be removed/changed for sake of re-use will be great help to me.
Another thing is, I have not learned Ajax.
All I have is the basics of both JavaScript and XML, but not anything Ajaxed.
Anyway, is ajax "long curve" to learn? I would be happy to learn and implement that.
I appreciate your reply, which is great
The easiest way to implement it would be using a library, such as jQuery or the scriptaculous ones.
Be aware that 'AJAX' requires JavaScript to be available, so design your sites to work without it.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
1
#6 Nov 1st, 2009
•
•
•
•
Can you help me point out method names that you see will prevent reuse?
The displogin method also echoes HTML, but it can hardly be considered a logic method. It's more a HTML-specific display method. I would actually argue that it doesn't belong to that class, but rather in the HTML page itself, but that's a different matter.
As a side-note. You should try to avoid abbreviating your method names, like "auth". Even if it seems obvious to you what it stands for at the moment, it may not seem that obvious in the future, if you, or whoever receives your code, ever need to review it.
It's best to adopt a good naming convention and stick to it throughout the project. The PHP "standard" is to use all-lower case names, using _ to separate words. ('authenticate', 'check_empty', 'add_user').
Personally, I like the C# naming conventions. ('Authenticate', 'CheckEmpty', 'AddUser'). It allows me to more clearly distinguish between public and private methods. ('PublicMethod', 'privateMethod')
•
•
•
•
I see that I should change all login prompts may be to use XML (is that what you said?)
•
•
•
•
Another thing is, I have not learned Ajax.
All I have is the basics of both JavaScript and XML, but not anything Ajaxed.
Anyway, is ajax "long curve" to learn? I would be happy to learn and implement that.
Check out the W3Schools AJAX tutorial. It explains the basics of AJAX very quickly.
Last edited by Atli; Nov 1st, 2009 at 12:52 pm. Reason: I need to stop trusting the spell-checker xD
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
•
•
Join Date: Oct 2009
Posts: 101
Reputation:
Solved Threads: 18
0
#7 Nov 1st, 2009
Just keep your classes separate by thinking of what they are there for.
The classes/methods that access the database or some other datastore and (auth($username, $password)) process logic (also known as the Model or DAO - Data Access Objects) should be separated from classes/methods that just display some information given by the model (displogin($status)) - also known as the View.
Having controller classes sit in the middle and use different view classes (As Atli said, HTML, XML or Ajax) depending on your need is a good idea.
Do some research on the MVC pattern, it is really useful.
The classes/methods that access the database or some other datastore and (auth($username, $password)) process logic (also known as the Model or DAO - Data Access Objects) should be separated from classes/methods that just display some information given by the model (displogin($status)) - also known as the View.
Having controller classes sit in the middle and use different view classes (As Atli said, HTML, XML or Ajax) depending on your need is a good idea.
Do some research on the MVC pattern, it is really useful.
0
#8 Nov 1st, 2009
Thanks guys for your criticism and suggestions. What I say is, they are really really great! They help me improve and I will work on them and make an improved code. Before I implement above suggestions, here is what I have thought to do:
1. Separate into 3 classes (Database Connection, Login logic, Displaying HTML messages)
2. Apply naming Standard as Atli said (I loved that as I use them in Pythoning)
3. Check libraries like JQuery like Gresham said and take alook at Ajax
More suggestions and criticism are invited. Anyway, after it stands well I will add some security tightening like sessions, deciphering, et al
1. Separate into 3 classes (Database Connection, Login logic, Displaying HTML messages)
2. Apply naming Standard as Atli said (I loved that as I use them in Pythoning)

3. Check libraries like JQuery like Gresham said and take alook at Ajax
More suggestions and criticism are invited. Anyway, after it stands well I will add some security tightening like sessions, deciphering, et al
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
#9 Nov 1st, 2009
Thanks guys for your criticism and suggestions. What I say is, they are really really great! They help me improve and I will work on them and make an improved code. Before I implement above suggestions, here is what I have thought to do:
1. Separate into 3 classes (Database Connection, Login logic, Displaying HTML messages)
2. Apply naming Standard as Atli said (I loved that as I use them in Pythoning)
3. Check libraries like JQuery like Gresham said and take alook at Ajax
More suggestions and criticism are invited. Anyway, after it stands well I will add some security tightening like sessions, deciphering, et al
1. Separate into 3 classes (Database Connection, Login logic, Displaying HTML messages)
2. Apply naming Standard as Atli said (I loved that as I use them in Pythoning)

3. Check libraries like JQuery like Gresham said and take alook at Ajax
More suggestions and criticism are invited. Anyway, after it stands well I will add some security tightening like sessions, deciphering, et al
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
#10 30 Days Ago
I completely re-wrote the whole class at home. I added features like encryption, email check et al. I will post it tomorrow as I left laptop at home. Thanks for suggestions.
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 ajax apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date display dissertation dynamic echo echo$_get[x]changingitintovariable... email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link local login loop mail memberships menu mlm multiple multipletables mysql mysqlquery oop open paypal pdf persist php problem query radio random recursion regex remote rss script search server sessions sms soap sockets source space spam sql syntax system table tutorial update upload url validator variable video web xml youtube






