Sample Login Program (PHP)

Sample Login Program (PHP)

Date: 08/18/2009
Program Name: Sample Login Program (PHP)
Description: The purpose of this PHP program is allow a user to login and view the content of a page. If the user does not supply the correct login information, then the program will print out a message stating that the information is incorrect. In addition, The program checks to see if it’s able to connect to the database. If it can not than it will display a message stating that it can not connect to the database.

<?php
  session_start();
  include ('request_processor.php');
  include ('********.php');
  include ('menu.php');
  include ('head.php');
 
  //were going to check of the were able to connect to the database.  If we can not then print a message that we can not
  $result = con_test();
  if (!$result) 
  {
      echo ('Connection status: <b><span style="color:red">error</span></b> <br /> <p>Please check to see if the MySQL database service is on.');
      exit(0);
  }
  else
  {
    if ($_SESSION['login'] == '') //indicating that if the variable is not set than the 
    {  //print the login page
      print ("<table align=\"center\" border=\"0\"><form action=\"login.php\" method=\"post\"><table border=\"0\" align=\"center\">");
      print ("<tr><td>User Name:</td><td><input type=\"text\" size=\"30\" maxlength=\"28\" name=\"Name\" /></td>");
      print ("</tr><tr><td>Password:</td><td><input type=\"text\" size=\"30\" maxlength=\"28\" name=\"pass\" /></td>");
      print ("<tr><td align=\"center\" colspan=\"2\"><input type=\"submit\" value=\"Login\" />");
      print ("<input type=\"button\" value=\"Lost Password\" /></td></tr></table></form></body></html>");
      $_SESSION['login'] = 'check';
      exit(0);
    }
    elseif($_SESSION['login'] == 'check')
    {
      //Will start first by openning a connection to mysql
      $connection = con();
 
      //get the name and password from the login page
      $name = $_POST["Name"];
      $pass = $_POST["pass"];
 
      //Create the query command for MySQL
      $query = "select * from users where UserName = '$name' and password = '$pass'";
 
      $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
 
      if (!mysql_fetch_object($result))
      {
				$_SESSION['login'] = '';
				print ("I am sorry but the username or password does not match!<br /><a href=\"/portfilo/login.php\"><td>Go Back</a>");
				exit(0);
      }
      else
      {
				$_SESSION['login'] = 'ja';
				head();  //Create the html headers
				styles(); //Then the styles
				menu();   //Menu option
				print ("This is a sample programming Work.");
      }
 
      // close connection
      mysql_close($connection);
    }
    elseif ($_SESSION['login'] == 'ja')
    {
      head();  //Create the html headers
      styles(); //Then the styles
      menu();   //Menu option
 
      print ("This is a sample programming Work.");
    }
    // Session cleanup
    session_destroy();
  }
?>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: