problema doppi inviiiiiiii con <?php echo $_SERVER['PHP_SELF']; ?>

searedone

Utente Attivo
11 Giu 2010
508
0
0
ciao a tutti,
qualcuno mi sa dire come posso bloccare i doppi invii, causa refreash pagina ecc ecc con <?php echo $_SERVER['PHP_SELF']; ?>

PHP:
<?php  
include 'dbc.php'; 
page_protect(); 


?> 
<html> 
<head> 
<title>My Account</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 

<link href="styles.css" rel="stylesheet" type="text/css"> 
</head> 

<body> 
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main"> 
  <tr>  
    <td colspan="3">&nbsp;</td> 
  </tr> 
  <tr>  
    <td width="160" valign="top"> 
<?php  
/*********************** MYACCOUNT MENU **************************** 
This code shows my account menu only to logged in users.  
Copy this code till END and place it in a new html or php where 
you want to show myaccount options. This is only visible to logged in users 
*******************************************************************/ 
if (isset($_SESSION['user_id'])) {?> 
<div class="myaccount"> 
  <p><strong>My Account</strong></p> 
  <a href="myaccount.php">My Account</a><br> 
  <a href="mysettings.php">Settings</a><br> 
    <a href="logout.php">Logout </a> 
     
  <p>You can add more links here for users</p></div> 
<?php } 
if (checkAdmin()) { 
/*******************************END**************************/ 
?> 
      <p> <a href="admin.php">Admin CP </a></p> 
      <?php } ?> 
      <p>&nbsp;</p> 
      <p>&nbsp;</p> 
      <p>&nbsp;</p></td> 
    <td width="732" valign="top"><p>&nbsp;</p> 
      <h3 class="titlehdr">Welcome <?php echo $_SESSION['user_name'];?></h3>   
      <?php     
      if (isset($_GET['msg'])) { 
      echo "<div class=\"error\">$_GET[msg]</div>"; 
      } 
             
      ?> 
      <p>This is the my account page</p> 
       
      <a href="accetta.php" />Controlla se hai richieste d'amicizia</a> 
       
      <br /> 
      <br /> 
       
      <br /> 
<?php echo $_SESSION['user_id'];   
require ("db_connect.php");  


$user_id = $_POST['user_id'];  



$commento=$_POST["commento"]; 
$commento=ereg_replace("\n","<br>",$commento);  

if(get_magic_quotes_gpc()) {  


$commento=stripslashes($commento);  
}  

$commento =mysql_real_escape_string($commento);  

