<?php 
	if(isset($_POST['username'])){
		require 'connection.php';
		extract($_POST);
		 // prevent sql injection by using  "mysql_real_escape_string()"
        // $password = mysqli_real_escape_string($link ,$_POST['userpass']);

		// $qry = "SELECT * FROM userreg where username='$username' and userpass='$userpass'";
$qry = "SELECT * FROM userreg where username='$username'";
		$run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
		$data = mysqli_fetch_array($run);
		$count = mysqli_num_rows($run);
if($count==1){


$qry = "SELECT * FROM userreg where username='$username' and userpass='$userpass'";
		$run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
		$data = mysqli_fetch_array($run);
		$count = mysqli_num_rows($run);

$Query_username = $data['username'];
$Query_pswd = $data['userpass'];
$userName = $_POST['username'];
$pswd = $_POST['userpass'];

		 // if($count==1){
if($Query_pswd==$pswd){
session_start();
			$_SESSION['userId'] = $data['userid'];
			$_SESSION['userName'] = $data['username'];
			$uid= $_SESSION['userId'];
			$qry = "SELECT * FROM userreg  where userid = '$uid'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   $row= mysqli_fetch_array($run);
   $uname = $row['username'];
date_default_timezone_set("Asia/Karachi");
$uid= $data['userid'];
if($uid==2){
		header('Location: dashboard.php');

}else{


if($uid==1 ){
		header('Location: ..//apfs/index.php');

}

else{
		 header('Location: dashboard.php');
		 // header('Location: lastkhopri.php');
}
//if(($userName == $Query_username) && ($pswd == $Query_pswd)) {
//session_start();
		//$_SESSION['userId'] = $data['userid'];
		//	$_SESSION['userName'] = $data['username'];
		//header('Location: dashboard.php');
 // header('Location: dashboard.php');
		}

}

else {
header('Location: index.php?msg=Wrong Email or Password');


  //$message = "userName or UserPassword incorrect.\\nTry again.";
//  echo "<script type='text/javascript'>alert('$message');</script>";




 // header('Location: index.php');
}
		

} 
else
{

header('Location: index.php');
}
}
else{
		//}
		//else{

		//	 echo "wrong email or password";
		header('Location: index.php');
		//}
	
}
 ?>




