In una pagina in php ho alcune form dove gli utenti possono modificare i loro dati
Codice PHP:
<?php
session_start();
if(!IsSet($_SESSION['accreditato'])){
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'noaccesso.php';
header("Location: http://$host$uri/$extra");
exit;
} else {
require_once('/includes/conn_db.php');
/* DATI COMBO COMUNI */
$query_comuni="SELECT IDComune, Comune FROM fas_comuni";
$ris_comuni = mysql_query($query_comuni) or die(mysql_error());
/* variabile 'filtro' */
$filtro = "-1";
if (isset($_SESSION ['ID'])) {
$filtro = $_SESSION ['ID'];
}
/* DATI UTENTE */
$query_utente = sprintf("SELECT IDlogin, utente, utente_email, utente_telefono, utente_mobile, utente_fax FROM fas_login WHERE IDlogin = %s", $filtro);
$ris_utente = mysql_query ($query_utente) or die (mysql_error());
$row_utente = mysql_fetch_array($ris_utente);
/* $totalRows_utente = mysql_num_rows($ris_utente); ISTRUZIONE IN PIU'*/
}
/* DATI ENTE */
$query_ente = sprintf("SELECT IDente, IDlogin, ente, ente_tipoPICF, ente_PICF, ente_via, ente_civico, ente_comune, ente_cap, ente_email, ente_telefono, ente_fax FROM fas_enti WHERE IDlogin = %s", $filtro);
$ris_ente = mysql_query($query_ente) or die(mysql_error());
$row_ente = mysql_fetch_array($ris_ente);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<!-- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -->
<title>Formazione Asili Nido - Dati Utente</title>
<style type="text/css" media="all">
@import url("stile.css");
@import url("genericform.css");
</style>
</head>
<body>
<div id="container">
<div id="top">
<div id="intestazione"><span id="fltright">La Bottega del Formatore</span></div>
<div id="menu">
<table align="center" cellpadding="5" cellspacing="10">
<tr>
<td><?php echo "Benvenuto '". $_SESSION['accreditato'] . "'";?></td>
<td><a href="logout.php" target="_self"> Logout </a></td>
</tr>
</table>
</div id="logo">
<img src="immagini/marchio_small.gif" width="150" height="65" /></div>
<!-- INIZIO CONTENT -->
<div id="content">
<p align="center"><a href='utente.php' target='_self'>Torna indietro</a></p>
<table align="center">
<tr>
<td>
<!-- inizio form dati personali -->
<div>
<form action= 'registra_dati_utente.php' method='post' name='datipersonali' target="_self" id="datipersonali">
<table width="700" border="0" cellpadding="5" cellspacing="5">
<tr>
<td colspan="4"><p class="rosso">Dati personali</p></td>
</tr>
<tr class="sfondoarancio">
<td width="75">ID:</td>
<td><label><?php echo $row_utente['IDlogin']; ?></label></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Referente:</td>
<td width="190"><label><?php echo $row_utente['utente']; ?></label></td>
<td width="50">E-mail:</td>
<td width="310"><input name="txt_utente_email" type="text" class="testoform" id="utente_email" value="<?php echo $row_utente['utente_email']; ?>" size="45" /></td>
<tr class="sfondoarancio">
<td>Telefono:</td>
<td><input name="txt_utente_telefono" type="text" class="testoform" id="utente_telefono" value="<?php echo $row_utente['utente_telefono']; ?>" size="25" /></td>
<td>Mobile:</td>
<td><input name="txt_utente_mobile" type="text" class="testoform" id="utente_mobile" value="<?php echo $row_utente['utente_mobile']; ?>" size="25" /></td>
<tr>
<td>Fax:</td>
<td><input name="txt_utente_fax" type="text" class="testoform" id="utente_fax" value="<?php echo $row_utente['utente_fax']; ?>" size="25" /></td>
<td></td>
<td>
<input name="submit1" type="submit" class="button" id="submit1" value="Salva " />
</td>
</tr>
</table>
</form>
<!--- fine form dati personali -->
</div>
<!--- inizio form cambio password -->
<div>
<form action= 'modifica_password.php' method='post' name='cambiopassword' target="_self" id="cambiopassword">
<table width="700" border="0" cellpadding="5" cellspacing="5">
<tr>
<td colspan="2"><span class="rosso">Modifica della password</span></td>
<tr class="sfondogrigio">
<td width="200">Vecchia password:</td>
<td width="455"><input value="" name="txt_vecchia_pass" type="password" class="testoform" id="vecchia_pass" size="10" maxlength="8" />
Caratteri ammessi: <span class="rosso">lettere e/o numeri</span></td>
<tr>
<td>Nuova password:</td>
<td><input value="" name="txt_nuova_pass" type="password" class="testoform" id="nuova_pass" size="10" maxlength="8"/>
Massimo<span class="rosso"> 8 caratteri</span></td>
<tr class="sfondogrigio">
<td>Ripeti la nuova password:</td>
<td><input value="" name="txt_ripeti_pass" type="password" class="testoform" id="ripeti_pass" size="10" maxlength="8"/></td>
</tr>
<tr>
<td></td>
<td align="right"><input name="submit2" type="submit" class="button" id="submit2" value="Salva"/></td>
</tr>
</table>
</form>
<!--- fine form cambio password -->
</div>
<!--- inizio form dati ente -->
<div>
<form action= 'registra_dati_ente.php' method='post' name='entetitolare' target="_self" id="entetitolare">
<table width="700" border="0" cellpadding="5" cellspacing="5">
<tr>
<td colspan="4"><span class="rosso">Ente titolare</span></td>
<tr class="sfondoverde">
<td width="75">Ente:</td>
<td width="325"><input name="txt_ente" type="text" class="testoform" id="ente" value="<?php echo $row_ente['ente']; ?>" size="50" /></td>
<td width="75">Partita IVA / Cod. Fiscale:</td>
<td width="150"><select name="txt_ente_tipoPICF" class="testoform" ID="tipoPICF"><option value="<?php echo $row_ente['ente_tipoPICF']; ?>"><?php echo $row_ente['ente_tipoPICF']; ?></option><option value="PI">PI</option><option value="CF">CF</option></select> <input name="txt_ente_PICF" type="text" class="testoform" id="ente_PICF" size="16" maxlength="16" value="<?php echo $row_ente['ente_PICF']; ?>" /></td>
<tr>
<td>Via:</td>
<td><input name="txt_ente_via" type="text" class="testoform" id="ente_via" value="<?php echo $row_ente['ente_via']; ?>" size="50px" /></td>
<td>Civico:</td>
<td><input name="txt_ente_civico" type="text" class="testoform" id="ente_civico" value="<?php echo $row_ente['ente_civico']; ?>" size="10" maxlength="10" /></td>
<tr class="sfondoverde">
<td>Comune:</td>
<td>
<!--- inizio select -->
<select name="txt_ente_comune" class="testoform" id="ente_comune" >
<option value="<?php echo $row_ente['ente_comune']; ?>"><?php echo $row_ente['ente_comune']; ?></option>
<?php
while ($row_comuni = mysql_fetch_array($ris_comuni)) {
echo "<option value=$row_comuni[1]\">$row_comuni[1]</option>";
}
?>
</select>
<!--- fine select --> </td>
<td>CAP:</td>
<td><input name="txt_ente_cap" type="text" class="testoform" id="ente_cap" size="10" maxlength="5" value="<?php echo $row_ente['ente_cap']; ?>"/></td>
</tr>
<tr>
<td>Telefono:</td>
<td><input name="txt_ente_telefono" type="text" class="testoform" id="ente_telefono" value="<?php echo $row_ente['ente_telefono']; ?>" size="25" /></td>
<td>Fax:</td>
<td><input name="txt_ente_fax" type="text" class="testoform" id="ente_fax" value="<?php echo $row_ente['ente_fax']; ?>" size="25" /></td>
</tr>
<tr class="sfondoverde">
<td>E-mail:</td>
<td><input name="txt_ente_email" type="text" class="testoform" id="ente_email" value="<?php echo $row_ente['ente_email']; ?>" size="50" /></td>
<td>ID Ente:</td>
<td><label><?php echo $row_ente['IDente']; ?></label></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td align="right"><input name="submit3" type="submit" class="button" id="submit3" value="Salva"/></td>
</tr>
</table>
</form>
<p align="center"> </p>
<p align="center"><a href='utente.php' target='_self'>Torna indietro</a></p>
<!--- fine form dati ente titolare -->
</div>
</td>
</tr>
</table>
<!-- end .content -->
</div>
<div id="footer">
<p>Footer</p>
<!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>
<?php
mysql_free_result($ris_comuni);
mysql_free_result($ris_utente);
mysql_free_result($ris_ente);
mysql_close();
?>
Ora, in base ai dati che vuole aggiornare (o anche inserire nel caso della terza form --> form dati ente) nel caso in cui alcuni campi siano compilati male (es. codice fiscale non corretto) ho creato dei controlli che interrompono l'aggiornamento del database e segnalano qual'è il primo campo che è stato compilato male. Naturalmente, se i dati inseriti nella form sono coerenti vengono memorizzati sul database mysql.
Codice PHP:
<?php
session_start();
if(!IsSet($_SESSION['accreditato'])){
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'noaccesso.php';
header("Location: http://$host$uri/$extra");
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Formazione Asili Nido - Operazione interrotta</title>
</style>
<style type="text/css" media="all">
@import url("stile.css");
</style>
</head>
<body>
<div id="container">
<div id="top">
<div id="intestazione"><span id="fltright">Formazione Operatori Asili Nido</span></div>
<div id="menu">
<table align="center" cellpadding="5" cellspacing="10">
<tr>
<td><?php echo "Benvenuto '". $_SESSION['accreditato'] . "'";?></td>
<td><a href="logout.php" target="_self"> Logout </a></td>
</tr>
</table>
</div id="logo">
<img src="immagini/marchio_small.gif" width="150" height="65" /></div>
<div id="content"><!-- start content -->
<p align='center'></p>
<p align='center'></p>
<?php
require_once('/includes/conn_db.php');
require_once('includes/funzioni.php');
$flag_controllo = 0;
//Verifica dei dati immessi
while(list($chiave, $valore) = each($_POST) ){
if (substr($chiave,0,3) == "txt") {
$valore = trim($valore);
if (strlen($valore) ){
if ($chiave == 'txt_ente_email') {
if (strlen($valore) ) {
if (!chkEmail($valore) ) {
echo "<p align='center'>L'indirizzo di posta elettronica digitato non è corretto.</p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
$flag_controllo = 1;
break;
}
}
}
if ($chiave == 'txt_ente_telefono') {
if (!chkNumTel($valore) ) {
echo "<p align='center'>Nel campo 'telefono' sono stati digitati dei caratteri non consentiti.</p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
$flag_controllo = 1;
break;
}
}
if ($chiave == 'txt_ente_fax') {
if (!chkNumTel($valore) ) {
echo "<p align='center'>Nel campo 'fax' sono stati digitati dei caratteri non consentiti.</p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
$flag_controllo = 1;
break;
}
}
if ($chiave == 'txt_ente') {
if (!chkDenom($valore) ) {
echo "<p align='center'>Nel campo 'Ente' sono stati digitati dei caratteri non consentiti,</p>";
echo "<p align='center'>oppure non è stato compilato (CAMPO OBBLIGATORIO).</p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
$flag_controllo = 1;
break;
}
}
if ($chiave == 'txt_ente_PICF') {
if (!chkCFPI($valore,$_POST['txt_ente_tipoPICF']) ) {
echo "<p align='center'>Il campo 'Codice Fiscale' non è stato digitato in modo corretto.</p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
$flag_controllo = 1;
break;
}
}
if ($chiave == 'txt_ente_via') {
if (!chkInd($valore) ) {
echo "<p align='center'>Il campo 'Via' non è stato digitato in modo corretto.</p>";
echo "<p align='center'>Non sono ammessi caratteri numerici ma solo numeri latini.</p>";
echo "<p align='center'>Il numero civico va inserito nell'apposito campo.</p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
$flag_controllo = 1;
break;
}
}
if ($chiave == 'txt_ente_cap') {
if (!chkCAP($valore) ) {
echo "<p align='center'>Il campo 'CAP' non è stato digitato correttamente</p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
$flag_controllo = 1;
break;
}
}
}
}
}
//Procedo all'aggiornamento del Database
if ($flag_controllo == 0) {
$filtro = $_SESSION ['ID'];
$ente = trim ($_POST['txt_ente']);
$ente = ( get_magic_quotes_gpc() ) ? stripslashes($ente) : $ente;
$ente = mysql_real_escape_string ($ente);
$tiPICF = trim ($_POST['txt_ente_tipoPICF']);
$tiPICF = ( get_magic_quotes_gpc() ) ? stripslashes($tiPICF) : $tiPICF;
$tiPICF = mysql_real_escape_string ($tiPICF);
$PICF = trim ($_POST['txt_ente_PICF']);
$PICF = mb_strtoupper($PICF);
$PICF = ( get_magic_quotes_gpc() ) ? stripslashes($PICF) : $PICF;
$PICF = mysql_real_escape_string ($PICF);
$via = trim ($_POST['txt_ente_via']);
$via = ( get_magic_quotes_gpc() ) ? stripslashes($via) : $via;
$via = mysql_real_escape_string ($via);
$civico = trim ($_POST['txt_ente_civico']);
$civico = ( get_magic_quotes_gpc() ) ? stripslashes($civico) : $civico;
$civico= mysql_real_escape_string ($civico);
$comune = trim ($_POST['txt_ente_comune']);
$comune = ( get_magic_quotes_gpc() ) ? stripslashes($comune) : $comune;
$comune = mysql_real_escape_string ($comune);
$cap = trim ($_POST['txt_ente_cap']);
$cap = ( get_magic_quotes_gpc() ) ? stripslashes($cap) : $cap;
$cap = mysql_real_escape_string ($cap);
$email = trim($_POST['txt_ente_email']);
$email = ( get_magic_quotes_gpc() ) ? stripslashes($email) : $email;
$email = mysql_real_escape_string ($email);
$telefono = trim ($_POST['txt_ente_telefono']);
$telefono = ( get_magic_quotes_gpc() ) ? stripslashes($telefono) : $telefono;
$telefono = mysql_real_escape_string ($telefono);
$fax = trim($_POST['txt_ente_fax']);
$fax = ( get_magic_quotes_gpc() ) ? stripslashes($fax) : $fax;
$fax = mysql_real_escape_string ($fax);
$ins = 0;
//Verifico se si tratta di un aggiornamento oppure di un inserimento
$sql_test = sprintf("SELECT IDente, IDlogin FROM fas_enti WHERE IDlogin=%d",$filtro);
$test = mysql_query($sql_test) or Die(mysql_error() );
$test_row = mysql_num_rows($test);
if ($test_row >0 ) {
$ins = 1;
}
if ($ins == 1) {
//Costruisco la query di aggiornamento
$sql = sprintf("UPDATE fas_enti SET ente='%s', ente_tipoPICF='%s', ente_PICF='%s', ente_via='%s', ente_civico='%s', ente_comune='%s', ente_cap='%s', ente_email='%s', ente_telefono='%s', ente_fax='%s' WHERE IDlogin=%d",$ente, $tiPICF, $PICF, $via, $civico, $comune, $cap, $email, $telefono, $fax, $filtro);
} else { //Costruisco la query di inserimento
$sql = sprintf("INSERT INTO fas_enti (IDlogin, ente, ente_tipoPICF, ente_PICF, ente_via, ente_civico, ente_comune, ente_cap, ente_email, ente_telefono, ente_fax)VALUES(%d,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",$filtro, $ente, $tiPICF, $PICF, $via, $civico, $comune, $cap, $email, $telefono, $fax);
}
$eseguo = mysql_query ($sql) or die(mysql_error() );
if ($eseguo) {
echo "<p align='center'><span class='rosso'>Operazione effettuata con successo.</span></p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
} else {
echo "<p align='center'><span class='rosso'>Errore inaspettato.</span></p>";
echo "<p align='center'></p>";
echo "<p align='center'><a href='dati_utente.php' target='_self'><strong>Torna indietro</strong></a></p>";
}
}
?>
<!-- end .content -->
</div>
<div id="footer">
<p>Footer</p>
<!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>
<?php
ob_end_flush();
mysql_close();
?>
Ora vorrei poter riuscire a far si che, ritornando sulla pagina contenente la form, l'utente si ritrovasse almeno i dati compilati correttamente. Si può realizzare con php?
Questo è il file delle funzioni che vengono richiamate
Codice PHP:
//validazione sintattica indirizzo e-mail
function chkEmail($email)
{
// elimino spazi, "a capo" e altro alle estremità della stringa
$email = trim($email);
// se la stringa è vuota sicuramente non è una mail
if(!$email) {
return false;
}
// controllo che ci sia una sola @ nella stringa
$num_at = count(explode( '@', $email )) - 1;
if($num_at != 1) {
return false;
}
// controllo la presenza di ulteriori caratteri "pericolosi":
if(strpos($email,';') || strpos($email,',') || strpos($email,' ')) {
return false;
}
// la stringa rispetta il formato classico di una mail?
if(!preg_match( '/^[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}$/', $email)) {
return false;
}
return true;
}
//Verifica numeri telefono, fax, cellulare, etc.
function chkNumTel ($numtel) {
if (!preg_match ("/^[0-9\-\/' ]+$/", $numtel) ) {
return false;
} else {
return true;
}
}
//Verifica denominazioni, nominativi, etc. (campo obbligatorio)
function chkDenom ($denom) {
if (strlen($denom) < 1) {
return false;
}
if (!preg_match ("/^[A-Za-z0-9' \"\-]+$/", $denom) ) {
return false;
}
return true;
}
//Verifica Codice fiscale /Partita IVA
function chkCFPI ($CDF,$CP) {
if ($CP === 'CF') {
if (!preg_match ("/^[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[a-zA-Z][0-9]{3}[a-zA-Z]$/", $CDF) ) {
return false;
}
}
if ($CP === 'PI') {
if (!preg_match ("/^[0-9]{11}$/",$CDF) ) {
return false;
}
}
return true;
}
//Verifica Indirizzo
function chkInd ($Ind) {
if (!preg_match ("/^[a-zA-Z' \"\-\(\)]+$/", $Ind) ) {
return false;
}
return true;
}
//Verifica CAP
function chkCAP ($cod) {
if (!preg_match ("/^[0-9]{5}$/", $cod) ) {
return false;
} else {
return true;
}
}
?>
Dal punto di vista della sicurezza, il codice che ho implementato vi sembra abbastanza robusto? (forse quest'ultima domanda varrebbe un altro thread).
Ringrazio anticipatamente chi, più esperto di me (ho iniziato da poco con php) può darmi dei suggerimenti.