memorizzare utente in una tabella

« Older   Newer »
 
  Share  
.
  1. marKolixxx
     
    .

    User deleted


    ciao a tutti sto lavorando al sito di un'amico, che vuole creare una registrazione per gli utenti.
    Il sito è su altervista.
    Questo è il sito:
    http://prizex.altervista.org/
    cliccando su registrati: http://prizex.altervista.org/registrazione/registrati.php
    l'utente completa i campi e viene spedita una mail per confermare
    CODICE REGISTRAZIONE
    CODICE
    <html>
    <head>
    <title>Modulo di registrazione</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
    <!--
    body {
           background-color: #000;
    }
    body,td,th {
           color: #FFF;
    }
    .l {
           text-align: left;
           font-family: "Comic Sans MS", cursive;
           font-size: 100px;
           font-weight: bold;
    }
    -->
    </style></head>
    <body>
    <center>
    <form action="register.php" method="post">
    <div align="center">
     <p class="l">prizeX</p>
    <table border="0" width="300">
           <tr>
                   <td>Nome:</td>
                   <td><input type="text" name="name"></td>
           </tr>
           <tr>
                   <td>Cognome:</td>
                   <td><input type="text" name="surname"></td>
           </tr>
           <tr>
                   <td>Indirizzo:</td>
                   <td><input type="text" name="indirizzo"></td>
           </tr>
           <tr>
                   <td>Occupazione</td>
                   <td><input type="text" name="occupazione"></td>
           </tr>
           <tr>
                   <td>Username:</td>
                   <td><input type="text" name="username"></td>
           </tr>
           <tr>
                   <td>Password:</td>
                   <td><input type="password" name="password"></td>
           </tr>
           <tr>
                   <td>Mail:</td>
                   <td><input type="text" name="mail"></td>
           </tr>
           <tr>
                   <td colspan="2" align="center"><input type="submit" name="action" value="Invia"></td>
           </tr>
    </table>
    </div>
    </form>
    </center>
    </body>
    </html>


    CODICE INVIO MAIL
    CODICE
    <html>
    <head>
    <style type="text/css">
    <!--
    .style1 {
           color: #FF0000;
           font-weight: bold;
    }
    body {
           background-color: #000;
    }
    body,td,th {
           color: #F00;
    }
    -->
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
    <body>
    <p>
     <?php
    include_once("include/config.php");
    include_once("include/reg.lib.php");

    if(isset($_POST['action']) and $_POST['action'] == 'Invia'){
           $ret = reg_check_data($_POST);
           $status = ($ret === true) ? reg_register($_POST) : REG_ERRORS;
           
           switch($status){
                   case REG_ERRORS:
                           ?>
     <span class="style1">Sono stati rilevati i seguenti errori:</span><br>
     <?php
                           foreach($ret as $error)
                                   printf("<b>%­s</b>: %­s<br>", $error[0], $error[1]);
                           ?>
     <br>Premere "indietro" per modificare i dati
     <?php
                   break;
                   case REG_FAILED:
                           echo "Registrazione Fallita a causa di un errore interno.";
                   break;
                   case REG_SUCCESS:
                           echo "Registrazione avvenuta con successo.<br>
                           Vi è stata inviata una email contente le istruzioni per confermare la registrazione.";
                   break;
           }
    }
    ?>
    </p>
    <p>&nbsp;</p>
    </body>
    </html>

    FILE CONFIG
    CODICE
    <?php
    $_CONFIG['host'] = "localhost";
    $_CONFIG['user'] = "prizex";
    $_CONFIG['pass'] = "password";
    $_CONFIG['dbname'] = "my_prizex";

    $_CONFIG['table_sessioni'] = "sessioni";
    $_CONFIG['table_utenti'] = "utenti";

    $_CONFIG['expire'] = 60;
    $_CONFIG['regexpire'] = 24; //in ore

    $_CONFIG['check_table'] = array(
           "username" => "check_username",
           "password" => "check_global",
           "name" => "check_global",
           "surname" => "check_global",
           "indirizzo" => "check_global",
           "occupazione" => "check_global",
           "mail" => "check_global"
    );

    function check_username($value){
           global $_CONFIG;
           
           $value = trim($value);
           if($value == "")
                   return "Il campo non può essere lasciato vuoto";
           $query = mysql_query("
           SELECT id
           FROM ".$_CONFIG['table_utenti']."
           WHERE username='".$value."'");
           if(mysql_num_rows($query) != 0)
                   return "Nome utente già utilizzato";
           
           return true;
    }

    function check_global($value){
           global $_CONFIG;
           
           $value = trim($value);
           if($value == "")
                   return "Il campo non può essere lasciato vuoto";
           
           return true;
    }


    //--------------
    define('AUTH_LOGGED', 99);
    define('AUTH_NOT_LOGGED', 100);

    define('AUTH_USE_COOKIE', 101);
    define('AUTH_USE_LINK', 103);
    define('AUTH_INVALID_PARAMS', 104);
    define('AUTH_LOGEDD_IN', 105);
    define('AUTH_FAILED', 106);

    define('REG_ERRORS', 107);
    define('REG_SUCCESS', 108);
    define('REG_FAILED', 109);

    $conn = mysql_connect($_CONFIG['host'], $_CONFIG['user'], $_CONFIG['pass']) or die('Impossibile stabilire una connessione');
    mysql_select_db($_CONFIG['dbname']);
    ?>


    CODICE EMAIL
    CODICE
    <?php
    function reg_register($data){
           //registro l'utente
           global $_CONFIG;
           
           $id = reg_get_unique_id();
           mysql_query("
           INSERT INTO ".$_CONFIG['table_utenti']."
           (name, surname, indirizzo, occupazione, username, password, temp, regdate, uid)
           VALUES
           ('".$data['name']."','".$data['surname']."','".$data['indirizzo']."',
           '".$data['occupazione']."','".$data['username']."',MD5('".$data['password']."'),
           '1', '".time()."','".$id."')");
           
           //Decommentate la riga seguente per testare lo script in locale
           //echo "<a href=\"http://localhost/Articoli/autenticazione/2/scripts/confirm.php?id=".$id."\">Conferma</a>";
           if(mysql_insert_id()){
                   return reg_send_confirmation_mail($data['mail'], "prizeX@register", $id);
           }else return REG_FAILED;
    }

    function reg_send_confirmation_mail($to, $from, $id){
           //invio la mail di conferma
           $msg = "Per confermare l'avvenuta registrazione, cliccate il link seguente:
           http://prizex.altervista.org/registrazione/confirm.php".$id."
           ";
           return (mail($to, "Conferma la registrazione", $msg, "From: ".$from)) ? REG_SUCCESS : REG_FAILED;
    }

    function reg_clean_expired(){
           global $_CONFIG;
           
           $query = mysql_query("
           DELETE FROM ".$_CONFIG['table_utenti']."
           WHERE (regdate + ".($_CONFIG['regexpire'] * 60 * 60).") <= ".time()." and temp='1'");
    }

    function reg_get_unique_id(){
           //restituisce un ID univoco per gestire la registrazione
           list($usec, $sec) = explode(' ', microtime());
           mt_srand((float) $sec + ((float) $usec * 100000));
           return md5(uniqid(mt_rand(), true));
    }

    function reg_check_data(&$data){
           global $_CONFIG;
           
           $errors = array();
           
           foreach($data as $field_name => $value){
                   $func = $_CONFIG['check_table'][$field_name];
                   if(!is_null($func)){
                           $ret = $func($value);
                           if($ret !== true)
                                   $errors[] = array($field_name, $ret);
                   }
           }
           
           return count($errors) > 0 ? $errors : true;
    }

    function reg_confirm($id){
           global $_CONFIG;
           
           $query = mysql_query("
           UPDATE ".$_CONFIG['table_utenti']."
           SET temp='0'
           WHERE uid='".$id."'");
           
           return (mysql_affected_rows () != 0) ? REG_SUCCESS : REG_FAILED;
    }
    ?>


    In questa ultima parte di codice della email faccio confermare e cliccando sul link mando alla pagina confirm
    CONFIRM
    CODICE
    <style type="text/css">
    <!--
    body,td,th {
           color: #FFF;
    }
    body {
           background-color: #000;
    }
    .d {
           font-size: 36px;
           font-weight: bold;
           font-family: "Comic Sans MS", cursive;
    }
    -->
    </style>
    <span class="d">prizeX
    </span>
    <?php
    include_once("include/config.php");
    include_once("include/reg.lib.php");

    if(isset($_GET['id']) and strlen($_GET['id']) == 32){
           reg_clean_expired();
           $status = reg_confirm($_GET['id']);
           
           switch($status){
                   case REG_SUCCESS:
                           echo "La tua registrazione è stata confermata; ora puoi effettuare il login.";
                   break;
                   case REG_FAILED:
                           echo "La registrazione non può essere confermata, probabilemente poichè è scaduta.";
                   break;
           }
    }
    ?>


    La registrazione avviene con successo e arriva la mail, ma cliccando sul link della mail mi manda alla pagina http://prizex.altervista.org/registrazione/confirm.php ma non trova la pagina!

    Edited by ~Andrey™ - 16/8/2010, 22:25
     
    Top
    .
  2. ~Andrey™
     
    .

    User deleted


    In che senso non trova la pagina?
     
    Top
    .
  3. marKolixxx
     
    .

    User deleted


    Errore 404 :(

    La pagina http://prizex.altervista.org/registrazione...3ee251bb04a2687 non è presente, questo può dipendere dal fatto che:

    La pagina non esiste più.
    Il link che hai seguito non è valido.
    La pagina è stata spostata.
    Fai attenzione alle MAIUSCOLE e MINUSCOLE, ricorda che http://sito.altervista.org/Pagina.html è diverso da http://sito.altervista.org/pagina.html
    www.altervista.org
     
    Top
    .
  4. ~Andrey™
     
    .

    User deleted


    Qui c'è un errore.
    CODICE
    $msg = "Per confermare l'avvenuta registrazione, cliccate il link seguente:
          http://prizex.altervista.org/registrazione/confirm.php".$id."
          ";

    Dev'essere:
    CODICE
    $msg = "Per confermare l'avvenuta registrazione, cliccate il link seguente:
    http://prizex.altervista.org/registrazione/confirm.php?id=".$id;


    Devi mettere "id=", e poi il valore della variabile.

    Nel tuo esempio il link è http://prizex.altervista.org/registrazione...3ee251bb04a2687

    Ho già provato e funziona, aggiungi comunque la gestione del caso in cui l'utente ha già cliccato il link una volta, quindi è già registrato.
     
    Top
    .
  5. marKolixxx
     
    .

    User deleted


    ok, ora funziona, ma quando vado ad eseguire il login compare questo errore
    Fatal error: Call to undefined function auth_get_status() in /membri/prizex/login.php on line 5

    ora posto il codice delle tabelle...

    CODICE
    CREATE TABLE sessioni (
      uid CHAR(32) NOT NULL,
      user_id INT UNSIGNED NOT NULL,
      creation_date INT UNSIGNED NOT NULL,
      INDEX(uid)
    );

    CODICE
    CREATE TABLE utenti (
      id INT UNSIGNED NOT NULL AUTO_INCREMENT,
      name VARCHAR(30) NOT NULL,
      surname VARCHAR(30) NOT NULL,
      username VARCHAR(30) NOT NULL,
      password CHAR(32) NOT NULL,
      PRIMARY KEY(id),
      INDEX(username, password)
    );

    CODICE
    ALTER TABLE utenti
    ADD indirizzo VARCHAR( 100 ) NOT NULL,
    ADD occupazione VARCHAR( 100 ) NOT NULL,
    ADD temp SET( '0', '1' ) NOT NULL,
    ADD regdate VARCHAR( 11 ) NOT NULL,
    ADD uid VARCHAR( 32 ) NOT NULL;



     
    Top
    .
  6. ~Andrey™
     
    .

    User deleted


    Le tabelle non c'entrano. Probabilmente manca l'inclusione ad un file .php che contiene la funzione auth_get_status(). Posta il file .php che gestisce il login, a quanto vedo è la index.
     
    Top
    .
  7. marKolixxx
     
    .

    User deleted


    CODICE
    <style type="text/css">
    <!--
    body,td,th {
           color: #FFF;
    }
    body {
           background-color: #000;
    }
    -->
    </style><?php
    include_once("include/config.php");
    include_once("include/auth.lib.php");

    list($status, $user) = auth_get_status();

    if($status == AUTH_NOT_LOGGED){
           $uname = strtolower(trim($_POST['uname']));
           $passw = strtolower(trim($_POST['passw']));

           if($uname == "" or $passw == ""){
                   $status = AUTH_INVALID_PARAMS;
           }else{
                   list($status, $user) = auth_login($uname, $passw);
                   if(!is_null($user)){
                           list($status, $uid) = auth_register_session($user);
                   }
           }
    }

    switch($status){
           case AUTH_LOGGED:
                   header("Refresh: 5;URL=home.php");
                   echo '<div align="center">Sei gia connesso ... attendi il reindirizzamento</div>';
           break;
           case AUTH_INVALID_PARAMS:
                   header("Refresh: 5;URL=index.php");
                   echo '<div align="center">Hai inserito dati non corretti ... attendi il reindirizzamento</div>';
           break;
           case AUTH_LOGEDD_IN:
                   switch(auth_get_option("TRANSICTION METHOD")){
                           case AUTH_USE_LINK:
                                   header("Refresh: 5;URL=home.php?uid=".$uid);
                           break;
                           case AUTH_USE_COOKIE:
                                   header("Refresh: 5;URL=home.php");
                                   setcookie('uid', $uid, time()+3600*365);
                           break;
                           case AUTH_USE_SESSION:
                                   header("Refresh: 5;URL=home.php");
                                   $_SESSION['uid'] = $uid;
                           break;
                   }
                   echo '<div align="center">Ciao '.$user['name'].' ... attendi il reindirizzamento</div>';
           break;
           case AUTH_FAILED:
                   header("Refresh: 5;URL=home.php");
                   echo '<div align="center">Fallimento durante il tentativo di connessione ... attendi il reindirizzamento</div>';
           break;
    }
    ?>


    è il codice del login, la pagina home viene aperta se il login è corretto, mentre se è sbagliato rimanda alla index
     
    Top
    .
  8. ~Andrey™
     
    .

    User deleted


    Beh, la funzione dovrebbe essere contenuta nel file 'include/auth.lib.php' che viene incluso. Postalo.
     
    Top
    .
  9. marKolixxx
     
    .

    User deleted


    ecco il codice
    CODICE
    <?php

    $_AUTH = array(
           "TRANSICTION METHOD" => AUTH_USE_COOKIE
    );

    function auth_set_option($opt_name, $opt_value){
           global $_AUTH;
           
           $_AUTH[$opt_name] = $opt_value;
    }

    function auth_get_option($opt_name){
           global $_AUTH;
           
           return is_null($_AUTH[$opt_name])
                   ? NULL
                   : $_AUTH[$opt_name];
    }

    function auth_clean_expired(){
           global $_CONFIG;
           
           $result = mysql_query("SELECT creation_date FROM ".$_CONFIG['table_sessioni']." WHERE uid='".auth_get_uid()."'");
           if($result){
                   $data = mysql_fetch_array($result);
                   if($data['creation_date']){
                           if($data['creation_date'] + $_CONFIG['expire'] <= time()){
                                   switch(auth_get_option("TRANSICTION METHOD")){
                                           case AUTH_USE_COOKIE:
                                                   setcookie('uid');
                                           break;
                                           case AUTH_USE_LINK:
                                                   global $_GET;
                                                   $_GET['uid'] = NULL;
                                           break;
                                   }
                           }
                   }
           }
           
           mysql_query("
           DELETE FROM ".$_CONFIG['table_sessioni']."
           WHERE creation_date + ".$_CONFIG['expire']." <= ".time()
           );
    }

    function auth_get_uid(){
           
           $uid = NULL;

           switch(auth_get_option("TRANSICTION METHOD")){
                   case AUTH_USE_COOKIE:
                           global $_COOKIE;
                           $uid = $_COOKIE['uid'];
                   break;
                   case AUTH_USE_LINK:
                           global $_GET;
                           $uid = $_GET['uid'];
                   break;
           }

           return $uid ? $uid : NULL;
    }

    function auth_get_status(){
           global $_CONFIG;

           auth_clean_expired();
           $uid = auth_get_uid();
           if(is_null($uid))
                   return array(100, NULL);
           
           $result = mysql_query("SELECT U.name as name, U.surname as surname, U.username as username
           FROM ".$_CONFIG['table_sessioni']." S,".$_CONFIG['table_utenti']." U
           WHERE S.user_id = U.id and S.uid = '".$uid."'");
           
           if(mysql_num_rows($result) != 1)
                   return array(100, NULL);
           else{
                   $user_data = mysql_fetch_assoc($result);
                   return array(99, array_merge($user_data, array('uid' => $uid)));
           }
    }

    function auth_login($uname, $passw){
           global $_CONFIG;

           $result = mysql_query("
           SELECT *
           FROM ".$_CONFIG['table_utenti']."
           WHERE username='".$uname."' and password=MD5('".$passw."')"
           );
           
           if(mysql_num_rows($result) != 1){
                   return array(AUTH_INVALID_PARAMS, NULL);
           }else{
                   $data = mysql_fetch_array($result);
                   return array(AUTH_LOGEDD_IN, $data);
           }
    }

    function auth_generate_uid(){

           list($usec, $sec) = explode(' ', microtime());
           mt_srand((float) $sec + ((float) $usec * 100000));
           return md5(uniqid(mt_rand(), true));
    }

    function auth_register_session($udata){
           global $_CONFIG;
           
           $uid = auth_generate_uid();
           
           mysql_query("
           INSERT INTO ".$_CONFIG['table_sessioni']."
           (uid, user_id, creation_date)
           VALUES
           ('".$uid."', '".$udata['id']."', ".time().")
           "
           );
           if(!mysql_insert_id()){
                   return array(AUTH_LOGEDD_IN, $uid);
           }else{
                   return array(AUTH_FAILED, NULL);
           }
    }

    function auth_logout(){
           global $_CONFIG;

           $uid = auth_get_uid();
           
           if(is_null($uid)){
                   return false;
           }else{
                   mysql_query("
                   DELETE FROM ".$_CONFIG['table_sessioni']."
                   WHERE uid = '".$uid."'"
                   );
                   return true;
           }
    }
    ?>


    Edited by marKolixxx - 19/8/2010, 12:35
     
    Top
    .
  10. marKolixxx
     
    .

    User deleted


    idee?
     
    Top
    .
  11. ~Andrey™
     
    .

    User deleted


    Beh, l'unica è che il percorso del file incluso sia errato, perchè la funzione c'è.

    Controlla che venga incluso correttamente, basta una cosa del genere:

    CODICE
    if (include_once 'include/auth.lib.php')
       echo 'Incluso';
    else
       echo 'Non incluso';


    PS: include_once e gli altri costrutti simili non richiedono le parentesi, ed è meglio usare gli apici singoli.
     
    Top
    .
  12. marKolixxx
     
    .

    User deleted


    grazie ~Andrey™, pensa che era solo per una lettera sbagliata!!!
    GRRRR ^_^
     
    Top
    .
  13. ~Andrey™
     
    .

    User deleted


    CITAZIONE (marKolixxx @ 19/8/2010, 19:59)
    grazie ~Andrey™, pensa che era solo per una lettera sbagliata!!!
    GRRRR ^_^

    Eh beh, anche un solo carattere fa la differenza, bisogna stare molto attenti :)

    Comunque bene, chiudo ;)
     
    Top
    .
12 replies since 16/8/2010, 19:24   952 views
  Share  
.