Very Simple Login-script - This is a very simple and secure login-script

Pages: 1, 2
free web hosting

Latest Entry: (Post #18) by kanade on Jun 28 2008, 02:46 PM. (Line Breaks Removed)
QUOTE(Feelay @ Apr 22 2008, 08:17 PM) I use require_once when I want to "include" something, but not exactly include it.. it is hard to explain.. I just know, that when you want to check a database file, or something like it, it is better to use the require function instead of include. include is better to use when you want to include a part of a page.BTW: Sorry for the late ans... read more.
Express your Opinion! Contribute Knowledge.

Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > PHP

Very Simple Login-script - This is a very simple and secure login-script

Feelay
Hi. This is my first post here. please Tell me if i do something wrong.
This is a very simple and secure login script. I will try to add as many comments as possible, to make it easier to
understand.

Lets start with the database.

Just make a new SQL file, and call it whatever you want. Paste this code:

CODE
CREATE TABLE `user` (
  `id` int(4) unsigned NOT NULL auto_increment,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  `level` int(4) default '1',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=latin1;


Now we have made a table called 'user' in the SQL
We have made 4 colums.
Id, Username, Password, And Level.
Level is made for the Admin level.


Save the file, and import it into your database.
Now that part is done.

Now lets begin with the Index.phppage.

CODE
<?php
session_start();
require_once 'database.php';
if (isset($_SESSION['user'])){
echo "Welcome ".$_SESSION['user'];
?>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" id="logout" value="Logout">
</form>
<br /><form name="news" method="post" action="news.php">
  <input type="submit" name="news" id="news" value="News">
</form>
<?php
}

elseif(isset($_SESSION['admin'])){
echo"Welcome ".$_SESSION['admin'];
echo"<br><br>You are logged in as an Admin";
?>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" id="logout" value="Logout">
</form>
<br /><form name="news" method="post" action="news.php">
  <input type="submit" name="news" id="news" value="News">
</form>
<?php

}else{
?>
<form name="login_form" method="post" action="login2.php">
  <label>
  <input name="user" type="text" id="user">ID<br />
  <input name="pass" type="password" id="pass">Password<br />
  </label>
<input type="submit" name="login" id="login" value="Login">
   </label>
</p>
</form>
<form name="Register" method="post" action="reg.php">
  <input type="submit" name="register" id="register" value="Register">
</form><br />
<form name="news" method="post" action="news.php">
  <input type="submit" name="news" id="news" value="News">
</form>
<?php
}
?>

The First Thing We Do, Is Starting The Session
And we require the database file, so that the code can connect to the database

After that, we check if the 'user' session is active. If it is, the code will show u a text that says Welcome 'your name' and a logout button.

Then we check if the 'admin' session is active.
if it is, the code will write Welcome 'your name'.
But it will also write that you are logged in as an admin, and show you a logout button.

Then we check if there is a session at all wink.gif
If there isn't, we'll just show the login form and some buttons.


Now Lets Begin with "Login2.php"

CODE
<?php
session_start();
require_once 'database.php';

    # make  a variable out of the username that was posted in the index-page.
    $username = $_POST['user'];
    # I am not sure what this thing makes.. but it has something with safety to do.
    $escaped_username = mysql_real_escape_string($username);
    # make a md5 password.
    $md5_password = md5($_POST['pass']);
    
    $queryN = mysql_query("select * from user where username = '".$username."' and password = '".$md5_password."' AND
level='1'");#This variable will check if the user is a level 1 user (Normal User)
    $queryA = mysql_query("select * from user where username = '".$username."' and password = '".$md5_password."' AND
level='9'");#This variable will check if the user is a level 9 user (Admin User)
    
            
    if(mysql_num_rows($queryN) == 1)
    {
        $resultN = mysql_fetch_assoc($queryN);                    
$_SESSION['user'] = $_POST['user'];    
header("location:Index.php");      
}

elseif(mysql_num_rows($queryA) == 1)
    {
        $resultA = mysql_fetch_assoc($queryA);                    
$_SESSION['admin'] = $_POST['user'];    
header("location:index.php");      
}

else{
echo "Wrong Username or Password";
}
?>
<form name="back" method="post" action="login.php">
<input type="submit" name="back" id="back" value="Back to Home">


The First Thing We Do, Is Starting The Session
And we require the database file, so that the code can connect to the database
then we changing the $_POST['user'] into a variable.
Then we add some safety stuff.
Then the code will check, if the password, username was correct, and if the user level (admin or normal user) is level 1 (normal user).
if it is, the session 'user' will be created.

then, it will check if the level is level 9.
if it is, the session 'admin' will be created.

else if the username or password was incorrect, the code will write that the password or username was wrong, and show a "back to home" button.


I require the database.phpin both files. Here it is:
CODE
<?
$con = mysql_connect('localhost','mysql_username','mysql_password');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db('databasename');
?>


the first thing we do here, is:
Open a connection to the mysql server.
If the connection failed, the code will write an error
then, we select the database we want to use.


And Ofc we want the logout.php script:

CODE
<?php
session_start();#This will start the session
session_unset();   #Session_unset and Session_destroy
session_destroy();#Will remove all sessions.
header("location:index.php");#This code will sen du back to the index page
?>


the first thing we do, is:
start the session.
Then, we remove all session data, with session_unset and session_destroy.
Then, we make the code send the user back to the "index page"


Tell me if i missed something. I would also like to know if you liked this tutorial =)
And if you find any errors, tell me, and ill fix them =)

//Feelay

 

 

 


Reply

vujsa
Thanks for the tutorial. I'm sure that many people will find it useful.
A lot of comments in the code is very helpful. Sometimes that is more important than the tutorial itself.

I also find it useful if you describe the code that you post so that users will have something to refer to if they have a question.

vujsa

Reply

Miles
Nice tutorial. I usually write my own user systems, but if I'm short on time for a project, I'll probably adapt it in. Also, good commenting, that should help those learning php. One thing though, in the code for login2.php, you have a lot of whitespace. Might want to remove some of it.

Reply

Feelay
QUOTE(vujsa @ Jan 13 2008, 05:26 PM) *
I also find it useful if you describe the code that you post so that users will have something to refer to if they have a question.

vujsa


How do you mean by that last thing u said =? Describing the code? Do u mean something else than comments =?


QUOTE(Miles)
One thing though, in the code for login2.php, you have a lot of whitespace. Might want to remove some of it.


Now I have edited it.. Isn't it harder to read now =?

Thanks for the good replies =)

Reply

vujsa
QUOTE(Feelay @ Jan 13 2008, 01:50 PM) *
How do you mean by that last thing u said =? Describing the code? Do u mean something else than comments =?
Now I have edited it.. Isn't it harder to read now =?

Thanks for the good replies =)

