I'm working on a login screen for a College project. Right now I have these two files.
index.php
<html>
<head>
<meta charset = 'UTF-8'>
<link rel="shortcut icon" href="images/favicon.ico"/>
<title>Sistema de Estágios - UFMS - Login</title>
<link href = "css/bootstrap.css" rel = "stylesheet" >
<link href = "css/index.css" rel = "stylesheet" >
<script src="js/jquery-1.11.1.min.js"></script>
<?php
session_start(); // start session
if(isset($_SESSION["sessioname"]))
{
if($_SESSION["session_time"] >= time()) //time hasn't expired
{
$_SESSION["session_time"] = time() + 60;
header("Location:users.php"); /* Redirect browser */
exit();
}
}
?>
<script type="text/javascript">
$(document).ready(function()
{
$("input").blur(function() // This makes the container's border turn red when it is empty
{
if($(this).val() == "")
{
$(this).css({"border" : "1px solid #F00"});
}
});
$("#botao").click(function()
{
var cont = 0;
$("#form input").each(function()
{
if($(this).val() == "")
{
$(this).css({"border" : "1px solid #F00"});
cont++;
}
});
if(cont == 0)
{
$("#form").submit();
}
});
});
</script>
</head>
<body>
<center>
<center>
<div class = "container">
<div class = "principal">
<form id="form" name="form" method="post" action="entra.php">
<p>
<label for="a">Nome de Usuário:</label>
<input id="a" type ="text" name="username" class="form-control"/><br/>
<label id="name_null" hidden="hidden">O campo deve ser preenchido</label>
</p>
<p>
<label for="b">Password:</label>
<input id="b" type="password" name="password" class="form-control"/><br/>
<label id="pass_null" hidden="hidden">O campo deve ser preenchido</label>
</p>
<buttom id="botao" name="Entrar" value="login" class="btn btn-primary" style="width: 100%;">Login</buttom>
</form>
<label> <a href="register.php"><button class="btn">Cadastre-se</button></a> </label>
</div>
</div>
</center>
</center>
</body>
entra.php
<html>
<head>
<script src="js/jquery-1.11.1.min.js"></script>
</head>
<?php
require_once "config.php"; // include conection to database
$mysqli = new mysqli("localhost", "root", "", "sistema");
// verify if there is a person with the recived name
$Tipo = $_POST['tipo'];
$user_info = mysqli_query($mysqli,"SELECT * FROM users WHERE username='".addslashes($_POST['username'])."'");
if(mysqli_num_rows($user_info) != 0)
{
$result = mysqli_fetch_array($user_info); // put the informations in an array
if($result['password'] == sha1($_POST['password']))// if the password matches
{
session_start(); // começa a seesion
header("Cache-control: private");
$_SESSION["sessioname"] = $_POST['username'];
$_SESSION["auto"] = $result["Tipo"];
$_SESSION["id"]= $result["id"];
$_SESSION["session_time"] = time() + 60;// expiration timne
header("Location: users.php");
die();
}
else
{ // else show an alert
?>
<script type="text/javascript">
alert("Senha incorreta");
</script>
<?php
header("Location: index.php");
die();
}
}
header("Location: index.php");
?>
I'm looking for a way to make the login actions happen on index.php instead of entra.php.
I'm also looking for a better way to manage the session expire time. Something like a global variable so I don't have to change it on every single file whenever I want to change it for tests.
I'm pretty new with PHP so I would love to receive some help from you guys.