Tuesday, 15 December 2015

PHP login, logout, user home page navigation

Dear Students,

It is really important to understand the flow of data once a user log to a system and use various links in the user home pages and finally log out of the sytem.
Step1 : login using home.php
Step2: Navigate to faculty home or student home or admin home based on the privileges set in the database
Step3: logout from the system.



Flow of program

home.php [type username and password] -->check.php[check for authentication and SQL Injection threat] ---> navigate to either facultyhome.php or adminhome.php or studenthome.php or home.php ->logout.php[when you click logout link]

NB: create a Database 'SNIT' and copy paste the below given sql in the SQL tab of phpmyadmin;


CREATE TABLE IF NOT EXISTS `login` (
  `username` text NOT NULL,
  `password` text NOT NULL,
  `type` text NOT NULL,
  `status` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `login`
--

INSERT INTO `login` (`username`, `password`, `type`, `status`) VALUES
('admin', 'adm', 'admin', '1'),
('faculty', 'fac', 'faculty', '1'),
('student', 'stud', 'student', '1');

Check the system with these username, password pair 

For Admin  (admin,adm)
For Faculty (faculty,fac)
For Student  (student,stud)

home.php

 <html>
<!--
Authored by : Prof. Sajeev J. (sajeevjal@gmail.com)
Date : 16/12/2015
-->
<body>
<form id="form1" name="form1" method="post" action="check.php">
  <a name=#top>
  <table width="81%" border="0"  align="center">
 

    <tr>
      <td colspan="4"><div align="right">
        <label></label>
    <!-- Code to show error message if invalid username and password is typed -->  
     <?php
     $msg="";
    
     if($_GET['msg'])
     $msg=$_GET['msg'];
     if($msg=="Invalid Username or Password" || $msg=="You have not logged yet"|| $msg=="Your are not privilleged for this activity")
     echo "<font color=red >".$msg."</font>";
    
     ?></div></td>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right">Username</div></td>
      <td><div align="right">
        <input type="text" name="username" />
      </div></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right">Password</div></td>
      <td><div align="right">
        <input type="password" name="password" />
      </div></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><div align="right">
        <input type="submit" name="Submit" value="Sign in" />
        <input type="reset" name="Submit2" value="Clear" />
      
      </div></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

check.php

<!--
Authored by : Prof. Sajeev J. (sajeevjal@gmail.com)
Date : 16/12/2015
-->
<?php

$username=$_POST['username'];
$password=$_POST['password'];

$count1=0; $count2=0;
$count1=substr_count($username, "'");
$count2=substr_count($password, "'");
$err=0;
if ($count1>0 || $count2>0)
    $err=1;

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("SNIT", $con);
$result = mysql_query("SELECT * from login where username='".$username."' and password='".$password."' and status='1'");
$flag=0;
while($row = mysql_fetch_array($result))
  {
 
  $flag=1;
  $type=$row['type'];
 
    session_start();
    $_SESSION['user'] = $type; // store session data
    $_SESSION['username'] = $username;



  }
 
 
  echo $flag;
  echo $type;
 
  if($err>0)
      echo "<script>location.href='home.php?msg=Invalid Username or Password'</script>";
    else if($flag==1 && $type=="admin")
  echo "<script>location.href='adminhome.php'</script>";
  else if($flag==1 && $type=="student")
  echo "<script>location.href='studenthome.php'</script>";
  else if($flag==1 && $type=="faculty")
  echo "<script>location.href='facultyhome.php'</script>";
 
  else
   echo "<script>location.href='labhome.php?msg=Invalid Username or Password'</script>";
 
mysql_close($con);
?>
adminhome.php
 <html >
<head>
<title>Admin Home</title>
<!--
Authored by : Prof. Sajeev J. (sajeevjal@gmail.com)
Date : 16/12/2015
-->
</head>
<body>
<h1>Admin Home</h1>
<a href=logout.php>Logout</a>
</body>
</html>

facultyhome.php

<html >
<head>
<!--
Authored by : Prof. Sajeev J. (sajeevjal@gmail.com)
Date : 16/12/2015
-->
<title>Faculty Home</title>
</head>
<body>
<h1>Admin Home</h1>
<table border=1>
<tr><td><a href='addstudent.php'>Add Student</a></td><td><a href='addparent.php'>Add Parent</td><td><a href='addmarks.php'>Enter Marks</a></td><td><a href=logout.php>Logout</a></td></tr>
</table>
</body>
</html>



studenthome.php

<html >
<head>
<title>Student Home</title>
<!--
Authored by : Prof. Sajeev J. (sajeevjal@gmail.com)
Date : 16/12/2015
-->
</head>
<body>
<h1>Student Home</h1>
<a href=logout.php>Logout</a>
</body>
</html>

logout.php

<!--
Authored by : Prof. Sajeev J. (sajeevjal@gmail.com)
Date : 16/12/2015
-->
<?php


session_start();
session_destroy();
echo "<script>location.href='home.php?msg=0'</script>";


?>

Thursday, 3 December 2015

File Reading and Tockeniser in PHP

Hi Friends,
Reading a file using PHP is pretty easy. Here is the code which read a text file and tockenize that line into words and displaying them in a formatted table format.

So we need two files. One is the text file (HUPFilestructure.txt) and another PHP file to process the same.

1. HUPFilestructure .txt

HUP HUP HUPA
HUPA HUP TFTKTU
TFTKTU HUP HUPA

2. PHP file (fileprocess.php)

<?php
$myfiles = fopen("HUPFileStructure.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
echo "<table border=1>";
while(!feof($myfiles))
{
  $row= fgets($myfiles); // Read line by line
 
  $tok = strtok($row, " "); // Tockenize that line using space
  echo "<tr>";
  while ($tok !== false)
  {
      echo "<td>$tok</td>";
    $tok = strtok(" ");
  }
  echo "</tr>";
}
echo "</table>";
fclose($myfiles);
?>

Tuesday, 1 December 2015

View registraion details in a table with alternate row color

Dear Friends,
You can display the details in the table with alternate row and text color using this code.

viewadmission.php


<html>
<head>
<title>
Registered Candidates
</title>
<body >
<!--
Coded by Prof. Sajeev Jaladharan
Date : 27.11.2015
email: sajeevjal@gmail.com
-->
<?php



$con = mysql_connect("localhost","root","");
if (!$con)
  {
      die('Could not connect: ' . mysql_error());
  }

mysql_select_db("snit",$con);
$result = mysql_query("SELECT * FROM student order by id");

?>
 <table width="81%" border="0"  align="center">
<tr>
      <td colspan="12" align="center" width='300'><img id=bg src="images/bgacademic.jpg"  /> </td>
     
     
    </tr>
<tr>
      <th>Id</th>
      <th>Name</th>
      <th>City</th>
      <th>State</th>     
      <th>Email</th>
      <th>LPhone</th>
      <th>Mobile</th>
      <th>Qualification</th>
      <th>Subject</th>
      <th>College</th>
     
    </tr>

<tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>

<?php
$i=-1;
while($row = mysql_fetch_array($result))
  {
    if($i==1)
    {
        $clr="#FFFFFF";       
        $tclr="#000000";
    }
    else
    {
        $clr="#666666";
        $tclr="#FFFFFF";
    }
    $i=$i * -1;

    echo "<tr>";
      echo "<td  bgcolor='$clr'><font color='$tclr'>".$row['id'] . "</font></td> ";
    echo "<td  bgcolor='$clr'><font color='$tclr'>".$row['Name'] . "</font></td> ";
    echo "<td  bgcolor='$clr'><font color='$tclr'>".$row['City'] . "</font></td> ";
      echo "<td bgcolor='$clr'><font color='$tclr'>".$row['State'] . "</font></td> ";
    echo "<td bgcolor='$clr'><font color='$tclr'>".$row['Email'] . "</font></td> ";
    echo "<td  bgcolor='$clr'><font color='$tclr'>".$row['Land'] . "</font></td> ";
      echo "<td bgcolor='$clr'><font color='$tclr'>".$row['Mobile1'] . "</font></td> ";
      echo "<td bgcolor='$clr'><font color='$tclr'>".$row['Qualification'] . "</font></td> ";
    echo "<td  bgcolor='$clr'><font color='$tclr'>".$row['Subject'] . "</font></td> ";
      echo "<td bgcolor='$clr'><font color='$tclr'>".$row['College'] . "</font></td> ";
   
    echo "</tr>";
  }

mysql_close($con);
?>


   
  
  </table>
  <div class="info" align="center">
    <h2>&nbsp;</h2>
</div>

<ul><p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

</body>
</html>