Visualizzazione dei risultati da 1 a 6 su 6

Discussione: funzioni - php

  1. #1

    funzioni - php

    Qualcuno mi sa dire come mai mi si avviano solo le prime due funzioni quando apro questa pagina????
    ecco il codice:




    <html>
    <head>
    </head>
    <body>
    <?php
    /*mi si avviano le funzioni 4 e 5 da sole...perchè????*/

    $func5 = rate5();
    function rate5(){
    $text = "text.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 5!";
    fwrite ($fu,$string);
    fclose($fu);
    header('location:indexa.php');
    }

    $func4 = rate4();
    function rate4(){
    $text = "text.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 4!";
    fwrite ($fu,$string);
    fclose($fu);
    header('location:indexa.php');
    }

    $func3 = rate3();
    function rate3(){
    $text = "rates.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 3!";
    fwrite ($fu,$string);
    fclose($fu);
    header('location:indexa.php');
    }

    $func2 = rate2();
    function rate2(){
    $text = "rates.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 2!";
    fwrite ($fu,$string);
    fclose($fu);
    header('location:indexa.php');
    }

    $func1 = rate1();
    function rate1(){
    $text = "rates.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 1!";
    fwrite ($fu,$string);
    fclose($fu);
    header('location:indexa.php');
    }


    function brum(){return 'sono brum';}
    $sbam = brum();
    print ("eccolo $sbam ");


    ?>
    </body>
    </html>

  2. #2
    Perchè alla fine di ogni funzione metti header('location:indexa.php'); il problema sta li.
    Non puoi eseguire il redirect alla fine del file?
    CODENCODE \ Branding \ Design \ Marketing
    www.codencode.it

  3. #3
    ho provato così come hai detto tu...ma niente sempre lo stesso risultato...sul documento di testo vengono scritte solo le stringhe della prima e della seconda funzione...
    <?php
    $func5 = rate5();
    function rate5(){
    $text = "text.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 5!";
    fwrite ($fu,$string);
    fclose($fu);
    }

    $func4 = rate4();
    function rate4(){
    $text = "text.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 4!";
    fwrite ($fu,$string);
    fclose($fu);
    }

    $func3 = rate3();
    function rate3(){
    $text = "rates.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 3!";
    fwrite ($fu,$string);
    fclose($fu);
    }

    $func2 = rate2();
    function rate2(){
    $text = "rates.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 2!";
    fwrite ($fu,$string);
    fclose($fu);
    }

    $func1 = rate1();
    function rate1(){
    $text = "rates.txt";
    $fu = fopen ($text,"a+") or die ("sorry, but the vote system is currently not working");
    $string = "clash of the olympians Voted 1!";
    fwrite ($fu,$string);
    fclose($fu);
    }

    header('location:indexa.php');

    ?>

  4. #4
    Utente di HTML.it L'avatar di Laxus
    Registrato dal
    Oct 2010
    Messaggi
    251
    codencode, anche dopo l'header il codice continua l'eseguzione
    @sharkdrew, tutte e 5 le funzioni vengono eseguite; può darsi che tu ti aspetti 5 dati su text.txt? in questo caso, le prime due funzioni salvano in text.txt, mentre le ultime 3 in rates.txt

    comunque, a che serve scrivere funzioni se poi le riscrivi tutte le volte?
    Codice PHP:
    <?php
         
    function rate($id){
         
    $string "clash of the olympians Voted $id!";
         
    $f fopen ("text.txt","a+") or die ("sorry, but the vote system is currently not working");
         
    fwrite ($f,$string);
         
    fclose($f);
         
    //header('location:indexa.php'); // ociò, viene eseguito 5 volte!
    }

    rate(1);
    rate(2);
    rate(3);
    rate(4);
    rate(5);

    header('location:indexa.php');

    ?>

  5. #5
    grazie laxus ho capito ... comunque l'ho fatto x questo motivo:
    ho in un'altra pagina due link dove il primo dovrebbe (tramite get) richiamare
    la funzione x esempio 5 e quindi sul documento di testo verrà scritto voted 5
    e l'altro richiama un'altra funzione che scriverà voted 4 sul documento...x questo avevo fatto più funzioni e non così come me l'hai (giustamento) semplificato tu...
    ho da poco cominciato a fare php quindi scusa l'ignoranza!

  6. #6
    Laxus lo so che lo script continua, infatti per terminare lo script bisognerebbe inserire un exit(), e sinceramente non avevo notato il codice ripetuto.
    CODENCODE \ Branding \ Design \ Marketing
    www.codencode.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.