I was referring to tutorials writing. Yours flows well and is easy enough to read but for more complex scripts, I find it helps to "translate" your code into English.

For example with your logout.php script:
QUOTE
CODE
<?php
session_start();
session_unset();
session_destroy();
header("location:index.php");
?>


If you preceded that with an explanation of the script...

Then we finish with our logout script:
First we open the user's current session with session_start().
We follow this by clearing all of the session variables that might have been set with session_unset().
Then to get rid of the session, we run the session_destroy() function.
Finally, we return the user to the index page using the header() function. It should be noted that the location parameter is not the only use of this function.


This way the reader can learn more about PHP than just copying a login script!

vujsa

 

 

 


Reply

Feelay
Ok. I didn't do exactly as u said.. instead, i added some more comments. Because then the user knows exactly what the commets is about, and I find it easier to do it that way..

Reply

turbopowerdmaxsteel
If I were you, I would remove the comments and describe each of the steps in layman's terms. That way you would get a lot more credits for your posts. Remember, the text inside the Code block does not fetch you any credit. Also, not everyone feels comfortable with commented descriptions.

Reply

Feelay
Happy now wink.gif =?

Reply

rockarolla
Sorry if it sounds incompetent but I don't the code behind require_once clause(I don't know what is it - it looks to be a mySQL function, but it isn't standard function, probably a user defined?).

Otherwise the code is quite neat - and pretty easy to follow.

If there are questions about the md5() function, a detailed desciption can be found here e.g.

http://www.w3schools.com/php/func_string_md5.asp


Reply

Feelay
I use require_once when I want to "include" something, but not exactly include it.. it is hard to explain.. I just know, that when you want to check a database file, or something like it, it is better to use the require function instead of include. include is better to use when you want to include a part of a page.

BTW: Sorry for the late answere tongue.gif

Reply

Latest Entries

kanade
QUOTE(Feelay @ Apr 22 2008, 08:17 PM) *
I use require_once when I want to "include" something, but not exactly include it.. it is hard to explain.. I just know, that when you want to check a database file, or something like it, it is better to use the require function instead of include. include is better to use when you want to include a part of a page.

BTW: Sorry for the late answere :P


Nice toutorial, this is verry usefull script and lot of new things to learn. But it will be more usefull if any one explains the script in more details and with some more comments for the script.

Reply

maxmumford
Hi =]

nice tutorial - its so useful having so many notes too.

any idea why im getting this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in login2.php on line 18

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in login2.php on line 25
Wrong Username or Password

?

Thanks smile.gif
Max

Reply

mafialeg
Nice, really like it happy.gif

Reply

Ddraiggoch06
Very nice tutorial. Easy to understand and written for the beginner (like me smile.gif). I'll try this out sometime, I've always wanted to have a go at a login system. Simple but effective wink.gif

Reply

Feelay
It was a really long time ago I created a php-script tongue.gif So I have forgotten most of the codes tongue.gif I really have to make a comeback tongue.gif but I am starting to find it kinda boring tongue.gif

