Logo
Simple User Login
Online Now: 1
Snippet Code
This little tutorial shows new users how to make a simple user login with a login form and database query.

login_page.php


<form action="verify.php" method="post">
    User Name:<br>
    <input type="text" name="username"><br><br>
    Password:<br>
    <input type="password" name="password"><br><br>
    <input type="submit" name="submit" value="Login">
</form>

verify.php


<?php
if(isset($_POST['submit'])){
    
$dbHost "localhost";        //Location Of Database usually its localhost
    
$dbUser "xxxx";            //Database User Name
    
$dbPass "xxxxxx";            //Database Password
    
$dbDatabase "db_name";    //Database Name
    
    
$db mysql_connect($dbHost,$dbUser,$dbPass)or die("Error connecting to database.");
    
//Connect to the databasse
    
mysql_select_db($dbDatabase$db)or die("Couldn't select the database.");
    
//Selects the database
    
    /*
    The Above code can be in a different file, then you can place include'filename.php'; instead.
    */
    
    //Lets search the databse for the user name and password
    //Choose some sort of password encryption, I choose sha256
    //Password function (Not In all versions of MySQL).
    
$usr mysql_real_escape_string($_POST['username']);
    
$pas hash('sha256'mysql_real_escape_string($_POST['password']));
    
$sql mysql_query("SELECT * FROM users_table 
        WHERE username='
$usr' AND
        password='
$pas'
        LIMIT 1"
);
    if(
mysql_num_rows($sql) == 1){
        
$row mysql_fetch_array($sql);
        
session_start();
        
$_SESSION['username'] = $row['username'];
        
$_SESSION['fname'] = $row['first_name'];
        
$_SESSION['lname'] = $row['last_name'];
        
$_SESSION['logged'] = TRUE;
        
header("Location: users_page.php"); // Modify to go to the page you would like
        
exit;
    }else{
        
header("Location: login_page.php");
        exit;
    }
}else{    
//If the form button wasn't submitted go to the index page, or login page
    
header("Location: index.php");    
    exit;
}
?>

users_page.php


<?php
session_start
();
if(!
$_SESSION['logged']){
    
header("Location: login_page.php");
    exit;
}
echo 
'Welcome, '.$_SESSION['username'];
?> 
 
Snippet Comments

Add Your Comment

is pneumonia contagious
2011-06-25 18:36:03
Interesting, Keep up the good work... Thanks for bringing this up
Vacation Packages
2011-06-22 20:16:46
There is so much that you can do with this login script. I have set it up for a lot my users. They just love to use it.
network services orlando florida
2011-06-21 18:56:10
The login process works so good. The code is setup so good. Keep up the good work.
mulberry bags
2011-06-13 23:44:29
Any kinds of footwear can be found here. At the same time, our inventory is really large and they are all with highly quality. Moreover, all the things we sale here are with the latest design and lowest price! So start your fashion trip from our Supra online store! Believe yourself and choose us, just from now on!
beats by dre studio
2011-06-07 21:53:23
The mood is very good!Do not use this snippet, it is very poorly coded.
dissertation writing
2011-06-04 07:44:26
I want to recommend this posting to my friends.
Software Development
2011-03-28 09:29:15
Can I just point out a flaw in your commenting system...you cannot view the post while you make the comment.
Matt_112
2011-02-27 16:45:46
Good login script, nice for small websites but there are a few vulnerabilities, such as session hijacking, bruteforce hacking, no spam protection, etc ..
free tech articles
2011-02-27 04:28:18
Thanks for the new php snip I might actually end up using this one.
NZ Free Web Hosting
2011-01-16 20:38:25
This was just the code I was looking for to allow logins on my Free Web Hosting page. Thanks.
php code
2010-09-14 00:26:56
i\'m not sure does it works fine on me. but it\'s ok
php code
2010-09-14 00:24:28
This Snippet has be updated, it now is shorter, and has better SQL Injection prevention. It still may need to be modified, if you don\'t feel it works for you.
PHPKode
2010-09-13 23:48:41
so easy but you can practise it immediatley
admin
2010-08-20 00:00:00
Thanks everyone for all your input! I have updated the snippet with everyones suggestions! If you have any more suggestions, please feel free to let me know! I am always listening.
admin
2010-08-20 00:00:00
To return to the index page after you login, you just need to modify the following 2 lines:

header(\"Location: login_page.php\");
exit;
Ebbsfleet
2010-07-17 00:00:00
Is there a way of returning to the index after you login? its a great script as building the login needed exact function I have had problems at first but this is the best exp so far! thanks margret
Anonymous
2010-05-04 00:00:00
Do not use this snippet, it is very poorly coded.
UK Software companies
2009-12-09 00:00:00
Interesting, Keep up the good work... Thanks for bringing this up
CasTex
2009-11-23 00:00:00
Good work, I wish this has no vulnerabilities.
custom software
2009-11-04 00:00:00
Its a great tutorial, Its all fixed and working perfectly, Keep up the good work,

Add Your Comment

 
Snippet Tools
Rate this Snippet:

Rate the difficulty level:

Request Snippet Update


Suggested Difficulty Level: Professional
Current Score: 2.66
Total votes: 323
Total Views: 26414

Other top snippets by admin:

1. JPEG to ASCII Converter
2. Add (th, st, nd, rd, th) to the end of a number
3. Dynamic Page Content From Links
4. AJAX Quickie
5. Simple Image CAPTCHA

Search
Search for:







User Panel

User name:

Password:

Register And Post Your Own Snippets

New Snippets

1.  Shorten text and (4 of 1)

2.  file-based simple guest (0 of 0)

3.  adding an entry (0 of 0)

4.  integer to currency (1.33 of 3)

5.  Aggregate IP addresses (0 of 0)

6.  file handling (0 of 0)

7.  testing system with (0 of 0)

8.  Make an php (1 of 1)

9.  allow download in (5 of 1)

10.  redirect in 3 (0 of 0)

11.  live clock with (0 of 0)

12.  visit counter (3.5 of 2)

13.  contact form with (4 of 1)

14.  show a specific (0 of 0)

15.  The & sign (0 of 0)

16.  static variable in (0 of 0)

17.  multiplication table with (0 of 0)

18.  function with multiplicationt (0 of 0)

19.  php generated menu (0 of 0)

20.  drawing a table (0 of 0)

21.  embedded array (one (0 of 0)

22.  checks the week (0 of 0)

23.  put links on (4.5 of 2)

24.  Directory Image Gallery (0 of 0)

25.  TOS (4 of 1)

Home | Forum | Free PHP Web Hosting | Contact | Terms & Conditions |  
Donate
PHPSnips.com - ©2012 Amethyst Creative