$insert = mysql_query("INSERT INTO annunci SET 
commento='$commento', user_id=".$_SESSION['user_id']."");  

  


?> 
      
 <div class="content"> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
      
      <p> 
        <label for="commento"></label> 
        <textarea name="commento" id="commento" cols="55" rows="20"></textarea> 
      </p> 
      <p> 
        <input type="submit" name="submit" id="submit" value="Invia Commento" /> 
      </p> 
    </form> 
    </div> 
     
     
   <?php  
    
    
require ("db_connect.php");  



$result = mysql_query("SELECT * FROM annunci, users WHERE user_id=".$_SESSION['user_id']. " AND  user_id=users.id 
");  


//recuperiamo i dati da stampare  


while ($records = mysql_fetch_array($result)) {   


$commento = $records['commento']; 
$user_name = $records['user_name']; 
$ts = $records['ts']; 
 echo "<a href=\"myprofile.php?user_name=$user_name\">$user_name</a>";  
echo "<h2> <img src='immagini/.gif' width='30' height='50' /> $user_name</h2> ";  
echo "<h6>$commento</h6> ";  
echo "<h6>$ts</h6> ";  



}  

?> 


   
      
      
      </td> 
    <td width="196" valign="top">&nbsp;</td> 
  </tr> 
  <tr>  
    <td colspan="3">&nbsp;</td> 
  </tr> 
</table> 

</body> 
</html>
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
ciao
non c'entra $_SERVER['PHP_SELF'] in quanto è solo un modo (giusto) per far puntare il form alla pagina stessa, per esempio non scrivendo l'action otterresti lo stesso risultato
intanto metti una verifica per vedere se è stato premuto il submit
PHP:
<?php
//....
echo $_SESSION['user_id'];    
require ("db_connect.php");   
$user_id = $_POST['user_id']; //questo POST da dove salta fuori?? non lo vedo nel form  
//poi metti un controllo del submit
if(isset($_POST['submit'])){//****verifico se è stato premuto il submit
	$commento=$_POST["commento"];  
	$commento=ereg_replace("\n","<br>",$commento); //ereg_replace (come tutti gli ereg) è deprecato usa preg_replace  
	if(get_magic_quotes_gpc()) {
		$commento=stripslashes($commento);   
	}   
	$commento =mysql_real_escape_string($commento);   
	$insert = mysql_query("INSERT INTO annunci SET  commento='$commento', user_id=".$_SESSION['user_id']."");   
}//fine if submit - penso che dovrebbe finire qui, comunque alla fine di tutte le azioni che sosno "azionate" dal form
?>    
 <div class="content">
 <!-- metti per sicurezza htmlspecialchars --> 
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">  
      <p>  
        <label for="commento"></label>  
        <textarea name="commento" id="commento" cols="55" rows="20"></textarea>  
      </p>  
      <p>  
        <input type="submit" name="submit" id="submit" value="Invia Commento" />  
      </p>  
    </form>  
    </div>  
//.....
poi per impedire il doppio submit dai un occhio qui
https://www.mrw.it/javascript/articoli/impedire-doppio-submit-form-javascript_693.html
 

searedone

Utente Attivo
11 Giu 2010
508
0
0
Funziona,
ma se faccio refresh sulla pagina senza schiacciare il submit mi ripete il post :(
 

searedone

Utente Attivo
11 Giu 2010
508
0
0
Ho provato anche così...

ma peggio .....

PHP:
<?php echo $_SESSION['user_id'];  
require ("db_connect.php"); 


$user_id = $_POST['user_id']; 



$commento=$_POST["commento"];
$commento=ereg_replace("\n","<br>",$commento); 

if(get_magic_quotes_gpc()) { 


$commento=stripslashes($commento); 
} 

$commento =mysql_real_escape_string($commento); 

$insert = mysql_query("INSERT INTO annunci SET
commento='$commento', user_id=".$_SESSION['user_id'].""); 

 


?>
	 
 <div class="content">
    <form name="modulo">
<input type="text" name="commento">
<input type="button" name="bottone" value="Invia" onclick="Invia()">
</form>
    </div>
<?php    
    function Invia()
{
    var testo = document.modulo.testo.value;
    if (testo == "")
    {
        alert("Il campo è obbligatorio!");
    }
  else
{
    document.modulo.bottone.value = "Invio in corso...";
    document.modulo.bottone.disabled = true;
    document.modulo.method = "post";
    document.modulo.action = "esegui.php";
    document.modulo.submit();
 }
}
?>    
    
   <?php 
   
   
require ("db_connect.php"); 



$result = mysql_query("SELECT * FROM annunci, users WHERE user_id=".$_SESSION['user_id']. " AND  user_id=users.id 
"); 


//recuperiamo i dati da stampare 


while ($records = mysql_fetch_array($result)) {  


$commento = $records['commento'];
$user_name = $records['user_name'];
$ts = $records['ts'];
 echo "<a href=\"myprofile.php?user_name=$user_name\">$user_name</a>"; 
echo "<h2> <img src='immagini/.gif' width='30' height='50' /> $user_name</h2> "; 
echo "<h6>$commento</h6> "; 
echo "<h6>$ts</h6> "; 



} 

?>
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
ciao
ho commentato alcune istruzioni perchè non ho il tuo db e la sessione per l'id,
comunque leggi la nota che ho messo tra /* */

PHP:
<?php 
ob_start();
//.... 
//echo $_SESSION['user_id'];     
//require ("db_connect.php");    
//$user_id = $_POST['user_id']; //questo POST da dove salta fuori?? non lo vedo nel form   
//poi metti un controllo del submit 
if(isset($_POST['submit']) && $_POST['submit']="Invia Commento"){//****verifico se è stato premuto il submit 
    $commento=$_POST["commento"];   
    $commento=ereg_replace("\n","<br>",$commento); //ereg_replace (come tutti gli ereg) è deprecato usa preg_replace   
    if(get_magic_quotes_gpc()) { 
        $commento=stripslashes($commento);    
    }    
    //$commento =mysql_real_escape_string($commento);    
    //$insert = mysql_query("INSERT INTO annunci SET  commento='$commento', user_id=".$_SESSION['user_id']."");
	echo  $commento."<br>";//questo poi lo togli, l'ho messo per vedere il commento
	/*
	nell'header refresc ho messo 5 per vedere l'effetto dell'echo, tu prova a mettere 0 (zero), in questo modo
	ti riporta alla pagina bianca prima che tu abbia il tempo di dare il refresh dal browswer, i campi del form si vuotano
	poi eventualmente metti un if a commento del tipo
	if($commeto == ""){
	header("refresh:0;url=".htmlspecialchars($_SERVER['PHP_SELF'])."");
	}
	impedendo così (almeno spero) che ti inserisca un commento vuoto
	*/
	header("refresh:5;url=".htmlspecialchars($_SERVER['PHP_SELF'])."");//qui al posto di 5 metti 0
	
}//fine if submit - penso che dovrebbe finire qui, comunque alla fine di tutte le azioni che sosno "azionate" dal form 
?>     
 <div class="content"> 
 <!-- metti per sicurezza htmlspecialchars -->  
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">   
      <p>   
        <label for="commento"></label>   
        <textarea name="commento" id="commento" cols="55" rows="20"></textarea>   
      </p>   
      <p>   
        <input type="submit" name="submit" id="submit" value="Invia Commento" />   
      </p>   
    </form>   
    </div>   
<?php
ob_end_flush();
?>
fai delle prove e sappimi dire
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
ciao
mi sembra che tu abbia fatto un po' di confusione la funzione che hai creato (l'hai presa dal link che ti avevo postato?) sia in js come istruzioni e non in php, se volevi fre un js dovevi inserilo tra i tag
<script type="text/javascript">...</script>
 
Discussioni simili
Autore Titolo Forum Risposte Data
F PHP problema doppi apici all'interno di una query SQL PHP 1
L Problema paginazione che ripete i dati doppi PHP 1
I Sto progettando nuovi siti utilizzando bootstrap e devo dire funziona bene, l'unico problema e la maschera -moz- HTML e CSS 0
K Problema form update PHP 2
O problema con dvr dahua xvr5116 IP Cam e Videosorveglianza 0
S Problema nel ciclare un json Javascript 0
G Problema con Xampp Web Server 1
andrea barletta Problema con miniature comandi Photoshop 0
I problema con alice Posta Elettronica 0
K Problema Inner join PHP 1
F firefox problema http Linux e Software 0
N Problema con position absolute e overflow HTML e CSS 4
E Problema jquery Success jQuery 2
L Problema con inner join PHP 11
K [php] Problema con inner join PHP 4
E problema selezione sfumata Photoshop 2
K [PHP] Problema con variabili concatenate. PHP 1
A Problema filtro fluidifica Photoshop Photoshop 1
H Problema Bordi Scontorno Photoshop 1
O problema con query PHP 4
R Problema installazione Realtek WiFi USB rtl8821 Reti LAN e Wireless 0
I problema con 2 account Posta Elettronica 1
L problema collegamento file css con html HTML e CSS 1
Y Problema percorso file in rete PHP 1
N Problema SEO "L'URL non si trova su Google" SEO e Posizionamento 4
E Problema accesso a file con app sviluppata con MIT APP INVENTOR 2 Sviluppo app per Android 0
P Problema acquisizione clienti Webdesign e Grafica 1
F NetBeans problema creazione progetto Java Windows e Software 0
M Problema con Try Catch PHP 0
C problema seo + cerco esperto SEO e Posizionamento 11
Sergio Unia Problema con gli eventi del mouse su una data table: Javascript 2
T PROBLEMA CON SESSIONI PHP 3
A Problema, non so, di scale() o transform, oppure altro? HTML e CSS 0
T ALTRO PROBLEMA CON ARRAY PHP PHP 1
R problema con else PHP 0
T PROBLEMA CON ARRAY PHP 8
L problema con query select PHP 2
R Problema query con ricerca id numerico PHP 2
F Problema con risposta PHP 0
S problema con recupero dati tabella mysql PHP 2
Z Problema con il mio tp-l i nk Reti LAN e Wireless 1
I PROBLEMA: Sostituzione sito XAMPP E-Commerce 0
T problema data 30/11/-1 PHP 0
L Problema RAM con Tomcat 8 Apache 0
napuleone problema con sort e asort PHP 4
Y Problema incolonnamento tabella PHP 7
S problema salvataggio immagini Photoshop 0
Z Problema con INT MySQL PHP 1
Z Problema database MySQL con XAMPP PHP 0
M Problema con controllo form in real time jQuery 6

Discussioni simili