<?php


	ob_start();			// <-------- ob_start must be on top!!!
	session_start();	// <-------- session_start must be on second top!!!
	
	error_reporting(E_ALL);
	ini_set("display_errors", 1);

	include("lock/login_config.php");
	$error = "";
			
	if($_SERVER["REQUEST_METHOD"] == "POST") 
	{
			//echo "<font face='Verdana' style='font-size: 9pt' color=green> Login Form submited successfully !! <br> </font> ";
		
		//----- Test connection to database -----//
			if(!$db){  
				die('Could not connect: '.mysqli_connect_error());  
			} 
			else 
			{  
				//echo "<font face='Verdana' style='font-size: 9pt' color=green> Connected to database successfully! <br> </font> ";
			} 		
			
		//----- username and password sent from form -----//
			$myusername = $_POST['username'];
			$mypassword = $_POST['password']; 
			//echo "myusername =" . $myusername. "<br>" ;
			//echo "mypassword =" . $mypassword. "<br>" ;
	
		//----- Get data from database -----//
			$sql = "SELECT * FROM table03_engineer_password WHERE username='$myusername' and password='$mypassword'";
			$result = mysqli_query($db,$sql);
			
			$count = mysqli_num_rows($result);		
			//echo "count =" . $count. "<br>" ;		
		
			if ($count > 0) 
			{
				while($row = mysqli_fetch_assoc($result)) 
				{				
					$id = $row['id'];
					//echo "id =" . $id. "<br>" ;	
				}
			}
			else
			{
				//echo"<font face='Verdana' style='font-size: 9pt' color=red> <br> error! can't find data from database! <br> </font> ";			
			}
				

		//----- If result matched $myusername and $mypassword, (table row must be 1 row) , go to the index.php -----//
			if($count==1) 
			{
				//echo "Bingo!!! <br>" ;
				$_SESSION['login_user'] = $myusername;
				//echo "_session1=" . $_SESSION['login_user'] . "<br>" ;	
				header("location: index.php");			
				exit();
			}
			else 
			{
				$error = "Your Login Name or Password is invalid";
			}
	}

?>
<html>
 	<head>
		<title>M6027_CPO LOGIN</title>
		<meta name="viewport" content="width=device-width,initial-scale=1"/>		
		<meta http-equiv="Content-Language" content="en">
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">	
  
	<style type = "text/css">
		body {font-family:Arial, Helvetica, sans-serif; font-size:14px;}
		label {width:100px; font-size:14px; font-weight:bold; color:darkgrey; }

		.main_contain {width:300px; border: solid 1px #333333;  margin-top: 30px; display: table; } 
		
		.login_title {width : 100%; height:35px; background-color: #448E17; text-align: center; display: table;}
		.login_title_cell { color:#FFFFFF;  font-weight:bold; display: table-cell; vertical-align: middle;}
		
		.login_contain {background-color: white; padding:30px 30px 20px 30px;}
		
		.box {border:#666666 solid 1px;}

		/*------------------------------------------------------------------------------*/
		/*			RESPONSIVE LAYOUTS PART A					   		 				*/
		/*------------------------------------------------------------------------------*/
		
		/******* 5.Small Tablets & Larger Phones width of 685px *******/
		/******* 5.Michael's Huaiwei Landscape width of 601px   *******/
		/******* 6.iPhone (Landscape) width of 568px *******/
		/******* 7.Other Hand Phones (Landscape) width of 480px *******/
		/******* 8.iPhone (Portrait) & Other Hand Phones width of 320px *******/
		/******* 8.Michael's Huaiwei Portrait width of 360px  *****************/
		@media only screen and (min-width: 300px) and (max-width: 700px) 
		{
			.login_contain {padding:20px 20px 10px 20px;}
			.box {width : 100px;}		
		}		
		
	</style>
  
   </head>
   
   <body>
	
      <div align = "center" >
         <div class="main_contain"  >
            <div class="login_title" >
				 <div class="login_title_cell">Login </div>
			</div>
				
            <div class="login_contain">
               <form action = "" method = "post">
                  <label>User Name : <label><input type = "text" name = "username" class = "box"><br><br> 
                  <label>Password  :&nbsp;&nbsp;&nbsp;<label><input type = "password" name = "password" class = "box"><br><br> 
                   <div align = "center" ><input type = "submit" value = " Submit "><br></div>
               </form>
               <div style = "font-size:11px; color:#cc0000; margin-top:5px"><?php echo $error; ?></div>
            </div>
         </div>
			
      </div>

   </body>
</html>