Yo lo haria mas o menos asì (claro que si solo te interesa bloquear quien llega de fuera es demasiado, pero tambien es un buen metodo por hacking ataques)
formulario:
Código PHP:
<?php
function generateFormToken($formName)
{
$token = md5(uniqid(microtime(), true));
$_SESSION[$formName.'_token'] = $token;
return $token;
}
$newToken = generateFormToken('loginForm');
?>
<h1>conexion </h1>
<form id="loginForm" name="loginForm" method="POST" action="login-action.php">
<input type="hidden" name="token" value="<?php echo $newToken ?>">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td width="112"><b>nombre de usuario</b></td>
<td width="188"><input name="login" type="text" class="textfield" id="login" /></td>
</tr>
<tr>
<td><b>clave</b></td>
<td><input name="password" type="password" class="textfield" id="password" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="entrar" /></td>
</tr>
</table>
</form>
action-login.php:
Código PHP:
<?php
function verifyFormToken($formName)
{
$index = $formName.'_token';
// There must be a token in the session
if (!isset($_SESSION[$index])) return false;
// There must be a token in the form
if (!isset($_POST['token'])) return false;
// The token must be identical
if ($_SESSION[$index] !== $_POST['token']) return false;
return true;
}
if (!verifyFormToken('loginForm')) {
die('CSRF Attack detected OR External form processing detected');
}
//Include database connection details
require_once('conf.db.php');
//Array de errores
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, D//Include database connection details
require_once('conf.db.php');
//Array de errores
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//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);
}
//limpiar las variables
$login = clean($_POST['login']);
$password = clean($_POST['password']);
//Input Validations
if($login == '') {
$errmsg_arr[] = 'Login no valido';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'error introduzca contraseña';
$errflag = true;
}
//si hay un error en los datos ingresados volver al form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login-form.php");
exit();
}
//Create query
$qry="SELECT * FROM usuarios 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 correcto
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_USERID'] = $usuario['user_id'];
$_SESSION['SESS_FIRST_NAME'] = $usuario['firstname'];
$_SESSION['SESS_EMAIL'] = $usuario['email'];
$_SESSION['SESS_EDAD'] = $usuario['edad'];
session_write_close();
header("location: conectado.php");
exit();
}else {
//Login fallido
header("location: login-error.php");
exit();
}
}else {
die("Query failed");
}B_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//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);
}
//limpiar las variables
$login = clean($_POST['login']);
$password = clean($_POST['password']);
//Input Validations
if($login == '') {
$errmsg_arr[] = 'Login no valido';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'error introduzca contraseña';
$errflag = true;
}
//si hay un error en los datos ingresados volver al form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login-form.php");
exit();
}
//Create query
$qry="SELECT * FROM usuarios 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 correcto
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_USERID'] = $usuario['user_id'];
$_SESSION['SESS_FIRST_NAME'] = $usuario['firstname'];
$_SESSION['SESS_EMAIL'] = $usuario['email'];
$_SESSION['SESS_EDAD'] = $usuario['edad'];
session_write_close();
header("location: conectado.php");
exit();
}else {
//Login fallido
header("location: login-error.php");
exit();
}
}else {
die("Query failed");
}
?>