Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a simple PHP/MySQL login script
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:

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:

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


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:
$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)
'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) {
"The information you have entered is valid<br>";
"You have successfuly logged in.";
} else {
"The information you have entered in invalid<br>";
"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.

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
[Image: logo.png] | The New Sexy Forum ! [Erotic Pics & Videos]
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().
Superman I am here to rescue you.
This is Support Forums not Support PMs.  Do not PM me for support unless it's private and site related.
very nice tutorial mate. thanks for sharing.

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 818 07-27-2020, 11:26 PM
Last Post: tk-hassan
  [PHP] Very Basic Login Page BreShiE 17 7,259 07-11-2013, 05:57 AM
Last Post: 1n9i9c7om ツ
  [help] Improve login script Montana" 1 1,579 03-18-2013, 12:59 PM
Last Post: Haxalot
  Req. Upload PHP script. 3 X P L 0 I T 1 1,239 07-14-2012, 05:19 PM
Last Post: 'Snorlax
  Creating Dynamic images with PHP Gaijin 15 7,615 01-21-2012, 06:17 PM
Last Post: Dube

Forum Jump:

Users browsing this thread: 2 Guest(s)