PHP Simple Sign-in Tutorial

Furqan Aziz | 17 Oct 2013 | Tutorials

PHP Simple Sign-in Tutorial
Social Box
This tutorial is about simple sign-in php code for beginners. You can also protect pages from unauthorized persons.
  1. You will learn to make one database table, one form , checking php page, user session page, config page and secret page.
  2. First create a simple form using below code or you can use your own sign in form.
    • <form  method="post" action="CheckLogin.php">
      <input type="email" name="inputEmail"  id="inputEmail" placeholder="Email">
      <input type="password" name="inputPassword" id="inputPassword" placeholder="Password" maxlength="15" minlength="6"  >
      <button type="submit" >Sign in</button>
      </form>
  3. Now you will run below SQL Code to make a simple table in your database.
    • CREATE TABLE IF NOT EXISTS `members` (
      `id` int(4) NOT NULL AUTO_INCREMENT,
      `name` varchar(65) NOT NULL DEFAULT '',
      `email` varchar(65) NOT NULL DEFAULT '',
      `password` varchar(65) NOT NULL DEFAULT '',
      `Type` varchar(10) NOT NULL,
      PRIMARY KEY (`id`)
      ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  4. Now its time to connect to the database, so we will make database connection file config.php
    • <?php
      $con = mysql_connect("localhost","root","yourpaswword");
      if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
      mysql_select_db("YourDbName", $con);
      ?>
  5. Now when form submitted it will go to CheckLogin.php page and here is code for page.
    • <?php
      // Start Session because we will save some values to session varaible.
      session_start();
      ob_start();
      // include config file for php connection
      	include("config.php");
      // memebers table name
      $tbl_name="members";
      // Define $myusername and $mypassword
      $myusername=$_POST['inputEmail']; 
      $mypassword=$_POST['inputPassword']; 
      // To protect MySQL injection
      $myusername = stripslashes($myusername);
      $mypassword = stripslashes($mypassword);
      $myusername = mysql_real_escape_string($myusername);
      $mypassword = mysql_real_escape_string($mypassword);
      below query will check username and password exists in system or not
      $sql="SELECT * FROM $tbl_name WHERE email='$myusername' AND password='$mypassword'";
      $result=mysql_query($sql);
      // Mysql_num_row is used for counting table records
      $count=mysql_num_rows($result);
      // If result matched $myusername and $mypassword, table record must be equal to 1 	
      if($count==1)
      {
      	$sql2=mysql_query("SELECT Type,email,name FROM $tbl_name WHERE email='$myusername'");
      	$row=mysql_fetch_row($sql2);
      	$_SESSION[type]=$row[0];
      	$_SESSION[myusername]=$row[1];
      	$_SESSION[name]=$row[2];
      //Depending on type of user we will redirect to various pages		
      	if($row[0] == 'abc')	 { header( "location:http://localhost/abc.php"); 	}
      	else if($row[0] == 'xyz')	 { header( "location:http://localhost/xyz.php"); 	}
      	else if($row[0] == 'Admin')	 { header( "location:http://localhost/admin.php"); 	}
      	else    {   header( "location:http://localhost/index.php");  }
      }
      else
      {
      	header("location:http://localhost/index.php");
      }
      ob_end_flush();
      ?>
  6. Now on user pages like abc.php, xyz.php or admin.php your will write below lines on top of the page to make them protected.
    • <?php
      include('UserSession_xyz.php');
      ?>
  7. Below is code for UserSession_xyz.php. you will also make other pages for other users.
    • <?php
      session_start();
      if($_SESSION[type]!='xyz'){
      header('location:index.php');
      exit();
      }
      include('config.php');
      ?>
Above tutorial is very useful for making websites pages secure and make user Sing-In System. You can also use some features of this tutorials. For more information please post a comment on Blog.
LATEST TUTORIALS
PHP MySQL Simple Delete using PDO in Bootstrap Tutorial
Convert Simple Text To 3D Text using Adobe Fireworks CS6 Tutorial
PHP Simple Update using PDO in Bootstrap Tutorial
PHP Simple Insert using PDO in Bootstrap Tutorial
PHP MySQL Easy CSV Data Insertion in Bootstrap using PDO Tutorial
PHP Easy Sign-in Using PDO Prepared Statement Tutorial
Easy PHP Email With Attachments using Swift Mailer Tutorial
Easy Pagination with PDO in PHP plus Export to Excel Tutorial
PHP PDO to Connect MySQL Database Tutorial
Best CSS Frameworks Collection For Rapid Web Development
PHP-MySQL Ajax Jquery-UI Autocomplete in Bootstrap Tutorial
PHP Simple Sign-in Tutorials
Jquery Raty Usage via PHP
PHP Dynamic Select options Via Ajax
CSV Importer in MySQL
PHP File Load Via Jquery into DIV
© 2014 CreWow Network - All Rights Reserved.