Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-11-2007, 04:45 PM
Senior Member
 
Join Date: May 2007
Posts: 181
Blog Entries: 1
skyfe is on a distinguished road
php login system

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.

Quote:
session_start();
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
}
}
}
?>

Attached Files
File Type: zip login.zip (815 Bytes, 117 views)
Reply With Quote
  #2 (permalink)  
Old 06-13-2007, 10:45 AM
Junior Member
 
Join Date: Apr 2007
Posts: 20
alwin is on a distinguished road
wow nice i will try it out i was need something like this thank you
Reply With Quote
  #3 (permalink)  
Old 06-16-2007, 11:09 AM
Junior Member
 
Join Date: Mar 2007
Posts: 19
Kasharas is on a distinguished road
cool, this fits to reg script
Reply With Quote
  #4 (permalink)  
Old 06-18-2007, 06:31 AM
Junior Member
 
Join Date: Oct 2006
Posts: 25
FtK Shadow is on a distinguished road
I have been looking for something like this.
Reply With Quote
  #5 (permalink)  
Old 06-18-2007, 10:20 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Nice tut mate

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #6 (permalink)  
Old 06-19-2007, 12:20 AM
Member
 
Join Date: Feb 2007
Posts: 57
matthewgrainger is on a distinguished road
Cool.. looks good
__________________
StudentMeh.co.uk - Next Generation Student Networking!
Reply With Quote
  #7 (permalink)  
Old 06-19-2007, 10:46 AM
Junior Member
 
Join Date: Jun 2007
Posts: 19
dtfan14 is on a distinguished road
Thanks for your help, I needed this for my website.
Reply With Quote
  #8 (permalink)  
Old 06-19-2007, 11:04 AM
Junior Member
 
Join Date: Jan 2007
Posts: 20
decore is on a distinguished road
i didnt find a secure for the $act
Is he secured?
Reply With Quote
  #9 (permalink)  
Old 06-19-2007, 05:07 PM
Senior Member
 
Join Date: May 2007
Posts: 181
Blog Entries: 1
skyfe is on a distinguished road
Quote:
Originally Posted by decore View Post
i didnt find a secure for the $act
Is he secured?
what do you mean with, the $act?
Reply With Quote
  #10 (permalink)  
Old 06-22-2007, 10:39 AM
Junior Member
 
Join Date: Jun 2007
Posts: 21
Nightfox is on a distinguished road
Thanks i will use this on my website!

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 01:59 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com