sniko
11-07-2010, 12:13 AM
Overview
A login system is a way to filter unwanted activity in area's you dont want that activity to be, for such, a comment system. When making a login system, it is vital that you make it secure from any type attacks or bypassing, otherwise, there really is no point having one.
What do you need to learn
Sessions (http://www.php.net/sessions)
Mysql Functions (http://www.php.net/mysql)
mres (http://www.php.net/mysql_real_escape_string)
unset (http://www.php.net/unset)
Switch (Possibly) (http://www.php.net/switch)
connect to the database (http://uk.php.net/manual/en/function.mysql-connect.php)
What you need
Knowledge of using a database (to hold users information)
Database management system, For example phpmyadmin
Psuedo code In plain English, what do you need to do?
connect to the database
start sessions
show a form, where the user types in their credentails
secure input data from the user
check user inputs against database
show any errors at all
start login session
show success text
Ways of going about creating a login system
There are many ways of creating the login system. Using the switch function or the predefined variable $_GET/isset you can create the whole login and register system in one file. You can also add jQuery/Ajax effects to make it more user friends and customise it with css.
Skeleton Code
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('foo', $link);
session_start();
if(isset($_POST['submit'])) //pressed the submit button
{
* //do more
}
else
{
*//show form
}
?>
Creating the form
I assume you know the pre-defined variable $_POST and how to use it.
<form action='' method='post'>
* * * * *Login Name: <input type='text' name='login' length='5' maxlength='15' />
* * * * *Passcode: <input type='password' name='passcode' length='5' maxlength='25' />
* * * * *<input type='submit' name='submit' value='Login!'>
</form>
Skeleton Code [Update w/ Form]
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('foo', $link);
session_start();
if(isset($_POST['submit'])) //pressed the submit button
{
* //do more
}
else
{
* echo "<form action='' method='post'>
* * * * *Login Name: <input type='text' name='login' length='5' maxlength='15' />
* * * * *Passcode: <input type='password' name='passcode' length='5' maxlength='25' />
* * * * *<input type='submit' name='submit' value='Login!'>
* * * * *</form>";
}
?>
Assigning the inputs to variables and securing them
<?php
$login_name = htmlspecialchars(mysql_real_escape_string($_POST['login']));
$passcode = md5($_POST['passcode']); //Its best to use an alternative encryption method rather than md5 as it can be decrypted
?>
Checking to see if their inputted data exists on the database (assuming you have already created one)
<?php
$exists = mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'");
if(mysql_num_rows($exists) == 0)
*{
* * *echo "You do not exist!";
* }
?>
Skeleton Code Updated (w/ Form, $_POST and checking)
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('foo', $link);
session_start();
if(isset($_POST['submit'])) //pressed the submit button
{
*$login_name = htmlspecialchars(mysql_real_escape_string($_POST['login']));
*$passcode = md5($_POST['passcode']); //Its best to use an alternative encryption method rather than md5 as it can be decrypted
*$exists = mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'");
*if(mysql_num_rows($exists) == 0)
* *{
* * *echo "You do not exist!";
* * }
*else
* *{
* * * //They exist
* * }
}
else
{
* echo "<form action='' method='post'>
* * * * *Login Name: <input type='text' name='login' length='5' maxlength='15' />
* * * * *Passcode: <input type='password' name='passcode' length='5' maxlength='25' />
* * * * *<input type='submit' name='submit' value='Login!'>
* * * * *</form>";
}
?>
Assigning a session
<?php
$u = mysql_fetch_array(mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'"));
$_SESSION['user'] = $u['key'];
?>
Skeleton Code - Finished
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('foo', $link);
session_start();
if(isset($_POST['submit'])) //pressed the submit button
{
*$login_name = htmlspecialchars(mysql_real_escape_string($_POST['login']));
*$passcode = md5($_POST['passcode']); //Its best to use an alternative encryption method rather than md5 as it can be decrypted
*$exists = mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'");
*if(mysql_num_rows($exists) == 0)
* *{
* * *echo "You do not exist!";
* * }
*else
* *{
* * * $u = mysql_fetch_array(mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'"));
* * * $_SESSION['user'] = $u['key'];
* * * echo "You have loggedin!";
* * }
}
else
{
* echo "<form action='' method='post'>
* * * * *Login Name: <input type='text' name='login' length='5' maxlength='15' />
* * * * *Passcode: <input type='password' name='passcode' length='5' maxlength='25' />
* * * * *<input type='submit' name='submit' value='Login!'>
* * * * *</form>";
}
?>
Now that they have loggedin, you can then do the following with all other pages that require them to be loggedin
<?php
session_start();
if(!isset($_SESSION['user']))
{
* echo "You need to login";
* exit;
}
?>
Thanks for reading, if you have any other questions, Please reply or PM
-sniko
A login system is a way to filter unwanted activity in area's you dont want that activity to be, for such, a comment system. When making a login system, it is vital that you make it secure from any type attacks or bypassing, otherwise, there really is no point having one.
What do you need to learn
Sessions (http://www.php.net/sessions)
Mysql Functions (http://www.php.net/mysql)
mres (http://www.php.net/mysql_real_escape_string)
unset (http://www.php.net/unset)
Switch (Possibly) (http://www.php.net/switch)
connect to the database (http://uk.php.net/manual/en/function.mysql-connect.php)
What you need
Knowledge of using a database (to hold users information)
Database management system, For example phpmyadmin
Psuedo code In plain English, what do you need to do?
connect to the database
start sessions
show a form, where the user types in their credentails
secure input data from the user
check user inputs against database
show any errors at all
start login session
show success text
Ways of going about creating a login system
There are many ways of creating the login system. Using the switch function or the predefined variable $_GET/isset you can create the whole login and register system in one file. You can also add jQuery/Ajax effects to make it more user friends and customise it with css.
Skeleton Code
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('foo', $link);
session_start();
if(isset($_POST['submit'])) //pressed the submit button
{
* //do more
}
else
{
*//show form
}
?>
Creating the form
I assume you know the pre-defined variable $_POST and how to use it.
<form action='' method='post'>
* * * * *Login Name: <input type='text' name='login' length='5' maxlength='15' />
* * * * *Passcode: <input type='password' name='passcode' length='5' maxlength='25' />
* * * * *<input type='submit' name='submit' value='Login!'>
</form>
Skeleton Code [Update w/ Form]
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('foo', $link);
session_start();
if(isset($_POST['submit'])) //pressed the submit button
{
* //do more
}
else
{
* echo "<form action='' method='post'>
* * * * *Login Name: <input type='text' name='login' length='5' maxlength='15' />
* * * * *Passcode: <input type='password' name='passcode' length='5' maxlength='25' />
* * * * *<input type='submit' name='submit' value='Login!'>
* * * * *</form>";
}
?>
Assigning the inputs to variables and securing them
<?php
$login_name = htmlspecialchars(mysql_real_escape_string($_POST['login']));
$passcode = md5($_POST['passcode']); //Its best to use an alternative encryption method rather than md5 as it can be decrypted
?>
Checking to see if their inputted data exists on the database (assuming you have already created one)
<?php
$exists = mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'");
if(mysql_num_rows($exists) == 0)
*{
* * *echo "You do not exist!";
* }
?>
Skeleton Code Updated (w/ Form, $_POST and checking)
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('foo', $link);
session_start();
if(isset($_POST['submit'])) //pressed the submit button
{
*$login_name = htmlspecialchars(mysql_real_escape_string($_POST['login']));
*$passcode = md5($_POST['passcode']); //Its best to use an alternative encryption method rather than md5 as it can be decrypted
*$exists = mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'");
*if(mysql_num_rows($exists) == 0)
* *{
* * *echo "You do not exist!";
* * }
*else
* *{
* * * //They exist
* * }
}
else
{
* echo "<form action='' method='post'>
* * * * *Login Name: <input type='text' name='login' length='5' maxlength='15' />
* * * * *Passcode: <input type='password' name='passcode' length='5' maxlength='25' />
* * * * *<input type='submit' name='submit' value='Login!'>
* * * * *</form>";
}
?>
Assigning a session
<?php
$u = mysql_fetch_array(mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'"));
$_SESSION['user'] = $u['key'];
?>
Skeleton Code - Finished
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('foo', $link);
session_start();
if(isset($_POST['submit'])) //pressed the submit button
{
*$login_name = htmlspecialchars(mysql_real_escape_string($_POST['login']));
*$passcode = md5($_POST['passcode']); //Its best to use an alternative encryption method rather than md5 as it can be decrypted
*$exists = mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'");
*if(mysql_num_rows($exists) == 0)
* *{
* * *echo "You do not exist!";
* * }
*else
* *{
* * * $u = mysql_fetch_array(mysql_query("SELECT `key` FROM `tablename` WHERE `fieldname`='$login_name' AND `fieldname`='$passcode'"));
* * * $_SESSION['user'] = $u['key'];
* * * echo "You have loggedin!";
* * }
}
else
{
* echo "<form action='' method='post'>
* * * * *Login Name: <input type='text' name='login' length='5' maxlength='15' />
* * * * *Passcode: <input type='password' name='passcode' length='5' maxlength='25' />
* * * * *<input type='submit' name='submit' value='Login!'>
* * * * *</form>";
}
?>
Now that they have loggedin, you can then do the following with all other pages that require them to be loggedin
<?php
session_start();
if(!isset($_SESSION['user']))
{
* echo "You need to login";
* exit;
}
?>
Thanks for reading, if you have any other questions, Please reply or PM
-sniko