- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Simple PHP Login or Signin Script
This tutorial aims to present/show a simple 'log in' or 'sign in' script written in PHP, which you can use on your websites with little modifications. Script is tested before publishing and successfully executed without any error. So, here we go, You need to create two pages on your site with one having a form and other with PHP script. First of all, we will create a simple login form. Paste this code on any web page which you'll like to use for login purpose.
See the code below:
<!-- the " name="" " attrbute used here is mandatory, which have its use in php script --> <form method="POST" action="link of page that have php script" > <input type="text" name="username" /> <input type="text" name="password" /> <input type="submit" value="Log In" /> </form>
Now, We'll write our php script which will do the authentication work using the values submitted by login form. We first need to create a table in our site database with table-name 'login' having this attributes "id (auto increment)", "username", "password". if you didn't created your Site database, Then, go to control panel of your host and create one with your site's name.
(*)Tips:
- Create a separate page for this php script.
- Sql injection (mysql_real_escape_string() function) is used for security purposes such as protecting our script and sensible data from hackers - You can read the whole article about sql injection here.
- The lines with green color in the code below are comments and does not play any role in authentication, they are used to understand code better.
- This is not the advanced version of 'login script' but it's the base on which, you can also try your ideas to enhance it such as making more secure and multifunctional.
<?php /* $con make a connection with database */ $con=mysql_connect("hostname","username","password"); //select database mysql_select_db("database name"); /* Below two commands will store the data in variables came from form input */ $username=$_POST['username']; $password=$_POST['password']; /* below two commands are sql injection which stops extra characters as input */ $user=mysql_real_escape_string($username); $pass=mysql_real_escape_string($password); $query=mysql_query("SELECT * FROM login where username='$user' AND password='$pass' "); $count=mysql_num_rows($query); if($count==1) /* $count checks if username and password are in same row */ { echo "Login Successful"; $hour = time() + 3600; /* $hour sets cookie storage time for 1 hour */ /* setcookie() function sets cookie after login */ setcookie("username", $username, $hour); setcookie("password", $password, $hour); header("location: redirecting page link"); /* header() function redirect user to members page */ } else { echo "Username or password is incorrect"; } ?>
Now, to check if user is already logged in, We use $_COOKIE['username'] for the purposes like redirecting, displaying login or logout at user screen.
see example below:
<?php /* You should make changes in if else loops according to your needs here */ if(isset($_COOKIE['username'])) { echo "You were already logged in ".$_COOKIE['username']."."; /* " $_COOKIE['username'] " will fetch the username from cookie stored on browser if user is already looged on */ include("template_file_address"); //or you can redirect it to another page.... } else { header("location: login.php"); } ?>
Well, We already set the time of 1 hour in cookie itself for expiring but if user wants to 'Log Out' or 'Sign Out' from site earlier then here is the Logout script below. Create a new page with name 'logout.php' and paste this script in it with little modification in 'header()' function and paste the link of 'logout page' in your template or on every page that required login.
<?php /* we are setting the time of cookie destruction in the past to destroy the cookie */ $past = time() - 100; setcookie("username", gone, $past); setcookie("password", gone, $past); header("Location: link of login page or thank u page"); ?>
Hope, this article is easy to understand and helpful,
Thanks for your visit,
Shrikrishna Meena (An IT Student).