Archivo auth.php (incluyo en cada archivo que quiero que tenga login)
Código PHP:
<?php
session_start();
if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
header("location: login.php");
exit();
}
?>
Código PHP:
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Fallo la coneccion con el servidor!: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Base de datos no encontrada");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$login = clean($_POST['login']);
$password = clean($_POST['password']);
//Input Validations
if($login == '') {
$errmsg_arr[] = 'Id incorrecto';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password incorrecto.';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}
//Create query
$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
$_SESSION['hora']=time();
session_write_close();
header("location: index.php");
exit();
}else {
//Login failed
header("location: login-error.php");
exit();
}
}else {
die("Fallo el Query!");
}
?>
Código PHP:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ETL Computación - Formulario de Contacto - Base de Datos - Login</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<form id="loginForm" name="loginForm" method="post" action="login-exec.php">
<table width="200" border="0" align="center">
<tr>
<td>Usuario: </td>
<td><input name="login" type="text" id="login" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" id="password" /></td>
</tr>
<tr>
<td colspan="2"><input name="Submit" type="submit" id="entrar" value="Entrar" /></td>
</tr>
</table>
</form>
<div align="center"><a href="resetmail/"><p>¿Olvidó su contraseña?</p></a></div>
</body>
</html>
Gracias a todos por la ayuda!!