04-20-2010, 11:32 AM
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:
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:
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.
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.
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.";
}
?>
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.