I have been coding for 2 years now, so this tutorial probably needs someone with a basic knowlegde of PHP, and some function.
Things we will need
- Mysql Access
- Server or get one free www.wamp.com
- Some basic PHP knowledge.
- PHP 4 or later installed
- Login.php
Functions We will Use
- http://www.w3schools.com/php/php_mysql_insert.asp
- http://ca3.php.net/mysql_query
- http://ca3.php.net/manual/en/function.md5.php
- http://ca3.php.net/session_start
- http://ca3.php.net/manual/en/function.trim.php
- http://ca3.php.net/manual/en/ref.session.php
- http://ca3.php.net/manual/en/function.empty.php
- http://ca3.php.net/manual/en/function.mysql-connect.php
Step1.
To create our login system we will need 1 mysql table called "users". So lets begin...
// The mysql_query() function runs a MYSQL script
mysql_query("CREATE TABLE users(
'id' INT(11) UNSIGNED AUTO_INCREMENT,
'username' VARCHAr(225) NOT NULL,
'password' VARCHAr(225) NOT NULL,
UNIQUE KEY(id)")or die(mysql_error());
?>
// What we just did basically was create a table in our DATABASE (holds data), and we gave it the columns for a login system. You can add alot more to it, but this is short and simple.
Step2.
Now we need a test user for the login system, so we run another QUERY, this Statement is called INSERT. (Look at the functions list i showed you)
// Username: Demo Password: 12345
$query = mysql_query("INSERT INTO users (username,password) VALUES ('Demo','12345')") or die("LOL, didnt work");
// You can edit the information to your own likes.
?>
Step3.
The next step is our LOGIN.PHP page I told you to create earlier in this Tutorial. If you did this then insert the following code in it...
Step 4.
The final step is to validate the login once the user attempts to login.
THIS CODE GOES ON TOP OF THE FORM!!!
// Hello, this is Gaming Warrior walking you through the rest of the tutorial
session_start(); // we are going to use sessions, not cookies this time.
// Connect to the data
$dbhost = ''; // Localhost default
$dhname = '';
$dbuser = '';
$dbname = '';
mysql_connect($dbhost,$dbuser,$dbpass) or die("Website is Offline");
mysql_select_db($dbname);
// Now we need to set the variables
$form = $_POST['Submit'];
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if($_SESSION['is_online']==false){ // Check if the user is online, I will explain this later
if(isset($form)){ // Checks if the user has submitted the form
if(empty($username)) || (empty($password)){ // if the user has filled in the fields
echo 'Please fill in all the fields';
}
else{
$username = mysql_real_escape_string($username); // Prevent SQL injections
$password = mysql_real_escape_string(md5($password)); // Prevent SQL injections, and HASH the password
$check = mysql_query("SELECT * FROM users WHERE username = '$username' && password ='$password'")or die(mysql_error()); //this function selects the data from the mysql, with the username and password given
if(mysql_num_rows($check)==0){ // Check how many rows has that username and password (if 0 die) (if 1 success) //
echo 'Wrong Username/Password';
}
else{
$_SESSION['is_online'] = true; // This is a session, you can read about it on the links I gave you, this session will be a data that will be on every page.
$_SESSION['username'] = stripslashes($username); // This is the username we put in the session, to retrieve the online user's information
die("SUCCESS, you are now logged in ".$_SESSION['username']);
}
}
}
else{
die("You are already logged in"); // Put custom message for users that are already logged in
}
}
?>
To show the username just do
session_start(); // You need this on every page to get the user session
echo 'Hello '.$_SESSION['username'] . ' You are logged in';
?>
Well everyone that was it, I don't expect you to understand without reading those links, and going through the basics, but this is a little preview on how its done. This was done very quickly, and its for learning purpose, not for the WWW. Becareful, and read about security, and how to protect yourself against harmful scripts.
I will also post Registration, and V2 of this. Maybe it will be a video tutorial you never know.
thankyou :p