Thanks for the comments and tips smile.gif

//Feelay

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Pages: 1, 2
Recent Queries:-
  1. free secure php login scripts - 9.90 hr back. (1)
  2. simple login code - 79.77 hr back. (1)
  3. login id where it is stored - 197.79 hr back. (1)
Similar Topics

Keywords : simple, login, script, simple, secure, login, script

  1. Creating A Php Login Script
    A thorough look at the process behind it (3)
  2. A Simple Register Script
    This Is a Very Simple Register-Script (3)
    Some time ago, i made a login-script. But how do you use a login-script, if you can't register.
    So this morning, I decided to make a register-script.. What you should already know: The php
    basics and a little more. How to use php and mysql together. The HTML basics (to make the forms).
    The first thing we should do, is creating the database tables. Here is the code: CODE CREATE
    TABLE `user` (   `id` int(4) unsigned NOT NULL auto_increment,
      `username` varchar(32) NOT NULL,   `password` varchar(32)....
  3. Attack Script In Php
    This is a funny attack script that i made (5)
    Hey! I am going to share an attack script that i made for some time ago. I made it, as a test
    for my game.. And ofc, you can use it for your game to. It is still version 1.0. But I want you to
    learn something from it /wink.gif" style="vertical-align:middle" emoid=";)" border="0"
    alt="wink.gif" /> This is my second tutorial here, and I will try to make it better than my first
    one /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> Here is
    the SQL File. CODE CREATE TABLE `characterss` (   `health` int(2....
  4. Simple User Validation Script
    (5)
    This tutorial will show you how to create a simple user validation script with PHP. We will need
    two files: "protect.php" and "login.php". The protect file is not meant to be viewed by itself. In
    order to protect a page, you need to include that file by using PHP code like the following: CODE
    include("protect.php"); Keep in mind that this needs to be in between your
    tags. This bit of code uses the include function. It is a handy function that reads all the
    information contained in one file and temporarily adds it to another. For example, this c....
  5. PHP Tutorial: Form Verification And Simple Validation
    A One Page script for PHP form verification. (12)
    Having used various means of verifying HTML forms I believe that this method of verifying a form
    to be the best mostly because it does everything on one page. It presents the form on one page and
    then when the submit button is pressed, if all the required fields are not filled out then it will
    present the form again with all the fields intact and in red lettering will point out the fields
    that are required to be filled out in red. It is not possible to click submit using this method even
    if the user has turned JavaScript off. While it is possible to use javascript to ....
  6. PHP Tutorial: Menu Or Sidebar Script For CMS101
    and other applications as well (6)
    A Php Menu-builder Tutorial This Sidebar Menu-builder code and the php scripts are adapted from
    a Tutorial on the Astahost.com Forum titled : CMS101 - Content Management System Design .
    Since the original tutorial's author (vujsa) did such a marvellous job of describing the system
    in the original Topic posting, I will not attempt to explain it here, rather, I invite you to have a
    look at his Topic and learn from it. The Basic tutorial provided coding for developing a table-based
    web-site template which used php includes and embedded data to create a &....
  7. Creating Your Own Image Gallery With Php
    A Guideline, Not A Complete Script (3)
    Recently a member asked how to create a photo gallery using his various directories filled with
    image files. Here is an overview of the steps and fuctions needed to do this. Assuming that the
    following directories exists and are full of image files: www.testsite.web/photos/gallery1/
    www.testsite.web/photos/gallery2/ www.testsite.web/pictures/album1/ In order to get the contents
    for a specific gallery you'll need to let the script know which one to look in. You'll need
    to use a link that carries the arguments needed to locate the right photos. www.testsite.we....
  8. Secure Php Coding
    tips and hints for more secure PHP'ing (10)
    Secure PHP coding Today, PHP is a very common and very popular scripting language that is used
    by many people over the world. However, many php scripts that they make are vulnerable to
    'hacks' by leaving some security holes open. This article will explain how someone can abuse
    your script and can alter your site/files, but also (even more important), this article will tell
    you how to PREVENT your site from being hacked and how to spot and fix those security holes.
    Contents: - Chapter 1 : To serve or not to serve - Chapter 2 : MySql, friend or foe? - Conclu....
  9. PHP: Writing A Generic Login And Register Script
    (14)
    Now there are basically 3 functions that a user management system provides: login, register, and
    protection. A user management system can do more than this but that is all that this tutorial will
    be covering. I will try to explain what I am doing as I go along but to fully understand what is
    happening you should have a basic knowledge of PHP, SQL, and HTML. This tutorial assumes you are
    using MySQL, adjust accordingly for a different DBMS. First off lets define the database table
    where our users will be stored. Using phpMyAdmin run this statement to create our table....

    1. Looking for simple, login, script, simple, secure, login, script

Searching Video's for simple, login, script, simple, secure, login, script
advertisement




Very Simple Login-script - This is a very simple and secure login-script



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE