Manuale PHP Stig Sæther Bakken Alexander Aulbach Egon Schmid Jim Winstead Lars Torben Wilson Rasmus Lerdorf Andrei Zmievski Jouni Ahto A cura di Luca Perugini Simone Cortesi
1. Manuale PHP Stig Sther Bakken Alexander Aulbach Egon Schmid
Jim Winstead Lars Torben Wilson Rasmus Lerdorf Andrei Zmievski
Jouni Ahto A cura di Luca Perugini Simone Cortesi
2. Tradotto con la collaborazione di: Marco Cucinato Massimo
Colombo Marco De Nittis Darvin Andrioli Fabio Gandola Sergio
Marchesini Alan DAngelo Giacomo Tesio Marco Spisto Gabriele Scaroni
Mariano Calandra Rocco Curcio Luca Costantino 21-05-2002 Copyright
1997, 1998, 1999, 2000, 2001, 2002 Gruppo di Documentazione PHP
Copyright Questo manuale Copyright 1997, 1998, 1999, 2000, 2001,
2002 del Gruppo di Documentazione PHP. I membri di questo gruppo
sono elencati nella copertina di questo manuale. Questo manuale pu
essere redistribuito secondo i termini della GNU General Public
License come pubblicata dalla Free Software Foundation; sia la
versione 2 della licenza, oppure (a scelta) qualsiasi versione
successiva. La sezione Estendere PHP 4.0 di questo manuale
copyright 2000 della Zend Technologies, Ltd. Questo materiale pu
essere distribuito solo secondo i termini e le condizioni della
Open Publication License, v1.0 o successiva (la versione pi recente
al momento disponibile qui
http://www.opencontent.org/openpub/).
3. Manuale PHP Stig Sther Bakken, Alexander Aulbach, Egon
Schmid, Jim Winstead, Lars Torben Wilson, Rasmus Lerdorf, Andrei
Zmievski, e Jouni Ahto A cura di Luca Perugini A cura di Simone
Cortesi Tradotto con la collaborazione di: Marco Cucinato Massimo
Colombo Marco De Nittis Darvin Andrioli Fabio Gandola Sergio
Marchesini Alan DAngelo Giacomo Tesio Marco Spisto Gabriele Scaroni
Mariano Calandra Rocco Curcio Luca Costantino Pubblicato 21-05-2002
Copyright 1997, 1998, 1999, 2000, 2001, 2002 Gruppo di
Documentazione PHP Copyright Questo manuale Copyright 1997, 1998,
1999, 2000, 2001, 2002 del Gruppo di Documentazione PHP. I membri
di questo gruppo sono elencati nella copertina di questo manuale.
Questo manuale pu essere redistribuito secondo i termini della GNU
General Public License come pubblicata dalla Free Software
Foundation; sia la versione 2 della licenza, oppure (a scelta)
qualsiasi versione successiva. La sezione Estendere PHP 4.0 di
questo manuale copyright 2000 della Zend Technologies, Ltd. Questo
materiale pu essere distribuito solo secondo i termini e le
condizioni della Open Publication License, v1.0 o successiva (la
versione pi recente al momento disponibile qui
http://www.opencontent.org/openpub/).
4. Sommario Prefazione
.............................................................................................................................................i
I. Guida
Rapida...................................................................................................................................1
1. Introduzione
............................................................................................................................1
Che cos il
PHP?..............................................................................................................2
What can PHP
do?.............................................................................................................2
A brief history of
PHP.......................................................................................................4
2. Installazione
............................................................................................................................5
General Installation Considerations
..................................................................................6
Unix/HP-UX installs
.........................................................................................................6
Unix/Linux
installs............................................................................................................8
Using Packages
........................................................................................................8
Unix/Mac OS X
installs.....................................................................................................8
Using Packages
........................................................................................................8
Compiling for OS X
server......................................................................................8
Compiling for MacOS X
client..............................................................................10
Unix/OpenBSD installs
...................................................................................................10
Using
Ports.............................................................................................................11
Using Packages
......................................................................................................11
Unix/Solaris
installs.........................................................................................................11
Required software
..................................................................................................11
Using Packages
......................................................................................................12
Installation on UNIX systems
.........................................................................................12
Apache Module Quick
Reference..........................................................................12
Building
.................................................................................................................13
Installation on Windows systems
....................................................................................13
Windows InstallShield
...........................................................................................13
Manual Installation
Steps.......................................................................................14
Building from source
.............................................................................................16
Preparations..................................................................................................16
Putting it all
together....................................................................................17
Compiling.....................................................................................................18
Installation of Windows extensions
.......................................................................18
Servers-CGI/Commandline
.............................................................................................20
Testing....................................................................................................................21
Benchmarking........................................................................................................21
Servers-Apache................................................................................................................21
Details of installing PHP with Apache on Unix
....................................................21 Installing
PHP on Windows with Apache
1.3.x.....................................................24
Servers-Caudium
.............................................................................................................25
Servers-fhttpd
..................................................................................................................25
Servers-IIS/PWS..............................................................................................................26
Windows and PWS/IIS 3
.......................................................................................26
Windows and PWS 4 or newer
..............................................................................27
Windows NT/2000/XP and IIS 4 or
newer............................................................27
Servers-Netscape and iPlanet
..........................................................................................28
Installing PHP with Netscape on Sun
Solaris........................................................28
Installing PHP with Netscape on Windows
...........................................................31
Servers-OmniHTTPd Server
...........................................................................................32
OmniHTTPd 2.0b1 and up for
Windows...............................................................32
iv
5. Servers-Oreilly Website Pro
............................................................................................33
Oreilly Website Pro 2.5 and up for Windows
........................................................33
Servers-Xitami.................................................................................................................33
Xitami for
Windows...............................................................................................33
Servers-Other web
servers...............................................................................................34
Problems?
........................................................................................................................34
Read the
FAQ.........................................................................................................34
Other
problems.......................................................................................................34
Bug
reports.............................................................................................................34
Complete list of congure options
..................................................................................34
Congure Options in PHP 4
..................................................................................35
Database options
..........................................................................................35
Graphics
options...........................................................................................38
Misc options
.................................................................................................39
PHP
options..................................................................................................45
Server
options...............................................................................................46
XML options
................................................................................................47
3. Conguration
........................................................................................................................48
The conguration le
......................................................................................................49
General Conguration Directives
..........................................................................50
Safe Mode Conguration
Directives......................................................................55
Debugger Conguration Directives
.......................................................................55
Extension Loading
Directives................................................................................56
mSQL Conguration Directives
............................................................................56
Postgres Conguration Directives
.........................................................................56
SESAM Conguration
Directives..........................................................................57
Sybase Conguration
Directives............................................................................57
Sybase-CT Conguration Directives
.....................................................................57
Informix Conguration
Directives.........................................................................58
BC Math Conguration Directives
........................................................................59
Browser Capability Conguration
Directives........................................................59
Multi-Byte String Conguration Directives
..........................................................59 Exif
Conguration Directives
................................................................................60
4. Security
.................................................................................................................................62
General considerations
....................................................................................................63
Installed as CGI
binary....................................................................................................63
Possible
attacks......................................................................................................64
Case 1: only public les served
.............................................................................64
Case 2: using
--enable-force-cgi-redirect...............................................................64
Case 3: setting doc_root or user_dir
......................................................................65
Case 4: PHP parser outside of web
tree.................................................................65
Installed as an Apache
module........................................................................................66
Filesystem Security
.........................................................................................................66
Database Security
............................................................................................................68
Designing
Databases..............................................................................................69
Connecting to Database
.........................................................................................69
Encrypted Storage Model
......................................................................................69
SQL
Injection.........................................................................................................70
Avoiding
techniques.....................................................................................73
Error
Reporting................................................................................................................74
Using Register
Globals....................................................................................................75
User Submitted
Data........................................................................................................77
v
6. Hiding
PHP......................................................................................................................77
Keeping Current
..............................................................................................................78
II. Struttura del
Linguaggio.............................................................................................................79
5. Sintassi
Fondamentale...........................................................................................................79
Modi per uscire dalla modalit
HTML............................................................................80
Separazione delle
istruzioni.............................................................................................81
Commenti
........................................................................................................................81
6. Types
.....................................................................................................................................82
Introduction
.....................................................................................................................83
Booleans
..........................................................................................................................83
Syntax
....................................................................................................................83
Converting to
boolean............................................................................................84
Integers
............................................................................................................................85
Syntax
....................................................................................................................85
Integer overow
.....................................................................................................85
Converting to
integer..............................................................................................86
From
booleans..............................................................................................86
From oating point
numbers........................................................................86
From
strings..................................................................................................87
From other
types...........................................................................................87
Floating point
numbers....................................................................................................87
Strings..............................................................................................................................88
Syntax
....................................................................................................................88
Single
quoted................................................................................................88
Double quoted
..............................................................................................89
Heredoc
........................................................................................................89
Variable
parsing............................................................................................91
Simple
syntax......................................................................................91
Complex (curly) syntax
......................................................................91
String access by character
............................................................................92
Useful
functions.....................................................................................................93
String conversion
...................................................................................................93
Arrays
..............................................................................................................................94
Syntax
....................................................................................................................94
Specifying with
array().................................................................................94
Creating/modifying with square-bracket syntax
..........................................94 Useful
functions.....................................................................................................95
Array dos and
donts.............................................................................................95
Why is $foo[bar]
wrong?.........................................................................95
So why is it bad
then?.........................................................................96
Examples................................................................................................................96
Objects...........................................................................................................................100
Object Initialization
.............................................................................................100
Resource
........................................................................................................................100
Freeing resources
.................................................................................................100
NULL
............................................................................................................................101
Syntax
..................................................................................................................101
Type Juggling
................................................................................................................101
Type Casting
........................................................................................................102
7.
Variables..............................................................................................................................104
Basics.............................................................................................................................105
vi
7. Predened
variables.......................................................................................................106
Variable
scope................................................................................................................107
Variable variables
..........................................................................................................110
Variables from outside
PHP...........................................................................................110
HTML Forms (GET and
POST)..........................................................................111
IMAGE SUBMIT variable
names..............................................................112
HTTP Cookies
.....................................................................................................112
Environment variables
.........................................................................................112
Dots in incoming variable
names.........................................................................113
Determining variable
types..................................................................................113
8. Costanti
...............................................................................................................................114
Sintassi...........................................................................................................................115
Costanti
predenite........................................................................................................116
9. Expressions
.........................................................................................................................118
10. Operatori
...........................................................................................................................122
Operatori aritmetici
.......................................................................................................123
Operatori di
assegnazione..............................................................................................123
Operatori bitwise
...........................................................................................................124
Operatori di confronto
...................................................................................................124
Operatori di controllo errori
..........................................................................................125
Operatori di esecuzione
.................................................................................................126
Operatori di
incremento/decremento.............................................................................126
Operatori
logici..............................................................................................................127
Precedenza degli
operatori.............................................................................................128
Operatori di
stringa........................................................................................................129
11. Strutture di controllo
.........................................................................................................130
if...................................................................................................................................131
else
..............................................................................................................................131
elseif
..........................................................................................................................132
Sintassi alternativa per le strutture di
controllo.............................................................132
while
............................................................................................................................133
do..while....................................................................................................................134
for.................................................................................................................................135
foreach........................................................................................................................137
break
............................................................................................................................139
continue......................................................................................................................139
switch
..........................................................................................................................140
declare........................................................................................................................142
Ticks.....................................................................................................................143
return..............................................................................................................................144
require()
.........................................................................................................................144
include().........................................................................................................................145
require_once()................................................................................................................149
include_once()
...............................................................................................................149
12.
Functions...........................................................................................................................150
User-dened
functions...................................................................................................151
Function arguments
.......................................................................................................151
Making arguments be passed by reference
..........................................................151
Default argument values
......................................................................................152
Variable-length argument lists
.............................................................................153
Returning
values............................................................................................................153
old_function
.............................................................................................................154
vii
8. Variable
functions..........................................................................................................154
13. Classi e Oggetti
.................................................................................................................156
Classi
..........................................................................................................................157
extends........................................................................................................................159
Costruttori
...............................................................................................................160
::...................................................................................................................................162
parent
..........................................................................................................................163
Serializzare oggetti - oggetti nelle sessioni
...................................................................164
Le funzioni magiche __sleep e
__wakeup.................................................................165
Riferimenti allinterno del
costruttore...........................................................................166
14. References
Explained........................................................................................................170
What References
Are.....................................................................................................171
What References
Do......................................................................................................171
What References Are
Not..............................................................................................172
Passing by
Reference.....................................................................................................172
Returning References
....................................................................................................173
Unsetting
References.....................................................................................................174
Spotting
References.......................................................................................................174
global
References..............................................................................................174
$this...................................................................................................................174
III.
Caratteristiche..........................................................................................................................175
15. Gestione degli
errori..........................................................................................................175
16. Creazione e manipolazione di
immagini...........................................................................180
17. Autenticazione HTTP usando
PHP...................................................................................182
18.
Cookies..............................................................................................................................185
19. Handling le uploads
........................................................................................................187
POST method
uploads...................................................................................................188
Common
Pitfalls............................................................................................................190
Uploading multiple les
................................................................................................190
PUT method
support......................................................................................................191
20. Utilizzo di le remoti
........................................................................................................193
21. Connection handling
.........................................................................................................196
22. Connessioni Persistenti ai
Database..................................................................................198
23. Modalit sicura (Safe
mode).............................................................................................201
Funzioni limitate/disabilitate dalla modalit sicura (safe-mode)
..................................203 24. Using PHP from the
command line
..................................................................................207
IV. Guida
Funzioni.........................................................................................................................218
I. Funzioni
Apache..................................................................................................................218
apache_child_terminate.................................................................................................219
apache_lookup_uri
........................................................................................................219
apache_note
...................................................................................................................220
apache_setenv................................................................................................................220
ascii2ebcdic
...................................................................................................................220
ebcdic2ascii
...................................................................................................................220
getallheaders..................................................................................................................221
virtual.............................................................................................................................221
II. Funzioni di Array
...............................................................................................................222
array...............................................................................................................................223
array_change_key_case.................................................................................................224
array_chunk
...................................................................................................................225
array_count_values........................................................................................................226
viii