Support Forums

Full Version: Creating a simple PHP/MySQL login script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.
*cough*Injectable*cough*

Otherwise, nice tutorial. Have a look at UserCake by the way.
Obviously injectable.

Like I said at the end of the tutorial, the script is NOT secure and should NOT be used on a website.

Thanks for reading.
Nice share great job
Thanks. I'm not very good with MySQL databases yet (and their integration with PHP), and this helped me out a bit with them.
I use something like this, but my script is way more secure. Great guide mate.
I'll use this to start my own CMS, Although I will not use this exact code, I will use parts of this.
Thank you
Thank's. Simple but Usefull Oui
Always good to teach good security habits even for beginners. As a matter of fact...especially for beginners.

Instead of stripslashes I recommend mysql_real_escape_string().

http://us.php.net/manual/en/function.mys...string.php
very nice tutorial mate. thanks for sharing.
Pages: 1 2