Ver Mensaje Individual
  #14 (permalink)  
Antiguo 30/06/2009, 15:11
waty90
 
Fecha de Ingreso: diciembre-2006
Mensajes: 153
Antigüedad: 17 años, 4 meses
Puntos: 1
Pregunta Respuesta: bloquear formularios externos con php.

formulario login-form.php:

Código PHP:
<h1>conexion </h1>

<
form id="loginForm" name="loginForm" method="POST" action="login-action.php">

<
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>&nbsp;</td>
      <
td><input type="submit" name="Submit" value="entrar" /></td>
    </
tr>
</
table>
</
form>
   
         
        </
div


y el archivo que procesa el login: login-action.php


Código PHP:
<?

//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_HOSTDB_USERDB_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");
    }
?>