<?php
session_start();
require('45789417/ref4780.php');
if (isset($_SESSION['userid'])) {
  echo "<script language='Javascript'> location.href='home.php';</script>";
    exit();
  }; 
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Historia Clínica, Clínica Los Robles</title>
<!--- css style GENERAL -->
<link rel="stylesheet" type="text/css" href="css/general.css">
<script type="text/javascript" language="javascript" src="js/general.js"></script>
<!-- boostrap -->
<link rel="stylesheet" href="dist/bootstrap/css/bootstrap.min.css">
<script src="dist/bootstrap/js/bootstrap.min.js"></script>
<link rel="shortcut icon" type="image/png"  href="images/favicon.png" />
</head>
<body>

<div align="center"><br><br><h3>Habilitado solo para consultas</h3><br>
		<form action='login.php' method='post' class="form-horizontal" role="form" >
			<div id="login"><!-- BLOQUE CENTRAL -->
				<div id="wrapper" >
					<div id="tb_col"><p><img src="images/logo_login.png"></p></div>
					<div id="tb_col" style="width:10px"></div>
					<div id="tb_col" style="text-align: right">
						<form action="home.php">
							<p><input type="text" name="user" class="form-control input-sm"  placeholder="Usuario..."></p>
							<input type="password" name="password" class="form-control input-sm" placeholder="Contraseña...">
							<input name='login' type='submit' class="btn btn-default btn-info btn-sm"   value='Entrar' >	<br /><br />
						</form> 
					</div>
				</div>
			<div><!-- END BLOQUE CENTRAL -->
		</form>
<?php
//función verificar_login consulta a bd si usuario ingresado es correcto o no.
function verificar_login($user,$pass3,&$result)
    {	
        $con = new mysqli(DB_SERVER,DB_USER,DB_PASS,DB_NAME) or  die('Could not connect: 4444'); 
	  mysqli_select_db($con,DB_NAME) or  die('Could not connect3333: ' . mysqli_error());
        $rec = mysqli_query($con,"SELECT id,username,password,status FROM user WHERE username = '$user' and password = '$pass3' and status = '1'");
        $count = 0;
        while($row = mysqli_fetch_object($rec))
        {
            $count++;
            $result = $row;
        }
        if($count == 1)
        {
            return 1;
        }
        else {
            return 0;
        }
    }
/* condicionales que identificaran el momento en el boton de login es presionado y cuando este sea presionado llamaremos a la función verificar_login() */
if(!isset($_SESSION['userid']))
{
    if(isset($_POST['login']))
    {
	$pass3 = md5("7981".$_POST["password"]);  
  /* $pass3 = md5($_POST["password"]);  */
	if(verificar_login($_POST['user'],$pass3,$result) == 1)
        {
            $_SESSION['userid'] = $result->id;
            session_set_cookie_params(6000);
            $_SESSION['validado'] = true;
            $_SESSION['usuario'] = $strusuario; 
            $iduser_live = $_SESSION['userid'];
             mysqli_select_db($con,DB_NAME) or  die('Could not connect3333: ' . mysqli_error());
            $consulta_mysql= mysqli_query($con,"SELECT id,name,type_user,username FROM user WHERE id = '$iduser_live'");
            if ( $registro = mysqli_fetch_assoc($consulta_mysql) ){            
              $_SESSION["name_live"] = $registro['name'];
              $_SESSION["type_user_live"] =  $registro['type_user'];  
              $_SESSION["id_paci"] = $registro['id'];             
           };       
         echo "<script language='Javascript'> location.href='home.php?login2=1';	</script>"; 
       /*  echo "bienvenido"; */
        }
        else  {
            echo "<div id='alert' class='alert alert-warning'>Su usuario es incorrecto, intente nuevamente.</div>";
        }
  }
?>
<?php
} 
?>
</div>
<?php
      if ($_GET['bye'] == 1) {
     		 echo "<div id='alert' class='alert alert-success'>
             <b>Gracias</b> por usar nuestro sistema.</div> ";
            }
?> 

</body>
</html>
