Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 135,930 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,649 people online right now. Registration is fast and FREE... Join Now!




Basic Login Script with PHP

 
Reply to this topicStart new topic

Basic Login Script with PHP, A rudimentary login script tutorial aimed at those looking to learn ho

akozlik
22 May, 2008 - 08:51 AM
Post #1

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 603



Thanked: 22 times
Dream Kudos: 750
My Contributions
This tutorial will attempt to teach you how to build a rudimentary login system for your site. It's assumed that you understand MySQL concepts, as well as session variables and form handling.

To begin, you will need to create a new table in your database named 'users'. In this database create three new fields 'id' (primary key), 'username', 'password'. You can add more fields as you need them later. For now we're just going to worry about checking for an existing username and password combination.

Next, create your html page with the login form. Below is a quick sample of a form you can build.

CODE

<form action="checkLogin.php" method="post">
     <table>
          <tr>
               <td>Username: </td>
               </td><input type="text" name="user"></td>
          </tr>
          <tr>
               <td>Password: </td>
               <td><input type="password" name="pass"></td>
          </tr>
                  <tr>
                           <td></td>
                           <td><input type="submit" value="Submit"></td>
                  </tr>
     </table>
</form>


Please notice that we are using the POST method for the form. This is to ensure that the username and password aren't passed as URL parameters, which is a security flaw for obvious reasons.

Next we'll code our checkLogin.php page. This page is going to select all the rows with matching username and password combinations. There should only be one row that does so, which is our valid row. I'm not going to cover data integrity here, but you'll definitely want to sanitze your data from SQL injection. Keeping in the theme of my tutorials though, I only want to focus on the task at hand.

CODE

<?php
// checkLogin.php

session_start(); // Start a new session
require('conn.php'); // Holds all of our database connection information

// Get the data passed from the form
$username = $_POST['user'];
$password = $_POST['password'];

// Do some basic sanitizing
$username = stripslashes($username);
$password = stripslashes($password);

$sql = "select * from users where username = '$username' and password = '$password'";
$result = mysql_query($sql) or die ( mysql_error() );

$count = 0;

while ($line = mysql_fetch_assoc($result)) {
     $count++;
}

if ($count == 1) {
     $_SESSION['loggedIn'] = "true";
     header("Location: loginSuccess.php"); // This is wherever you want to redirect the user to
} else {
     $_SESSION['loggedIn'] = "false";
     header("Location: loginFailed.php"); // Wherever you want the user to go when they fail the login
}

?>


You may want to consider posting the form to PHP_SELF for basic error handling, or you can pass error messages through the url parameter, it's up to you. As I said, this is just a rudimentary example of how to set up a basic user login script. From here, if you want to check and see if a user is logged in, just put the following at the top of a page.

CODE

<?php
session_start();
if ($_SESSION['loggedIn'] != "true") {
     header("Location: http://www.whatever.com/login.php");
}

?>


Naturally there are many different ways to achieve the same thing in PHP. This script is great for basic logins, but may not be what you need for something more complex. Adapt it to your needs or just use it as a place to begin learning. Hope everything is clear with the instructions. As usual, questions and comments are more than welcome. Take care.

This post has been edited by akozlik: 22 May, 2008 - 02:11 PM
User is offlineProfile CardPM
+Quote Post

akozlik
RE: Basic Login Script With PHP
22 May, 2008 - 11:03 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 603



Thanked: 22 times
Dream Kudos: 750
My Contributions
Damn, this was actually supposed to be submitted under Tutorials. I resubmitted it under that category, but I can't seem to close it out here. If the Admin or Moderators can help let me know.
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Basic Login Script With PHP
22 May, 2008 - 12:50 PM
Post #3

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,264



Thanked: 14 times
Dream Kudos: 650
My Contributions
Just a tip - you don't have a submit button on your form.
User is offlineProfile CardPM
+Quote Post

akozlik
RE: Basic Login Script With PHP
22 May, 2008 - 02:12 PM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 603



Thanked: 22 times
Dream Kudos: 750
My Contributions
QUOTE(girasquid @ 22 May, 2008 - 01:50 PM) *

Just a tip - you don't have a submit button on your form.


Indeed I didn't. It's edited. Thanks for catching that.
User is offlineProfile CardPM
+Quote Post

rjolitz
RE: Basic Login Script With PHP
23 May, 2008 - 04:03 AM
Post #5

D.I.C Head
Group Icon

Joined: 17 May, 2008
Posts: 86



Thanked: 1 times
Dream Kudos: 75
My Contributions
Thank you for this. Very easy to follow!

Rich



QUOTE(akozlik @ 22 May, 2008 - 09:51 AM) *

This tutorial will attempt to teach you how to build a rudimentary login system for your site. It's assumed that you understand MySQL concepts, as well as session variables and form handling.


User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Basic Login Script With PHP
23 May, 2008 - 04:57 AM
Post #6

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 476



Thanked: 22 times
Dream Kudos: 350
My Contributions
for a basic script, it's actually pretty good... however I would suggest adding in some type of password hashing, such as MD5 or SHA-1....

you also might want to include setting up the database....

now, since it's a tutorial, and a basic one, I won't go into the more detailed, such as COUNT(*) wink2.gif
User is offlineProfile CardPM
+Quote Post

akozlik
RE: Basic Login Script With PHP
23 May, 2008 - 08:35 AM
Post #7

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 603



Thanked: 22 times
Dream Kudos: 750
My Contributions
QUOTE(JBrace1990 @ 23 May, 2008 - 05:57 AM) *

for a basic script, it's actually pretty good... however I would suggest adding in some type of password hashing, such as MD5 or SHA-1....


I definitely agree with the hashing. I meant to mention something about it, but I forgot. You should definitely be hashing your passwords before you store them in the database, and then hash the login password and check that against the one stored.

Also, using COUNT is a good idea. That just shows that there are many different ways to achieve the same affect. Using COUNT would actually be more efficient, but I figured this would be a good way to get people started on user authentication.

Thanks for the comments and suggestions.
User is offlineProfile CardPM
+Quote Post

Chubber
RE: Basic Login Script With PHP
29 May, 2008 - 09:30 AM
Post #8

D.I.C Head
Group Icon

Joined: 16 Oct, 2006
Posts: 118


Dream Kudos: 225
My Contributions
And how about some protection from SQL Injection?
User is offlineProfile CardPM
+Quote Post

akozlik
RE: Basic Login Script With PHP
29 May, 2008 - 10:12 AM
Post #9

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 603



Thanked: 22 times
Dream Kudos: 750
My Contributions
QUOTE

I'm not going to cover data integrity here, but you'll definitely want to sanitze your data from SQL injection.


Yeah I mentioned that in the tutorial to look into SQL injection as well. I just wanted to keep this tutorial simple so people could understand the concepts behind user authentication systems. SQL Injection is something that could be covered in its own tutorial, as it's a beast all of its own.

Also, in response to the hash comment, I recently wrote a tutorial on hash techniques. It can be found here. That could be integrated with this tutorial for the truly adventurous. Check them out if you get a chance.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:23AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month