Hello,
today I'm going to explain how to create a login system. It's not so hard, you only have to know HOW to do

Let's start with the form, which requires the user to fill in his username and password. Take for example: we have an table in the database called 'accounts' where all registered accounts are into, with the fields:
id - INT - primaire key
username - VARCHAR
password - VARCHAR
Oke, lets start with the form.
Quote:
<form method='POST' action='login.php'>
<b>username:</b> <input type='text' name='username'><br>
<b>password:</b> <input type='password' name='password'><br>
<input type='submit' value='login' name='submit'>
</form>
|
we just started with a simpel form, where the user have to fill in his username and password, once the user clicked on the submit button (value: login name:submit) it will set all filled in values, into $_POST['field_name'] and will redirect to login.php(as you can see in the form tag with ; action='login.php'). If you want you can give it a layout by yourself.
Oke, now we have to go, check if the values the user filled in, are correct; if there does exist an account with the filled in username and password.
Quote:
<?php
session_start();
mysql_connect("localhost","root","password");
mysql_select_db("database");
if(isset($_POST['submit'])) {
if(mysql_num_rows(mysql_query("SELECT username, password FROM accounts WHERE username = '".$_POST['username']."' && password = '".$_POST['password']."' ")) > 0) {
if(mysql_num_rows(mysql_query("SELECT id FROM accounts WHERE username = '".$_POST['username']."' && password = '".$_POST['password']."' ")) > 0 ) {
$_SESSION['logged'] = true;
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
echo "<font color='green'>You have succsfully logged in!</font>";
}else{ //if the username and password aren't from the same account but does exist
echo "<font color='red'>Incorrect login! Please try again</font>";
}
}else{ //username/password doesn't exist
echo "<font color='red'>Username/Password doesn't exist!</font>";
}
}
?>
|
Oke, let's go trough the script.
this will make us avaible to use sessions
Quote:
mysql_connect("localhost","root","password");
mysql_select_db("database");
|
connect to the database and host, change it to the correct information for your host and database
Quote:
|
if(isset($_POST['submit'])) {
|
if the user pressed on the submit (login) button, and so tried to login
Quote:
|
if(mysql_num_rows(mysql_query("SELECT username, password FROM accounts WHERE username = '".$_POST['username']."' && password = '".$_POST['password']."' ")) > 0) {
|
check if the username and password does exist, it will count the query which will select the fields 'username' and 'password' from the table 'accounts' and search to those fields where the field username = what the user filled in the field 'username' and search to a field 'password' where the value is equal to what the user filled in in the field (called)'password'. If the query find at least 1 result, so if the username and password does exist, then it will going to check the follow:
Quote:
|
if(mysql_num_rows(mysql_query("SELECT id FROM accounts WHERE username = '".$_POST['username']."' && password = '".$_POST['password']."' ")) > 0 ) {
|
this will check if the username and password are also from the same account, and not from 2 other accounts selected. It will select with the query a row where the username = what the user filled in in the field 'username' and where the password = what the user filled in in the field 'password' ($_POST['field_name'] will obtain the value of the field_name, so to check what the user filled in in the field called 'username' you can use $_POST['username'] and for the field called 'password'; $_POST['password'])
if that's also true, it will set some sessions;
Quote:
$_SESSION['logged'] = true;
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
|
we'll set $_SESSION['logged'] = true, so we can later check on other pages if the session logged does already exist, if so, then the user is logged in so he'll get access.
Oke, lets put all together.
Quote:
<?php
session_start();
mysql_connect("localhost","root","password");
mysql_select_db("database");
if(isset($_SESSION['logged'])) { //if the user is already logged in
echo "Welcome ".$_SESSION['username']; //show a welcomes message
}else{ //if not
if(isset($_POST['submit'])) {
if(mysql_num_rows(mysql_query("SELECT username, password FROM accounts WHERE username = '".$_POST['username']."' && password = '".$_POST['password']."' ")) > 0) {
if(mysql_num_rows(mysql_query("SELECT id FROM accounts WHERE username = '".$_POST['username']."' && password = '".$_POST['password']."' ")) > 0 ) {
$_SESSION['logged'] = true;
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
echo "<font color='green'>You have succsfully logged in!</font>";
}else{ //if the username and password aren't from the same account but does exist
echo "<font color='red'>Incorrect login! Please try again</font>";
}
}else{ //username/password doesn't exist
echo "<font color='red'>Username/Password doesn't exist!</font>";
}
}else{ //if the user didn't press on the submit(login) button yet, show the form
?>
<form method='POST' action='login.php'>
<b>username:</b> <input type='text' name='username'><br>
<b>password:</b> <input type='password' name='password'><br>
<input type='submit' value='login' name='submit'>
</form>
<?php
}
}
?>
|
now we only need a logout system, it's verry easy, as you can see the system for loggin in works like this:
user -> fill in his username + password -> check if it's correct -> if it's correct it will show a welcomes message and set $_SESSION['logged'] equal to 'true', so we know: if the $_SESSION['logged'] does exist and is equal to 'true', THEN the user is already logged in.
So now we actually have to destroy the $_session['logged'], so if it will check if the user is logged in by checking the $_session['logged'], it will see it doesn't exist; so the user is not logged in. I hope you'll understand it. We'll need to make a logout button, once the user pressed it, it will destroy all sessions, also the session that says the user is logged in, so the user isn't logged in anymore once all sessions have been destroyed.
Quote:
<form method='POST' action='login.php'>
<input type='submit' value='logout' name='logout'>
</form>
|
we just made a logout button, called 'logout', we now have to make a php script which checks if that button have been pressed; if so, then destroy all session so the user is 'logged out'.
Quote:
<?php
if(isset($_POST['logout'])) {
session_destroy();
echo "You have succesfully logged out! Click <a href='login.php'>here</a> to proceed!";
}
?>
|
we again use the function
isset() it will check if the submit button called 'logout' have been pressed, if that's the case; it will destroy all sessions by th function:
session_destroy(); Oke let's put all together for the full login and logout system.
Quote:
<?php
session_start();
mysql_connect("localhost","root","password");
mysql_select_db("database");
if(isset($_POST['logout'])) {
session_destroy();
echo "You have succesfully logged out! Click <a href='login.php'>here</a> to proceed!";
}else{ //if the user didn't try to logout
if(isset($_SESSION['logged'])) { //if the user is already logged in
echo "Welcome ".$_SESSION['username']; //show a welcomes message
?>
<form method='POST' action='login.php'>
<input type='submit' value='logout' name='logout'>
</form>
<?php
}else{ //if not
if(isset($_POST['submit'])) {
if(mysql_num_rows(mysql_query("SELECT username, password FROM accounts WHERE username = '".$_POST['username']."' && password = '".$_POST['password']."' ")) > 0) {
if(mysql_num_rows(mysql_query("SELECT id FROM accounts WHERE username = '".$_POST['username']."' && password = '".$_POST['password']."' ")) > 0 ) {
$_SESSION['logged'] = true;
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
echo "<font color='green'>You have succsfully logged in!</font>";
}else{ //if the username and password aren't from the same account but does exist
echo "<font color='red'>Incorrect login! Please try again</font>";
}
}else{ //username/password doesn't exist
echo "<font color='red'>Username/Password doesn't exist!</font>";
}
}else{ //if the user didn't press on the submit(login) button yet, show the form
?>
<form method='POST' action='login.php'>
<b>username:</b> <input type='text' name='username'><br>
<b>password:</b> <input type='password' name='password'><br>
<input type='submit' value='login' name='submit'>
</form>
<?php
}
}
}
?>
|