Help me with Login System | Newbie | | Join Date: Dec 2008
Posts: 28
| |
Hi all, after thinking for sometimes, I thought it will be great 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 -
<?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
-
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
-
?>
-
| |
best answer - posted by TheServant |
Sure. Let us know if you have a question. This section of Bytes is really for people who need some help with something specific. As much as we'd like to read through your code and impart some wisdom and knowledge in layout, syntax and method, proof reading code is not really in the job description. If you get an error, or something is not working as it should, post relavent code and all error messages and a full explanation, so we don't have to spend half our day looking through irrelevant code trying to find an unidentified problem.
If you're wanting to write a tutorial, write it in PHP insights.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 913
| | | re: Help me with Login System
Sure. Let us know if you have a question. This section of Bytes is really for people who need some help with something specific. As much as we'd like to read through your code and impart some wisdom and knowledge in layout, syntax and method, proof reading code is not really in the job description. If you get an error, or something is not working as it should, post relavent code and all error messages and a full explanation, so we don't have to spend half our day looking through irrelevant code trying to find an unidentified problem.
If you're wanting to write a tutorial, write it in PHP insights.
| | Newbie | | Join Date: Dec 2008
Posts: 28
| | | re: Help me with Login System
this is newbie start writting the script. So IWhat I wanted is criticism and suggestion. I want to end up with full secure login system. That is my intention and I believe it is in Job descriptin ;)
Sorry for being vague and welcome for help :)
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Help me with Login System
knowledge has its price… either money (if you hire someone) or effort (to learn it yourself).
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 913
| | | re: Help me with Login System Quote:
Originally Posted by Dormilich knowledge has its price… either money (if you hire someone) or effort (to learn it yourself). True. Apostle, you need to try and improve you script and come to us when you're stuck on something. Type in PHP login script, or login tutorial in Google and you'll have plenty of places to get the basics. Always start with the basics.
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,094
| | | re: Help me with Login System Quote:
Originally Posted by Apostle this is newbie start writting the script. So IWhat I wanted is criticism and suggestion. I want to end up with full secure login system. That is my intention and I believe it is in Job descriptin ;)
Sorry for being vague and welcome for help :) You need some major help!
What you had is not even a class. Here's what real class looks like:
All your other functions should be in a different file that use this class. I'll leave that for you to learn.
* YOUR BIGGEST MISTAKE *
You did not validate the user input before inserting them in an SQL.
Imagine if I tried to login to your used any bogus user name this for a password: hack' OR 1 = 1 LIMIT 1;
Thus your SQL would look like this when executed: -
-
SELECT * FROM users WHERE username = 'hacker' AND password = 'hack' OR 1=1 LIMIT 1;' ";
-
-
Then your check, which says the number of results should be 1 return true because i'm sure you have at least one user name in your users table where the number 1 is always equal to 1. This is called SQL INJECTION
Google the **** out of it. You're software is always unsecured without it.
I've done more than enough. I hope you learn PHP before you write unsafe software like this. I really REALLY hope you go read up on tutorials and practice programming and proper software testing before deploying any code.
Good luck,
Dan
| | Newbie | | Join Date: Dec 2008
Posts: 28
| | | re: Help me with Login System
Thanks Dan for Postive criticism.
I completely rewrote the whole thing and will post it here. For now I it is Here
I will post it here.
The reason I want to write from the scratch is to learn new thing as I go, and I know there are many experts that can drill and expose my ignorance on something and definitely improve my skills.
So feel free to criticize me or advice me on anything (code, good coding habits et al)
Thanks for your time guys :)
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 913
| | | re: Help me with Login System
Writing from scratch is the best for learning, and that is what you should do. However, when you start spending time developing, you can't re-write everything (and have a life) so you will need to learn how to use and modify already tried and tested code.
Again, we're here to help when you get stuck, and generally we don't read through screens of code, but if you post snippets for specific problems, we'll mention any issues with the surrounding code no probs ;)
| | Newbie | | Join Date: Dec 2008
Posts: 28
| | | re: Help me with Login System
Any recommended code that I can build upon? As per say, I'm beginner in these things and security matters alot in web apps :)
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Help me with Login System
currently the best measure against SQL Injection is using Prepared Statements (implemented in PHP’s MySQLi & PDO classes)
| | Newbie | | Join Date: Dec 2008
Posts: 28
| | | re: Help me with Login System
I have learned a little on MYSQLi, I will check for PDO!
If you don't mind you can provide me a link. For now, I going to google
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Help me with Login System MySQLi PDO
_________________
| | Newbie | | Join Date: Dec 2008
Posts: 28
| | | re: Help me with Login System
Thanks I'm going to check
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,094
| | | re: Help me with Login System
Learn OOP too while you're at it. Practice makes perfect. In the beginning working with already made code and reverse engineering it, modifying it, and especially improving and testing is the ultimate learning experience. That is how I learned PHP.
The reason I recommend OOP is I no longer see PHP as a scripting language and I use it for large applications.
In my opinion if someone wants to script, go learn Perl, PHP's sister. She's much much better at little scripts that make your life easier.
An advanced login script to me is an entry to a small to medium application. PHP/MySQL is a good choice for this.
Dan
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|