Dsy Network www | forum | my | didattica | howto | wiki | el goog | stats | blog | dona | rappresentanti
Homepage
 Register   Calendar   Members  Faq   Search  Logout 
.dsy:it. : Powered by vBulletin version 2.3.1 .dsy:it. > Didattica > Corsi A - F > Basi di dati ~ informatica triennale > Un buon uso di php :)
  Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
NoWhereMan
.illuminato.

User info:
Registered: Jul 2003
Posts: 222 (0.03 al dì)
Location: Segrate (MI)
Corso: Dottorato in Informatica
Anno:
Time Online: 1 Day, 21:56:46 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Un buon uso di php :)

oggi ho chiacchierato con un paio di voi a proposito di php e dell'uso di $_POST e $_SESSION; riflettendoci credo che ai vostri fini sia molto meglio usare $_POST, dovete solo rivedere un attimo l'organizzazione del vostro codice.

Non pensate ai file *.php come a delle unità che eseguono codice: costruite un file che nei richiami altri attraverso include(), piuttosto (qui nel codice ho usato require())!

Se il vostro scopo è definire un file dove "vengono eseguite le transazioni", costruite piuttosto N funzioni all'interno di questo file e poi includetelo!

se la sessione vi serve ESCLUSIVAMENTE nel momento in cui salvate o tentate il salvataggio dei dati, è COMPLETAMENTE inutile!

Ecco del codice che spero possa esservi di aiuto (abbiate pietà per gli eventuali errori di sintassi... non l'ho testato e sono tornato un'ora fa :D)


PHP:

transactions.php
---------------------
<?php

/*
  ritorna un array vuoto se non ci sono errori
  oppure un array associativo 
  con i nomi dei campi non validi come chiavi
  
  NOTA: array vuoto valuta == false, quindi bisogna dare un
  if ( !trans_registration_errors($_POST)  )
  
  
*/

function trans_user_exist($username) {
    
// controlla se lo username esiste
    // inserire qui il codice
    
return false;
}

function 
trans_registration_errors($data) {

    
// nota: se vi dà noia usare $data['campo'] usate la funzione 
    // extract($data) :  genererà variabili aventi il nome del campo
    // si usa così: supponiamo di avere $data['pippo']="pluto";
    // extract($data);
    // ora potete usare $pippo che avrà valore 'pluto'
    

    
     
$messaggi = array();
     if (empty(
$data['usr']) {
           
$messaggi['usr'] = 'Non hai inserito il nome!';
     } elseif (
trans_user_exist($data['usr'])) {
        
// in questo modo se un utente fa refresh sulla pagina con il postdata non viene creato un nuovo utente!
        
$messaggi['usr'] = "Il nome utente esiste già: sceglierne un altro";
     }
     
     
     if (
strlen($data['pwd'])<6) {
           
$messaggi['usr'] = "La password dev'essere lunga almeno 6 caratteri";
     }
     
     
// altri if vanno qui. nota: io uso di solito gli if, perché
     // lo switch/case fa solo test di uguaglianza

     
return $messaggi;

}

/*
    inserisce i dati nel database my_sql
    ritorna true o false
*/
    

function trans_registration_insert($data) {
    
    
// fa cose varie....
    
return true;
    
}

// altro codice andrà qui

?>
FINE transactions.php
-------------


forms.php
---------------------
<?php

    
function register_form_show() {
?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'?>
            method="POST" name="vbform">
            
    <p>
    Inserisci i tuoi dati personali
    </p>
    
    <p>
    <label for="usr">Username</label>
    <input type="text" name="usr" id="usr">
    </p>
    
    <p>
    <label for="usr">Password</label>
    <input type="password" name="pwd" id="pwd">
    </p>    

</form>    
    
<?php
    
}
    
    
// altri form utili...

?>




register.php
-------------
<?php

require('transactions.php');
require(
'forms.php');

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>

   <head>
      <title>Blog</title>
   </head>


   <body>
      <?php
        
if ( !empty($_POST) && !($array_errori trans_registration_errors($_POST)) ) {
            
            
// successo!
            
echo "<p>Registrazione di $_POST['usr'] effettuata con successo</p>";
            
// non è necessario che sia un if/else, ovviamente
            
        
} else {
            
            
/*
            Se ci sono stati errori, visualizziamoli prima del form, formattati
            in un'elegante lista <img src="images/smilies/icon_wink.gif" border="0" alt=";)">
            */
            
            
if ($array_errori) {
            
                echo 
"<ul>"
                
foreach ($array_errori as $chiave => $valore) {
                    echo 
"<li>$valore</li>\n";
                }
                echo 
"</ul>";
            
            }
            
            
register_form_show();
      }
      
?>
   </body>

</html>




naturalmente questo è UN modo, ma mi sembra meglio della SESSION, che è utile in altri casi, ossia quando vorrete gestire il login :)

PS: nel database è meglio memorizzare l'hash md5 della password

$pwdhash = md5($pwd);


ciao!

Last edited by NoWhereMan on 29-03-2007 at 19:47

29-03-2007 19:41
Click Here to See the Profile for NoWhereMan Click here to Send NoWhereMan a Private Message Visit NoWhereMan's homepage! Find more posts by NoWhereMan Add NoWhereMan to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 03:37.    Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread | Add to Bookmarks

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

Powered by: vBulletin v2.3.1 - Copyright ©2000 - 2002, Jelsoft Enterprises Limited
Mantained by dsy crew (email) | Collabora con noi | Segnalaci un bug | Archive | Regolamento | Licenze | Thanks | Syndacate
Pagina generata in 0.146 seconds (55.55% PHP - 44.45% MySQL) con 26 query.