Ver Mensaje Individual
  #8 (permalink)  
Antiguo 05/07/2013, 06:48
gio_vela10
 
Fecha de Ingreso: agosto-2011
Ubicación: por ahi
Mensajes: 28
Antigüedad: 12 años, 9 meses
Puntos: 0
Respuesta: Usando Hash en URL

Hola amigos, gracias por las respuestas, voy a explicar más o menos como es la estructura de la aplicación.

En la BD tengo 3 Tablas:
Proceso: id,fechaCreado,FechaModificado,email,hash,tipoProc eso,hash_Usado,transferenciaTerminada,nombre,titul o, mensaje.
Files:id,fechaCreado,fechaModificado,nombre,tipo,hash,f ile,tamano, Ref_process.
Recipientes:id,fechaCreado,FechaModificado,nombre,email,hash, hash_usado,transferencia_confirmada,Ref_process.

En el codigo tengo un index principal, que es donde el usuario puede mandar un Email con el link+hash.
Este sería el archivo PHP que procesa la info:
Código PHP:
<?php
include_once '../mailer/class.phpmailer.php';
include_once 
'conec.php';    
            
$mail = new PHPMailer(true);            
            if(isset(
$_POST['name']) && !empty($_POST['name']) AND isset($_POST['email']) && !empty($_POST['email']) AND isset($_POST['switch']) OR isset($_POST['email2']) && !empty($_POST['email2']) AND isset($_POST['mailfrom']) && !empty($_POST['mailfrom'])){
                
$name mysql_escape_string($_POST['name']);
                
$opcion mysql_escape_string($_POST['switch']);
                if(
$opcion == "op1"){
                    
$emailC $_POST['email'].'@'.$_POST['filiales'];
                    
$email mysql_escape_string($emailC);  
                }else{
                    
$emailTo mysql_escape_string($_POST['email2']);
                    
$email2 $_POST['mailfrom'].'@'.$_POST['filiales2'];
                    
$email mysql_escape_string($email2); 
                }
                          
            if(
preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@/"$_POST['email']) or preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/"$_POST['email']))
            {
                
// Return Error - Invalid Email                
                
echo 0;                
            }else{
                
                if(!
preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/"$email)){
                    
// Return Error - Invalid Email
                    
echo 0;
                }else{                        
                    
// Return Success - Valid Email    
                    
                    
$hash md5(uniqid(rand())); // Generate random 32 character hash and assign it to a local variable.
                     
$horaActual date("d-m-Y H:i:s");
                     if(
$opcion == "op1"){
                         
$tipo 0;
                     }else if(
$opcion == "op2"){
                         
$tipo 1;
                     }                    
                    
mysql_query("INSERT INTO Proceso(correo, token, tipo, token_usado, nombre, titulo, mensaje) VALUES(
                    '"
mysql_escape_string($email) ."',
                    '"
mysql_escape_string($emailTo) ."',  
                    '"
mysql_escape_string($hash) ."', 
                    '"
mysql_escape_string($tipo) ."', 
                    '"
mysql_escape_string(0) ."', 
                    '"
mysql_escape_string($name) ."', 
                    '"
mysql_escape_string('') ."', 
                    '"
mysql_escape_string('') ."') ") or die(mysql_error());     
                                        
                    
$mail->From $email;
                    
$mail->FromName 'smth';
                    
$mail->IsHTML(true); // Set email format to HTML                    
                    
$mail->Subject 'smth';
                    if(
$opcion == 'op1'){
                        
$mail->AddAddress($email'smth');  // Add a recipient
                        
$mail->Body    '
<b>Thanks a lot!</b><br />
Your account has been validated, you can send a file by pressing the url below.
<br />
    ------------------------<br />
    Name: '
.$name.'        <br />        
    ------------------------<br />

Please click this link:<br />
http://dominio.com/verify/verify.php?hash='
.$hash.'

                    '
// Our message above including the link
                    
}else if($opcion == 'op2'){
                        
$mail->AddAddress($emailTo'smth');  // Add a recipient
                            
$mail->Body    '
<b>Hello</b><br />
You have received a link to upload a file, you can use it by pressing the url below.
<br />
Please click this link:<br />
http://dominio.com/verify/verify.php?hash='
.$hash.'

                    '
// Our message above including the link
                    
}                    
                    if(!
$mail->Send()) {
                       echo 
'Message could not be sent.';
                       echo 
'Mailer Error: ' $mail->ErrorInfo;
                       exit;
                    }else{
                        echo 
1;
                    }         
                    } 
// End second else 
                
// End first else
            
// End IF
            
mysql_close();            
?>
Cuando el usuario recibe el correo con el link+hash, los mando un archivo para verificar el estado del hash, como sigue:
Código PHP:
<?php        
    
include_once 'conec.php';        

    if(isset(
$_GET['hash']) && !empty($_GET['hash'])){
        
// Verify data    
        
if( ! ctype_alnum($_GET['hash'])){            
            
$hash strip_tags($_GET['hash']); // Set hash variable
            
$cons mysql_query("SELECT email FROM proceso WHERE hash='".$hash."' AND hash_usado='0'") or die(mysql_error());
            
$valida mysql_fetch_array($cons);
            
$email $valida[0];
            
$search mysql_query("SELECT email, hash, hash_usado FROM proceso WHERE email='".$email."' AND hash='".$hash."' AND hash_usado='0'") or die(mysql_error()); 
            
$match  mysql_num_rows($search);
            
$today date("Y-m-d H:i:s"); 
            if(
$match 0){
            
// We have a match, activate the account
                
mysql_query("UPDATE proceso SET hash_usado='1',record_modified ='".$today."' WHERE email='".$email."' AND hash='".$hash."' AND hash_usado='0'") or die(mysql_error());
                
header('Location: ../index.php?hash='.$hash);
                exit();
            }else{
            
// No match -> invalid url or account has already been activated.
                //echo '<div class="alert alert-error">The url is either invalid or you already have not been activated your account.</div>';
                
header('Location: ../404.html');
                exit();
            }
        }else{
            
// Invalid approach
            
header('Location: ../404.html');
            exit();
        }                
    }else{
    
// Invalid approach
        //echo '<div class="alert alert-error">Invalid approach, please use the link that has been send to your email.</div>';
        
header('Location: ../404.html');
        exit();
    }
    
mysql_close();
?>
Bueno aqui si todo sale bien, modifico el campo de "token_usado" a 1 (o true, que es mejor para ustedes?) en la tabla Proceso.
y lo redirijo a la parte donde puede mandar correo a N Recipientes con N Files mediante un link+hash del proceso, todos los recipientes tendrian el ID-Refencia del proceso.

Lo dejare hasta aqui por hoy, en el archivo dondeles agradezco cualquier consejo o critica, que quieran hacer.

Gracias de antemano.