Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a simple PHP/MySQL login script
#1
In this tutorial I'll be teaching you how to create a simple PHP login script, it is not very complicated, all this does is just give you an idea of how a login script works. You will need a MySQL database.

Creating the table

The first step is obviously creating the table, so let's get right to it. I'll only use 2 fields, "username" and "pw".

CREATE TABLE tns_users (username VARCHAR(60), pw VARCHAR(60))
Run that SQL query on any of your databases and it will create a table called: tns_users with 2 fields, "username" and "pw".

Creating the user(s)

Now it's time we create our user:

Code:
INSERT INTO tns_users (username, pw) VALUES ('TNS', 'thenopslide')

Run that SQL query on the same database where you ran the first one, you should now have a new user in your table, with TNS as a username and thenopslide as password.

Login Page

Now that the database work is done, it's time we create our HTML/PHP files. Let us start with the Login Page:

Code:
<form action="check.php" method="post">
<table>
<tr>
<td>Username: </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="pw"></td>
</tr>
</table>
</form>

Check.php

Now that we've successfully made a working form, it's time we create the check.php file that will check the username and password.

PHP Code:
<?php
session_start
();
$link mysql_connect("localhost""db_username""db_pass"); // Creates a string that will later be used to connect to the database table. Change the values according to what you have.
if (!$link)
{
die(
'Could not connect: ' mysql_error());
}

mysql_select_db("db"$link); // Chooses table and connects using the string set above.

$username $_POST['username']; // Sets string username to the text entered in the username field.
$pw $_POST['pw']; // Sets string pw to the text entered in the password field.

$uname stripslashes($username); // Removes slashes
$paw stripslashes($pw); // Removes slashes

$sql "SELECT * FROM tns_users where username = '$uname' and pw = '$paw'"// Selects the fields from the table
$result mysql_query($sql) or die ( mysql_error() );

$check 0;

$check mysql_num_rows($result);

if (
$check == 1) {
echo 
"The information you have entered is valid<br>";
echo 
"You have successfuly logged in.";
} else {
echo 
"The information you have entered in invalid<br>";
echo 
"You are not logged in.";
}
?>
Now what you have to do is go to the login.php, run it and enter the username and password added to the database earlier, which are TNS and thenopslide, if you enter them successfuly check.php will tell you that you have logged in, and if not they will tell you the information is invalid.

That's pretty much it, be sure to replace some of the database information with your own.

BEWARE: This script is NOT secure and should NOT be used on a website. The script was just to give you a general idea about how a login script works.
Reply


Messages In This Thread
Creating a simple PHP/MySQL login script - by ndee - 04-20-2010, 11:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 786 07-27-2020, 11:26 PM
Last Post: tk-hassan
  [PHP] Very Basic Login Page BreShiE 17 7,051 07-11-2013, 05:57 AM
Last Post: 1n9i9c7om ツ
  [help] Improve login script Montana" 1 1,546 03-18-2013, 12:59 PM
Last Post: Haxalot
  Req. Upload PHP script. 3 X P L 0 I T 1 1,203 07-14-2012, 05:19 PM
Last Post: 'Snorlax
  Creating Dynamic images with PHP Gaijin 15 7,449 01-21-2012, 06:17 PM
Last Post: Dube

Forum Jump:


Users browsing this thread: 2 Guest(s)