Login is a very important part of any website, from securing contents from prying eyes to tracking visitors on your website. This tutorial is a simple login form for a website.
Firstly you create "login.php" like this
Code:
if(isset($_POST['submit']))
{
$name = mysql_real_escape_string($_POST['username']);
//get all users in the database with that username and password.
<?php
session_start();
session_start();
// This starts the session which is like a cookie,
but it isn't saved on your hdd and is much more secure.
mysql_connect("localhost","DATABASE USER HERE","PASSWORD");
// Connect to the MySQL server
mysql_select_db("login");
// Select your Database
if(isset($_SESSION['loggedin']))
{
die("You are already logged in!");
{
die("You are already logged in!");
// That bit of code checks if you are logged in or not,
and if you are logged, you can't log in again!
}
if(isset($_POST['submit']))
{
$name = mysql_real_escape_string($_POST['username']);
// The function mysql_real_escape_string() stops hackers!
$pass = mysql_real_escape_string($_POST['password']);
$mysql = mysql_query("SELECT * FROM users WHERE name = '{$name}'
AND password = '{$pass}'");
//get all users in the database with that username and password.
if(mysql_num_rows($mysql) < 1)
{
die("Password was probably incorrect!");
}
{
die("Password was probably incorrect!");
}
//check number of rows the MySQL query was less than 1,
so if it couldn't find a row,
the password is incorrect or the user doesn't exist!
$_SESSION['loggedin'] = "YES";
// Set it so the user is logged in!
$_SESSION['name'] = $name;
// Make it so the username can be called by $_SESSION['name']
die("You are now logged in!");
// it doesn't show the login form after you are logged in!
}
echo "<form type='login.php' method='POST'>
Username: <br>
<input type='text' name='username'><br>
Password: <br>
<input type='password' name='password'><br>
<input type='submit' name='submit' value='Login'>
</form>";
<input type='text' name='username'><br>
Password: <br>
<input type='password' name='password'><br>
<input type='submit' name='submit' value='Login'>
</form>";
//the form to enter your password and username to login.
?>
After you have put that code, run this query on your database:
Code:
And then add all of the users you wish to the table!
--------------
To integrate this within your site, you can do the following at the top of your site.
This Code Like Lock.php or This Code Using to Lock Your Webpages
Code:
If you want display your user name on your site use this code:
Code
Logout.php:
Code
Post A Comment:
0 comments so far,add yours