Trattare con i Form

Una delle caratteritiche più forti di PHP è il modo in cui gestisce i form. Il concetto da comprendere principalmente è che qualsiasi elemento di un form sarà automaticamente disponibile per i vostri script PHP. Per maggiori informazioni ed esempi relativi all'utilizzo dei form consultate la sezione del manuale che si riferisce a le Variabili da sorgenti esterne. A seguire un esempio di un form HTML:

Example #1 Un semplice form HTML

<form action="action.php" method="POST">
 <p>Il tuo Nome: <input type="text" name="name" value="" /></p>
 <p>La tua età: <input type="text" name="age" value ="" /></p>
 <p><input type="submit"></p>
</form>

Questo form non ha niente di speciale. È un semplice form in HTML che non presenta nessun tipo di tags particolari. Quando l'utente riempie questo form e preme il pulsante submit, viene richiamata la pagina action.php. In questo file il risultato sarà qualcosa di simile:

Example #2 La stampa video di dati dal nostro form

Ciao <?php echo htmlspecialchars($_POST['name']); ?>.
La tua età è di <?php echo (int)$_POST['age']; ?> anni.

Ecco un possibile output di questo script:

Ciao Joe. La tua età è di 22 anni.

A parte le parti relative a htmlspecialchars() e (int), ciò che avviene dovrebbe risultare ovvio. htmlspecialchars() si assicura che qualsiasi carattere speciale in HTML sia propriamente codificato in modo che la gente non possa iniettare tag HTML o Javascript nella vostra pagina. Per il campo age, in quanto sappiamo che è un numero, possiamo semplicemente convertirlo in un int il quale si sbarazzerà automaticamente di qualsiasi carattere strano. Puoi anche fare questo automaticamente con PHP utilizzando l'estensione filter. Le variabili $_POST["name"] e $_POST["age"] vengono impostate automaticamente dal PHP. Prima avevamo usato la variabile autoglobal $_SERVER, ora invece abbiamo introdotto la variabile autoglobal $_POST che contiene tutti i dati di tipo POST. Notate che il metodo del nostro form è il POST. Se usassimo il metodo GET le informazioni ricavate dal nostro form si troverebbero invece in $_GET. Si può anche usare la variabile autoglobale $_REQUEST se la provenienza dei dati richiesti non ci interessa. Questa variabile contiene un misto di dati GET, POST e COOKIE.

Si può anche trattare con gli input XForms input in PHP, anche se vi troverete a vostro agio con i form HTML ben supportati per parecchio tempo. Nonostante lavorare con XForms non sia per principianti, si potrebbe essere interessati a loro. Abbiamo anche una breve introduzione per gestire i dati ricevuti da XForms nella nostra sezione features.

add a note add a note

User Contributed Notes 3 notes

up
162
sethg at ropine dot com
20 years ago
According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached.

You should use the GET method when your form is, well, getting something off the server and not actually changing anything.  For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.
up
62
Johann Gomes (johanngomes at gmail dot com)
13 years ago
Also, don't ever use GET method in a form that capture passwords and other things that are meant to be hidden.
up
25
nucc1
6 years ago
worth clarifying:

POST is not more secure than GET.

The reasons for choosing GET vs POST involve various factors such as intent of the request (are you "submitting" information?), the size of the request (there are limits to how long a URL can be, and GET parameters are sent in the URL), and how easily you want the Action to be shareable -- Example, Google Searches are GET because it makes it easy to copy and share the search query with someone else simply by sharing the URL.

Security is only a consideration here due to the fact that a GET is easier to share than a POST. Example: you don't want a password to be sent by GET, because the user might share the resulting URL and inadvertently expose their password.

However, a GET and a POST are equally easy to intercept by a well-placed malicious person if you don't deploy TLS/SSL to protect the network connection itself.

All Forms sent over HTTP (usually port 80) are insecure, and today (2017), there aren't many good reasons for a public website to not be using HTTPS (which is basically HTTP + Transport Layer Security).

As a bonus, if you use TLS  you minimise the risk of your users getting code (ADs) injected into your traffic that wasn't put there by you.
To Top