You are on page 1of 184

PHP

PHP

Word Wide Web



.
open source ) .
(. Unix Linux
.
.
.

PHP .
PHP .
:PHP
PHP 1994 Rasmus Lerdorf . PHP
.
95 " " Personal Home Page Tools

.

95 " PHP/FI " 2 . FI


Rasmus HTML . PHP
. 96 15 .
97 50 . PHP
. PHP
PHP3 . . PHP4

PHP

Zend
.
PHP3 PHP4 RedHat's Stronghold WEB SERVER

. 6 PHP
IIS .

PHP
PHP Open Source PHP
.

C.

PHP .
PHP .

.
PHP
.

PHP C
.

PHP . PHP
. Perl
PHP .


.
6 PHP .

PHP

.
.
PHP . :
<?php
;)"$brow ser = getenn("HTTP_USER_AGENT
>?
<p>You are using the <?php echo($brow ser);?> w eb brow ser .
></p


PHP ) IIS
(Apache . ASP windows
JSP
.


.
PHP
DCOM Windows API .
PHP ISAPI .

PHP

Open Source

PHP
PHP 1.


.


.NET Visual Studio.NET .

PHP

. php
ASP) . IIS Cach ASP
(
ASP COM
PHP
mySql .
Zend
PHP
) (Cash .

Download 1

PHP

PHP
PHP

.
ZIP PDF DNS FTP TCP
SMTP ... ASP Upload File

.
PHP .

mySql,Sql

Server,mSql,dBase,Oracle,IBM DB2,PostgreSQL,InterBase
ODBC COM Ms Access
.

) 1(OOP .

PHP .
) (syntax PHP C++ Perl .
C Perl
ASP VBScript JSP
.

Object Oriented Programming 1

PHP

PHP
) (Sort .
.

PHP Perl CGI

PHP
PHP .
PHP PHP
PHP CGI .
PHP ) (server side
. form... PHP .

-1:
C , C++ , Perl Java PHP
! PHP .
PHP PHP ) ( $ .
$whatever .
$whatever :
; $Whatever++ ; $whatever =+1 ;$whatever=$whatever+1

Perl C++ C Java .

-2 )( Built-in facilities

PHP

Perl PHP
PHP

Perl .
. Perl

;)"\open ( MAIL,\"|/usr/sbin/sendmail -t
; "\print MAIL \"To: myself\@mydomain.com\n
; "\print MAIL \"From: visitor\@hisdomain.com\n
; "\print MAIL \"Subject: Comments from Web Form\n\n
; print MAIL $mainmessage
; ) close ( MAIL

PHP :
<?php
mail ( 'myself@mydomain.com', 'Comments from Web Form',
;) '$mainmessage, 'From: visitor@hisdomain.com
>?

!
FTP .HTTP
PHP
.
input form
:
>"\<input type=text name=\"dateofbirth

PHP

$dateofbirth .
input . -
[18] .

7 PHP ASP ][8


-1
PHP Pentium 166Mhz
Linux . Apache Web Server .
Windows NT IIS
Serve html ASP
. Microsoft Technology ASP
VBScript, MSSQL, ODBC
Engine Engine
.
Engine
MSSQL . MSSQL Extensions MSSQL

Query 1,88 ODBC


9,54 ASP
Engine .

-2 Memory

IIS4 20 Include 20
20 .
2000 IIS5 ASP

PHP

10

Hosting NT
Load .
PHP Memory
Load .

-3 !
ASP Encryption File Uploading
IIS .
PHP Compile Standard

MySQL -4
MySQL MSSQL . MySQL
DBMS PHP PHP MySQL Internally

Module
SQL .

-5 Syntax C/C++ Java

C/C++ Java
Syntax PHP . Syntax
Syntax Microsoft
.

-6

PHP

11

Microsoft
Boeing .
Open Source PHP
PHP Mailing List PHP
Patch .

-7 Platform
Microsoft Technology
) .Net ( ASP
ASP Windows ! ASP Engine
. ASP
.
PHP GNU C Compiler Platform Compile
Engine OS
.

PHP

12

PHP
PHP ' 'Hypertext Preprocessor
) . ) (preprocessor PHP
HTML (.
PHP www.php.net ) PHP (1
) Cross-platform (Server-side HTML embedded .
PHP
) (Server .
.
.

-1 PHP . www.php.net
. .

Cross-platform
Unix Macintosh Windows NT OS/2 .
PHP .

PHP

13

PHP
.
HTML embedded PHP HTML
. PHP HTML .
PHP ) (programming Languages

(scripting

) Language . ) (event .
1URL . Java

Script .
Java Script PHP Java Script
) (client-side . Java Script PHP ) (interpreted .
C Java
) (Compile .
PHP 4.0
PHP . 3.x

. PHP
.
PHP
PHP .
:
) CGI (Common Gateway Interface perl .
).ASP (Active Server Pages
).JSP (Java Server Pages

-URL (Uniform Resource Locator) 1 .

PHP

14

Java Script PHP . PHP


. CHT PHP HTML
.
PHP HTML HTML
HTML
. PHP

. PHP ) (Database
HTML .
HTML

.
CGI .
.
.


PHP CGI ASP JSP
: PHP .
PHP
PHP . ASP
VBScript ) CGI Perl C (
.
PHP .
VBScript Perl Java PHP

PHP

15

. PHP
Perl ASP Java CGI .
PHP .
PHP
PHP .

PHP
PHP
) (host .
www.dmcinsights.com(Internet Service ISP

) Provider ) (request
.
PHP .
PHP HTML
) . (2

:2 ) (client ) (Server
PHP . PHP .

PHP

16

) (ASP ) (third-party
.

HTML .
HTML
) . (3

-3
HTML . 2 .
HTML
.

home.html home.php .


. PHP
.

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

17

) (
php linux Windows
.
php
php

.

.
php Windows Linux .
PC ) PHP (
:
" XP NT 2000

XP NT IIS - 2000 ! "
php windows .
IIS IIS . ) (Internet Information Server
www ftp
IIS . 4 NT

XP 2000 .

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

18

:IIS
IIS XP NT 2000
IIS .
IIS START Settings Control Panel
Remove Programs Add or
Add or Remove Programs
Components Add/Remove Windows Windows
Components Wizard Components Internet Information

(Server(IIS . :

: Check Box


.

PHP

19

Next .
. )
( ) (
.

: CD

IIS CD Hard Disk .
Finish
IIS
.
IIS IIS . IIS .
IIS Control Panel Administrative Tools
Administrative Tools Internet Information Server
IIS IIS
) . IIS ( .
.

PHP

20

IIS IIS
PHP . PHP
PHP .
PHP PHP
4.4.2 .
http://www.php.net/downloads.php
. PHP

) PHP php-4.3.0-installer (
installation php 4.3.0 Welcome
.

PHP

21

Next Wizard .
Next License Agreement I Agree
PHP .
Installation Type Standard Advanced
Advanced ) Advanced

!(

Next Choose Destination Location


PHP .

PHP

22

Next Backup Replaced Files


Back up . PHP
Back up

Next Directory Choose Upload Temporary


PHP
.
Next Choose Session Save Directory
) Session ( .
Next Mail Configuration Smtp

PHP

23

Server )
Next (

Next Error Reporting Level



.

Next Server Type


Web server PHP Microsoft IIS 4 or Higher
XP NT 2000 .

PHP

24

Next Extensions File


WebServer PHP

). 3 (.

Next Start Installation PHP


Next
.
Installing
.

PHP

25

" "php.ini System32



ok ).
(
IIS Script tamp Node Selection
Select All ok .
PHP PHP
Ok .


php windows XP ME 2000
.
PWS Easy

.PHP

PHP

26

1 PWS Microsoft XP NT
2000 .
. PHP PWS
PWS .

:PWS
Setup.exe . Setup
Setup is initializing Wizard
.
Microsoft Personal Web Server Setup
PWS .
Next Microsoft Personal

Server Setup Web End User License Agreement


Pack Accept
.
Typical
) Minimum
Custom
Components . Typical (.
Typical Microsoft Personal Web Server Version...
Root ). www
Personal Web Server 1

PHP

27

ASP PHP (.
Browse .
FTP ).
Custom (.
Root Next
Completing Installation
.
Wizard PWS
Finish .
Finish .
PWS .
PHP PHP PWS
Server Type Microsoft Pws On Windows 9x or ME
Workstation NT

Microsoft PWS on NT

Workstation . PWS

PHP .

:Easy PHP
Easy PHP .
PHP .
Easy PHP .
IIS PWS PHP .

PHP

28

PHP
! Easy PHP
.
http://easyphp.abbal.com/depot/easyphp1-6_setup. exe

" "easyphp1-6_setup Wizard .


Easy PHP
YES .
Easy PHP
Suivant .

Accord de License Oui .

PHP

29

Wizard Easy PHP


> Suivant .

Easy PHP Programes


> Suivant Wizard .
Easy PHP Installer .
.


Terminer .

PHP

30

System Try Easy PHP


PHP .

PHP

31

PHP
PHP
PHP Web-Server . Web-Server IIS ) .
(.
PHP ) (Web Browser
Internet Explorer . PHP
) Wordpad ( Notepad Microsoft Word

PHP . PHP.
PHP . ) (Word pad
:
<?php
;)!echo (Hi! I am the first PHP script you have made
>?

1st.php Web Server Local Folder ) IIS


Windows Inetpub .
wwwroot (.
) (Internet Explorer http://
IP )

Control panel/System/Network

Identification IP Command Prompt ipconfig

( (/) Backslash ) (1st.php Enter .


simak . :

PHP

32

http://simak/1st.php http://localhost/1st.php

PHP >? <? php .


.

)( echo PHP .
HTML :
><html
><body
<?php
;)"!echo ("Hi! I am the first PHP script you have made
>?
></body
></html

):( Comments

PHP . // # PHP

33

PHP

/* Comment .

.* /

<?php
// This line will not be parsed in PHP
# This line will not be parsed in PHP like the line above
/* Line number1: These 3 lines will not either!
Line number2
Line number3: End of comment */
?>

PHP

34

HTML PHP .
PHP . PHP
)( print PHP .

. -
.
.
.

. PHP
.
PHP $ . PHP $
.
Pascal C++ C
Float Real Integer .
PHP .
. " PHP
" .
:

PHP

35

-1 PHP ) .( Case Sensitive


my_var My_Var MY_VAR PHP .
-2 ) Underscore
( . ) .
$s12 $S_1 $1s2 (.
PHP Underscore
. PHP
Underscore .
:
<?php
;$a=4
;$b=23
;" !$c="20hi
;"$d="world 26
$D=650.25; //It is different from $d
echo($D);//650.25
;)">echo("<br
echo($d);//world 26
;)">echo("<br
echo($a+$b); //27
;)">echo("<br
echo($b+$c);//43
;)">echo("<br
echo($c+$d);//20
;)">echo("<br
echo($c.$d);//20hi! world 26
;)">echo("<br
echo($a.$c.$d);//420hi! world 26
;)">echo("<br
;$c.=$d

PHP

36
echo($c);//20hi! world 26
;)">echo("<br
echo($d);//world 26
>?

)"
"( PHP .
" " PHP

.


: ) (numbers ) (strings
).(Arrays

PHP

37

: ) (integers )) (Floating-point
) (double precision floating point ) ((doubles .
.
PHP ) (object .
.


. .
) ( .
1.0 PHP . PHP -
. 1/4 0.25
.
:
1 1972 -1

:
1.0 19.72 -1.0

:
11/4 1972a 02.23.72

PHP

38

PHP
1.25 . .
.


) (string ) (
. ) ' ' ( quotation )""( Double Qutation
.
:
"!"Hello, world
"!"Hello, First Name
""1 1/4
"?"Hello World! How are you today
""02.23.72
""1972

.
. .
:
! Hello ,World
""?"I said, "how are you

PHP

39

.
. PHP
. .
1 )( print quotation
)\( backslash . PHP quotation
) (
.
""\?"I said,\"How are you

.
.
)\( Backslash . :
Backslash (\) single quotation mark (') Apostrophe ).dollar ($
: double quotes Single quotes

. :
;'!print 'Hello,$FirstName

Hello,$FirstName . $FirstName Larry


( . :
;"!Print "Hello,$FirstName

! Hello,Larry.

PHP

40

][14][15

PHP
!
PHP
.
) (Single Quote Double Quote . :
<?php
;"echo "Visit http://www.phpmystery.com
>?

:
<?php
;'echo 'Visit http://www.phpmystery.com
>?

.
:
HTML :
<?php
;">"\echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0
>?

.
<?php
;'>"echo '<table border="1" cellspacing="0" cellpadding="0
>?

PHP

41

)"( )\(
Parse Error )'(
.
!
!
Benchmark .
double (") vs. single (') quotes
Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x
]+ 101 % 1: single (') quotes. Just an empty string: $tmp[] = ''; Total time: 3[ms
]+ 100 % 2: double (") quotes. Just an empty string: $tmp[] = ""; Total time: 3[ms
]+ 111 % 3: single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa'; Total time: 3[ms
]+ 118 % 4: double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa"; Total time: 3[ms
+ 115 % 5: single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a'; Total time:
]3[ms
+ 461 % 6: double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a"; Total
]time: 13[ms
+ 113 % 7: double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a"; Total
]time: 3[ms

: " $

.
\n \r \t
' " .
<?php
;'echo 'check out http://www.phpmystery.com' . "\n\r" . 'to discover unknowns about PHP
>?

PHP

42

:
1 .
2 }{ (*).
<?php
"$sql = "select * from `mytable` where `status` = '{$status}' order by name
>?

3 Print Echo .
)*(

) Property (
:
<?php
;'$sport1 = 'volley
;"$plan1 = "I will play $sport1ball in the summertime
;"$plan2 = "I will play {$sport1}ball in the summertime
>?

$

NULL . :
<?php
;'// $plan1 = 'I will play in the summertime
'// $plan2 = 'I will play volleyball in the summertime
>?

PHP

43

: \n print .
backslash n .
\r 1 ) \t (tab .



.
.
. .
: PHP ).
) (indexed ) (vector ( perl
associative3 hash2 multi_dimensional4 .
PHP ) ( "" .

:Carriage return 1 . )(return


) . "
:: (70
2
3
4

PHP

44

PHP ) (declare .
. PHP )=( -
. ) (assignment operator .
:
;$number=1
;$floating-number=1.2
;"!$string="Hello,World

PHP Java Script .


$variable Greeting .
;$Variable=1
;"$Variable="Greeting

:
) (Predefined Variables
) (Web server Application
)) (Web Server Operating System Windows NT (Solaris . PHP
) (Variables environment .
.
:

.

PHP

45

) HOSTNAME :
( . ) OSTYPE (.
Apache ) DOCUMENT_ROOT :
( ) HTTP_USER_AGENT platform (.
PHP-SELF PHP .

PHP

46

[8] :

PHP

PHP HTML


TextEditor php . html

php ). (
Notepad . .php .
first.php
Code:
?< 1:
;"!2: print "Hello Web
>? 3:

php
browser . PHP browser

.
! Hello Web .
.
php ) php
(.
php upload .

Code:

?<
. php . html

PHP

47

PHP
Code:

>?
server php html
.

Code:

;
. .

Code:
>? ; "!<? Print "Hello Word

: Print
.
Print .

PHP HTML :
notepad upload.
Code:
>1: <html
>2: <head

PHP

48

>3: <title>Listing 3.2 A PHP script including HTML</title


>4: </head
>5: <body
>6: <b
?< 7:
;"8: print "hello world
>? 9:
>10: </b
>11: </body
>12: </html

HTML PHP .

Code:
?<

html
Code:
?<

Code:
>?

php .

Comment

comment

.
) . comment (
comment
Code:

PHP

49

//


Code:
#

Code:
// This is a comment

Code:
# in yek comment ast

PHP

50

:
-

) (

:
. $
. _ . space
.
:
Code:
;$a
;$a_longish_variable_name
;$2453
$sleepyZZZZ

PHP.
.
php . :
Code:
;$num1 = 8
;$num2 = 23

. :
Code:
;print $num1

PHP

51


Code:
;print 8

: DATA TYPE

.

. PHP
.
Type - Example - Description
number Integer - 5 - A whole
Double - 3.234 - A floating-point number
collection of characters String - "hello" - A
false Boolean - true - One of the special values true or

:OBJECT
)( gettype . :
Code:
>1: <html
>2: <head
>3: <title>Listing 4.3 Testing the type of a variable</title
>4: </head
>5: <body
6: <?php
;7: $testing = 5
8: print gettype( $testing ); // integer
;">9: print "<br

PHP

52

;"10: $testing = "five


11: print gettype( $testing ); // string
;)">12: print("<br
;13: $testing = 5.0
14: print gettype( $testing ); // double
;)">15: print("<br
;16: $testing = true
17: print gettype( $testing ); // boolean
;">18: print "<br
>? 19:
>20: </body
>21: </html

:
Code:
integer
string
double
boolean

INTEGER . STRING .
. PHP STRING " ' DOUBLE .
BOOLEAN . TRUE .FALSE

)(: settype
PHP )( settype .
.
:
Code:
>1: <html

53

PHP

2: <head>
3: <title>Listing 4.5 Changing the type of a variable with settype()</title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: print gettype( $undecided ); // double
9: print " -- $undecided<br>"; // 3.14
10: settype( $undecided, string );
11: print gettype( $undecided ); // string
12: print " -- $undecided<br>"; // 3.14
13: settype( $undecided, integer );
14: print gettype( $undecided ); // integer
15: print " -- $undecided<br>"; // 3
16: settype( $undecided, double );
17: print gettype( $undecided ); // double
18: print " -- $undecided<br>"; // 3.0
19: settype( $undecided, boolean );
20: print gettype( $undecided ); // boolean
21: print " -- $undecided<br>"; // 1
22: ?>
23: </body>
24: </html>

GETTYPE ()
.
STRING 10 DOUBLE 3,14 7

) ( . 3 INTEGER 13
......

:
Data Type
.

54

PHP

:
Code:
1: <html>
2: <head>
3: <title>Listing 4.6 Casting a variable</title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: $holder = ( double ) $undecided;
9: print gettype( $holder ) ; // double
10: print " -- $holder<br>"; // 3.14
11: $holder = ( string ) $undecided;
12: print gettype( $holder ); // string
13: print " -- $holder<br>"; // 3.14
14: $holder = ( integer ) $undecided;
15: print gettype( $holder ); // integer
16: print " -- $holder<br>"; // 3
17: $holder = ( double ) $undecided;
18: print gettype( $holder ); // double
19: print " -- $holder<br>"; // 3.14
20: $holder = ( boolean ) $undecided;
21: print gettype( $holder ); // boolean
22: print " -- $holder<br>"; // 1
23: ?>
18: print gettype( $holder ); // double
19: print " -- $holder<br>"; // 3.14
20: $holder = ( boolean ) $undecided;
21: print gettype( $holder ); // boolean
22: print " -- $holder<br>"; // 1
23: ?>
Integer $holder 14

. 16 3

PHP

55

: C $a+=$b $a++

:
data type .
.
.
+.

9=5+4
+ 4 5 9 .
) = ( .
PHP Code:
;) "print ( $name = "matt

matt name matt .

PHP

56

:
PHP .

$a + $b

$a - $b

$a * $b

$a / $b

$a % $b

5+3

10-3

10/2

20

2*10

10%3

) : ( .
.
:
PHP Code:
""hello"." world
returns
""hello world

PHP

57

php .
= + .

PHP Code:
;$x = 4
$x += 4; // $x now equals 8

= - = / .
PHP Code:
;$x = 4
$x -= 4; // $x now equals 1

:
True
False .

PHP Code:
$x < 5

x 3 True .

==

. x 4 .
PHP Code:
$x == 5

False .

PHP

58

=! :

x 4 :
PHP Code:
$x =! 5

True .

===

Data type .

or

)( .

||

PHP Code:
true || false

True .
&& True True.

PHP Code:
) ( $x > 2 ) && ( $x < 15

True x 2 15.

PHP

59

Bold .

++ --
++ -- .

PHP Code:
;$x++

$x .

PHP Code:
;$x = 3
$x++ < 4; // true

True.
.
:
PHP Code:
;$x = 3
++$x < 4; // false

False .

:
php :

PHP

60

PHP Code:
)++ (cast
/ *%
+
> >= =< <
=! === ==
&&
||
== += = /= *=%= .
and
xor
or


PHP Code:
4+5*2

2 5 4 .
14 .
php .
PHP Code:
(4+5) * 2

18 .

PHP

61

PHP
.
. .

TrueCLAUSE IF .

FalseELSE .

switch .-

while .

FOR . - FOR .

script . IF
php .

: IF
IF True
.
PHP Code:
) if ( expression
{
// code to execute if the expression evaluates to true
}

IF expression . . } {
true If .
PHP Code:

PHP

62
>1: <html
>2: <head
>3: <title>Listing 5.1</title
>4: </head
>5: <body
6: <?php
;"7: $mood = "happy
) "8: if ( $mood == "happy
{ 9:

;"10: print "Hooray, I'm in a good mood


} 11:
>? 12:
>13: </body
>14: </html

8 == mood happy .
} { .
:
PHP Code:
) "if ( $mood == "happy
;"print "Hooray, I'm in a good mood

Hooray, I'm in a good mood . mood " "sad


.

ELSE :IF
:
PHP Code:
) if ( expression
{
// code to execute if the expression evaluates to true
}

PHP

63

else
{
// code to execute in all other cases
}

else if False.
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.2</title
>4: </head
>5: <body
6: <?php
;"7: $mood = "sad
) "8: if ( $mood == "happy
{ 9:
;"10: print "Hooray, I'm in a good mood
} 11:
12: else
{ 13:
;"14: print "Not happy but $mood
} 15:
>? 16:
>17: </body
>18: </html

mood " "sad " "happy IF


else .
Code:

Not happy but sad


.

: ElseIf

PHP

64

ElseIf True .
:
PHP Code:
) if ( expression
{
// code to execute if the expression evaluates to true
}
) elseif ( another expression
{
// code to execute if the previous expression failed
// and this one evaluates to true
else
{
// code to execute in all other cases
}

IF Esleif
. else IF
Elseif .
esleif . Else
.
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.3</title
>4: </head
>5: <body
6: <?php
;"7: $mood = "sad
) "8: if ( $mood == "happy
{ 9:
;"10: print "Hooray, I'm in a good mood
} 11:

PHP

65
) "12: elseif ( $mood == "sad
{ 13:

;"!14: print "Awww. Don't be down


} 15:
16: else
{ 17:
;"18: print "Neither happy nor sad but $mood
} 19:
>? 20:
>21: </body
>22: </html

mood sad . Happy


Elseif . mood sad True .

: Switch
. switch

True.
:
PHP Code:
) switch ( expression
{
case result1:
// execute this if expression results in result1
;break
case result2:
// execute this if expression results in result2
;break
default:
// execute this if no break statement
// has been encountered hitherto

PHP

66
}

) switch expression ( .
) switch } { ( case
switch case .
default . case
default .

: break case .
case default
. break case
switch .

:
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.4</title
>4: </head
>5: <body
6: <?php
;"7: $mood = "sad
) 8: switch ( $mood
{ 9:
10: case "happy":
;"11: print "Hooray, I'm in a good mood
;12: break
13: case "sad":
;"!14: print "Awww. Don't be down
;15: break
16: default:

PHP

67

;"17: print "Neither happy nor sad but $mood


} 18:
>? 19:
>20: </body
>21: </html
mood sad Php 8 switch mood

happy ) (10 True 13


mood=sad 11 12 . 12 break 18
.

)( :
IF ! IF

.
PHP Code:
( expression )?returned_if_expression_is_true:returned_if_expression_is
;_false

True :
.
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.5</title
>4: </head
>5: <body
6: <?php
;"7: $mood = "sad
8: $text = ( $mood=="happy" )?"Hooray, I'm in a good mood":"Not happy but

PHP

68
;"$mood
;"9: print "$text
>? 10:
>11: </body
>12: </html

8 . mood happy
Hooray, Im in good mood ) (
Not Happy But $mood $mood Sad .Not Happy But Sad

.

:
.
.
.
) (
loop .

: while
PHP Code:
) while ( expression
{
// do something

PHP

69
}

) while expression ( True


while .
False .
2 .
PHP Code:
Listing 5.6: A while Statement
>1: <html
>2: <head
>3: <title>Listing 5.6</title
>4: </head
>5: <body
6: <?php
;7: $counter = 1
) 8: while ( $counter <= 12
{ 9:
;">10: print "$counter times 2 is ".($counter*2)."<br
;11: $counter++
} 12:
>? 13:
>14: </body
>15: </html

7 counter 1 . 8
counter 12 . 10 11 counter
. ++ .
12 10 - 8 - 4 - 2 .

PHP

70

11 counter
counter 12 .

do : while
while !
.
PHP Code:
{ do
// code to be executed
}
;) while ( expression

: while ; .

while .
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.7</title
>4: </head
>5: <body
6: <?php
;7: $num = 1
8: do
{ 9:
;"10: print "Execution number: $num<br>\n
;11: $num++
} 12:
;) 13: while ( $num > 200 && $num < 400
>? 14:

PHP

71

>15: </body
>16: </html
do....while num 200 400 . num

7 while False
Execution numnber 1. :

: For
For While .
For while .
:
PHP Code:
) for ( variable assignment; test expression; variable increment
{
// code to be executed
}

for ; .
loop .

:
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.8</title
>4: </head
>5: <body
6: <?php
) 7: for ( $counter=1; $counter<=12; $counter++

PHP

72
{ 8:

;">9: print "$counter times 2 is ".($counter*2)."<br


} 10:
>? 11:
>12: </body
>13: </html

7 counter 1
counter 12 .
counter .
for while .
For
.

: break
.
.
:
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.9</title
>4: </head
>5: <body
6: <?php
) 7: for ( $counter=1; $counter <= 10, $counter++
{ 8:
;9: $temp = 4000/$counter
;">10: print "4000 divided by $counter is... $temp<br
} 11:

PHP

73

>? 12:
>13: </body
>14: </html

4000 counter 1 10
. . counter
.
php .
-4 10 counter .
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.10</title
>4: </head
>5: <body
6: <?php
;7: $counter = 4
) 8: for ( ; $counter <= 10; $counter++
{ 9:
) 10: if ( $counter == 0
;11: break
;12: $temp = 4000/$counter
;">13: print "4000 divided by $counter is... $temp<br
} 14:
>? 15:
>16: </body
>17: </html

10 11 counter .
break 14 .
7 counter . 8
counter .

PHP

74

for ; .

: continue

. continue .
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.11</title
>4: </head
>5: <body
6: <?php
;7: $counter = 4
) 8: for ( ; $counter <= 10; $counter++
{ 9:
) 10: if ( $counter == 0
;11: continue
;12: $temp = 4000/$counter
;">13: print "4000 divided by $counter is... $temp<br
} 14:
>? 15:
>16: </body
>17: </html

10 11 counter
8 .

PHP

75

php .
.
:
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 5.12</title
>4: </head
>5: <body
6: <?php
;"7: print "<table border="1">\n
) 8: for ( $y=1; $y<=12; $y++
{ 9:
;"10: print "<tr>\n
) 11: for ( $x=1; $x<=12; $x++
{ 12:
;">13: print "\t<td
;)14: print ($x*$y
;"15: print "</td>\n
} 16:
;"17: print "</tr>\n
} 18:
;">19: print "</table
>? 20:
>21: </body
>22: </html

PHP

76

)(Functions

. .

:
-

Global .


.
.

.
. 1000
.
.

.

PHP

77

. php
.
php print.
PHP Code:
;)"print("Hello Web

)( print
PHP Code:
;)"print(("Hello Web
and
;"print "Hello Web

print .
print " "hello word .
. . Print
argument . Argument
. :
PHP Code:
;) some_function( $an_argument, $another_argument

.
True False .
)( ABS .
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 6.1</title
>4: </head
>5: <body
6: <?php
;7: $num = 321

PHP

78
;) 8: $newnum = abs( $num
;9: print $newnum
"10: // prints "321
>? 11:
>12: </body
>13: </html

-321 $num . abs


$newnum .
abs .
PHP Code:
;) ) print( abs( 321

.
.

:
Function .
PHP Code:
) function some_function( $argument1, $argument2
{
// function code here
}

Function .
Argument ) (
. Argument .
PHP Code:
>1: <html
>2: <head

79

PHP

3: <title>Listing 6.2</title>
4: </head>
5: <body>
6: <?php
7: function bighello()
8: {
9: print "<h1>HELLO!</h1>";
10: }
11: bighello();
12: ?>
13: </body>
14: </html>

. bighello 7
Argument bighello . H1 ! Hello
.
PHP Code:
1: <html>
2: <head>
3: <title>Listing 6.3</title>
4: </head>
5: <body>
6: <?php
7: function printBR( $txt )
8: {
9: print ("$txt<br>\n");
10: }
11: printBR("This is a line");
12: printBR("This is a new line");
13: printBR("This is yet another line");
14: ?>
15: </body>
16: </html>

PHP

80

printBR Argument . 11 12 13
$txt .
7 . 11 12 13 $txt
9 .
.
Argument
.
:
PHP Code:
)"function printBR( $txt = "nothing

$txt default " "nothing .


Argument Default $txt $txt
.
:
][php
;)(PrintBR
;)"PrintBr("Hello
PHP Code:

Nothing Hello . Default


][b

Return . return
.
PHP Code:

PHP

81
>1: <html
>2: <head
>3: <title>Listing 6.4</title
>4: </head
>5: <body
6: <?php

;7: function addNums( $firstnum, $secondnum


{ 8:
) 9: $result = $firstnum + $secondnum
;10: return $result
} 11:
;)12: print addNums(3,5
"13: // will print "8
>? 14:
>15: </body
>16: </html

8 . 3 5 $firstnum and $secondnum -


. $result 10 .
Return :
PHP Code:
) function addNums( $firstnum, $secondnum
{
;) return ( $firstnum + $secondnum
}

.
return .
PHP Code:
;return 4

:
PHP Code:
;) return ( $a/$b

PHP

82

:
PHP Code:
;) ) return ( another_function( $an_argument

Function :
String .
. :
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 6.5</title
>4: </head
>5: <body
6: <?php
)(7: function sayHello
{ 8:
;">9: print "hello<br
} 10:
;"11: $function_holder = "sayHello
;)(12: $function_holder
>? 13:
>14: </body
>15: </html

7 Sayhello function_holder 11
sayHello function_holder
.

PHP

83

.
. url
.
function function.

:
: .
.


.
PHP Code:
>1: <html
>2: <head
>3: <title>Listing 6.6</title
>4: </head
>5: <body
6: <?php
)(7: function test
{ 8:
;"9: $testvariable = "this is a test variable
} 10:
;">11: print "test variable: $testvariable<br
>? 12:
>13: </body
>14: </html

PHP

84

. $testvariable 11
$testvariable 9 function .

) Global ( :
Default .

PHP Code:
>1: <html
>2: <head
>3: <title>Listing 6.7</title
>4: </head
>5: <body
6: <?php
;7: $life = 42
)(8: function meaningOfLife
{ 9:
;">10: print "The meaning of life is $life<br
} 11:
;)(12: meaningOfLife
>? 13:
>14: </body
>15: </html

$life .
. Global .
:
PHP Code:
>1: <html
>2: <head

PHP

85

>3: <title>Listing 6.8</title


>4: </head
>5: <body
6: <?php
;7: $life = 42
)(8: function meaningOfLife
{ 9:
;10: global $life
;">11: print "The meaning of life is $life<br
} 12:
;)(13: meaningOfLife
>? 14:
>15: </body
>16: </html

10 ; global $life . $life 7


The meaning of life is 42 .
.
.
: $life $life .

PHP

86

)(GD


PNG , GIF , JPG . WBMP , XPM
.
.
) ( GD ) ( library
...
/http://www.boutell.com/gd
...
PNG 1,6 GIF 2,0,28-
.
:
PHP Code:
<?php
;)"header("Content-type: image/png
;]'$string = $_GET['text
;)"= imagecreatefrompng("images/button1.png

$im

;)$orange = imagecolorallocate($im, 220, 210, 60


;= (imagesx($im) - 7.5 * strlen($string)) / 2

$px

;)imagestring($im, 3, $px, 9, $string, $orange


;)imagepng($im
;)imagedestroy($im
>?

PHP

87

HTML Code:
>"<img src="button.php?text=text


) ( button.php images/button1.png TEXT
) (

)
( dynamically .
gd :
: gd_info GD .
... :
PHP Code:
<?php
;))(print_r(gd_info
>?

) (
Code:
Array
(
)[GD Version] => bundled (2.0.28 compatible
[FreeType Support] => 1
[FreeType Linkage] => with freetype
>= ][T1Lib Support
[GIF Read Support] => 1
[GIF Create Support] => 1
[JPG Support] => 1
[PNG Support] => 1
[WBMP Support] => 1

88

PHP

[XBM Support] => 1


[JIS-mapped Japanese Font Support] =>
)

( ) : getimagesize
GIF, JPG, PNG, 52*48

. JPX, JB2, JPC, XBM, or WBMP ,SWF, SWC, PSD, TIFF, BMP, IFF, JP2
:
PHP Code:
<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";
?>

: URL 4,0,5
PHP Code:
<?php
$size = getimagesize("http://www.example.com/gifs/logo.gif");
?>

image type() : image_type_to_extension

:
PHP Code:
image_type_to_extension ( int imagetype [, bool include_dot] )

mime type : image_type_to_mime_type


( PNG ) mime type
.
:
PHP Code:

89

PHP

<?php
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
?>

:

image/gif IMAGETYPE_GIF
IMAGETYPE_JPEG image/jpeg
IMAGETYPE_PNG image/png
IMAGETYPE_SWF application/x-shockwave-flash
IMAGETYPE_PSD image/psd
IMAGETYPE_BMP image/bmp
IMAGETYPE_TIFF_II(intel byte order) image/tiff
IMAGETYPE_TIFF_MM (motorola byte order) image/tiff
application/octet-stream IMAGETYPE_JPC
IMAGETYPE_JP2 image/jp2
application/octet-stream IMAGETYPE_JPX
IMAGETYPE_JB2 application/octet-stream
IMAGETYPE_SWC application/x-shockwave-flash
IMAGETYPE_IFF image/iff
IMAGETYPE_WBMP image/vnd.wap.wbmp
IMAGETYPE_XBM image/xbm

. :
: image2wbmp
:
PHP Code:
<?php
$file = 'php.png';
$image = imagecreatefrompng($file);
header('Content-type: ' . image_type_to_mime_type(IMAGETYPE_WBMP));
image2wbmp($image); // output the stream directly
?>

Mysite: Http://Bashiry.250free.com

Email: m.bashiry@gmail.com

90

PHP

PHP Code:
<?PHP if($os !=="FREE"){ die("Please install a free os "); } ?>

Mysite: Http://Bashiry.250free.com

Email: m.bashiry@gmail.com

PHP

91

PHP
)( .
PHP Code:
<?php
)function fonts($txt,$siz
{
;"print "<font size=$siz >$txt
}
;)fonts("salam",5
;)fonts("khoobi",10
>?

!
Array :

. .
:
index value Array
Mehdi 0
Asef 1
tsotodeh 2
knowhow 3
Piter1356 4

:
PHP Code:
;)"$users=array("Mehdi","Asef","tsotodeh","knowhow","Piter1356

Asef :
Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

92
PHP Code:
;"]Print "$users[1


PHP Code:
;"$users[]="azemati

.
.. .. - 3 2 -1 0
:
PHP Code:
$user = array (name=>"Asef",job=>"Programming",age=>24,
;)""skill"=>"everyThing

:
PHP Code:
;"]Print"$user[name
Or
;"]Print"$user[job


.
:
PHP Code:
;"$user[name]="azemati
;"$user[job]="webmaster

:
Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

93

PHP Code:
$user = array (array(name=>"Asef",job=>"Programming",age=>24,
"skill"=>"everyThing"),array(name=>"mehdi",job=>"Programming",age=>18,
"skill"=>"noThing"),array(name=>"daftarkhatereh",job=>"Programming",age=>24,
)""skill"=>"everyThing

;)

:
PHP Code:
;]print $user[0][job
"//Print "Programming

;] print $user[count .

:
PHP Code:
<?php
;)"$users=array("a","b","c","d","e
;]print $users[count($users) 1
//Print 5
>?

!

)( Array_merge :
PHP Code:
<?php
;)"$first = array("a", "b", "c
;)$second = array(1,2,3
;) $third = array_merge( $first, $second
) foreach ( $third as $val
{

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

94
;">print "$val<BR
}
>?


foreach for
:
PHP Code:
) foreach( $array as $temp
{
//...
}

sort
PHP Code:
<?php
;)"$an_array = array("x","a","f","c
;)sort( $an_array
) foreach ( $an_array as $var
{
;">print "$var<BR
}
>?

.
;)( Sort .
.
;)( assort :
PHP Code:
<?php
;)$first = array("first"=>5,"second"=>2,"third"=>1
;) asort( $first
) foreach ( $first as $key => $val
{

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

95
;">print "$key = $val<BR
}
>?

:
third = 1
second = 2
first = 5


;)( ksort :
PHP Code:
<?php
;)$first = array("x"=>5,"a"=>2,"f"=>1
;) ksort( $first
) foreach ( $first as $key => $val
{
;">print "$key = $val<BR
}
>?


a=2
f=1
x=5

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

96

PHP



. Object
!
Properties
Method .

:
.

!

!

.

. .
:
PHP Code:
Class First_class
{
// properties

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

97

//
// methods
}

:
class .
properties
class .
methods.
}{ .

:

.
:
PHP Code:
{ Class f_class
;"var $name="mehdi
}
;)($obj1=new f_class
;)($obj2=new f_class
;"$obj1->name="Ali
;">print "$obj1->name<br
;">print"$obj2->name<br

:
PHP Code:
;)($obj1=new f_class

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

98

> -
name

PHP Code:
;" Print " $obj1->name

:
:
PHP Code:
class f_class
{
;var $name
)(function sayHello
{
;"Print "Hello World
}
}
;)($obj1=new f_class
;)($obj1->sayHello
// Hello World


> - .


Return :
PHP Code:
class f_class
{

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

99

;"var $name="mehdi
)(function sayHello
{
;">Print "Hello My names $this->name<br
}
}
;)($obj1=new f_class
;)($obj1->sayHello

// Hello My names mehdi

$this Return
> -
name :
PHP Code:
{

class f_class

;"var $name="mehdi
{)function setName($n
}

;$this->name=$n
)(function sayHello
{

;">Print "Hello My names $this->name<br


}
}
;)($obj1=new f_class
;)"$obj1->setName("Ali
;)($obj1->sayHello
// Hello My names Ali

this
.
Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

100


.
:
PHP Code:
{ class first_class
;var $name
{ ) "function first_class( $n="mehdi
;$this->name = $n
}
{ )(function sayHello
;">print "hello my name is $this->name<BR
}
}
;)"$obj1 = new first_class("Ali
;)"$obj2 = new first_class("Asef
;)($obj1->sayHello
// hello my name is Ali
;)($obj2->sayHello
// hello my name is Asef


.


.
extends
:
PHP Code:
{class first_class
;"var $name = "mehdi
{ ) function first_class( $n

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

101

PHP

$this->name = $n;
}
function sayHello(){
print "Hello my name is $this->name<br>";
}
}
class second_class extends first_class {
}
$test = new second_class("son of mehdi");
$test->sayHello();
// outputs "Hello my name is son of mehdi"


.
I dont know my name sayHello

12
: I dont know my name
PHP Code:
class first_class {
var $name = "harry";
function first_class( $n ) {
$this->name = $n;
}
function sayHello() {
print "Hello my name is $this->name<br>";
}
}
class second_class extends first_class {
function sayHello() {
print "I'm not going to tell you my name -- ";
first_class::sayHello();
}
}

Mysite: Http://Bashiry.250free.com

Email: m.bashiry@gmail.com

PHP

102

;)"$test = new second_class("son of harry


;)($test->sayHello
"// "I'm not going to tell you my name -- Hello my name is son of harry

) :: (
sayHello .

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

103

PHP
): (Classes

.
class PHP .
.

. .
.
PHP ) members :(
( Consructor members .
.
.

.
.

. car
.

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

104

Constructor : . Constructor

PHP . car
car .
Constructor
. .
Constructor .
Constructor

. Constructor
.
Constructor .
. Constructor

.

.
OPP .
Constructor
. .
.
Class PHP
:
PHP Code:
]class ClassName [extends ParentclassName
{
;var $member1
;var $member2

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

105

;var $memberN
// Constructor
)(function Class Name
{
}
)(function method1
{
}

)(function method2
{
}
)(function methodN
{
}
}

)(
)( . integer
. PHP
.
PHP
. .

.

. .

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

106

.
.
Car . .
Class .
.
. Constructor
.
. Car.php .

. .

. )( include_once
)( require_once .

PHP Code:
<? Php
// Car.php
class Car
{


.
:

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

107
PHP Code:
; var $engine
;var $requiredkey

constructor
. ()start ()stop
constructor
.
.
> $this-
. C++ .

PHP namespace ) . namespace


(.
namespace .
namespace Namespace . .
namespace . $this PHP
namespace ) ( . $this
namespace .

.

assertion .
)( start key . key
:

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

108

PHP

PHP Code:
// Constructor
function Car()
{
$this-> requiredkey();
$this->engine= new Engine() ;
}
function start ($Key)
{
if ($key->equals($this->requiredKey)) {
$this->engine->start();
return true ;
}
return false ;
}

. start() stop()
.
. engine stop()
: . ( logic)
PHP Code:
function stop ()
{
if ($this->engine->isRunning()) {
$this->engine->stop() ;
}
}
// Several other methods such as moving and turning , and so on .
}
?>

Mysite: Http://Bashiry.250free.com

Email: m.bashiry@gmail.com

PHP

109

):(Objects
.
) ( .

:
PHP Code:
<? Php
;)($car1=new Car
;)($car2=new Car

new .
)(instantiate
. $car1 $car2 .
.
:
PHP Code:
; )($cars = array
{ )for($I=0;$I<10;$I++
;)( $cars[$I]=new Car
}

()start :

PHP Code:
;)$carHasStarted = $car1->start($myKey
;)if ($carHasStarted) echo(Car has started.

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

110

:
PHP Code:
;)($car1->stop
>?

.
.
.
.
: ) Encapsulation ( .
) Factory Methods ( ) Encapsulation (
:

:

. ) (factory
. .

.
.
:
Form Control Factory
) submit ( HTML
eXtreme PHP ) open source
/http://www.extremephp.org ( .
Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

111


.
TextField
( SubmitButton ) PHP Extreme Form Control Factory .
. TextField.php
TextField SubmitButton.php SubmitButton .
Constructor
:
PHP Code:
<?php
;)include_once(./Text Field.php
;)include_once(./SubmitButton.php

Factory
Factory
:
PHP Code:
// FormControl Factory.php
class FormControlFactory
{

)( createTextField
TextField $ name $value :
PHP Code:
)function createTextField($name,$value
{
; )return new TextField($name,$value
}

)( createSubmitButton .
create .
Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

112

) (traceability
:
PHP Code:
)function createSubmitButton($name,$value
{
; )return new SubmitButton ($name,$value
}
}

TextField SubmitButton
Form Control Factory :
PHP Code:
;)($ Form Control Factory =new Form Control Factory
=$firstNameFild
;)$ Form Control Factory -> createTextField(firstname, Ken
=$lastNameFild
;)$ Form Control Factory -> createTextField(lastname, Egervai
=$ SubmitButton
;)$ Form Control Factory -> create SubmitButton(submit, Submit Name
>?

Form Control Factory


. )( createTextField
. submit "Submit Name
.

.
.
find

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

113

.
. encapsulation
.

) Encapsulation(:
.
.
. Car
.
. .

.
.
.

PHP
.


.
setSpeed ) (speed$ km/h200
km/h0 . constructor

)( :
PHP Code:

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

114

;)myKey =new Key(Key of my Porsche


;)($car = new Car
;)($car->engine = new Engine
;$car->speed=400
;)$car->start($myKey
;$car->engine =0
;)($car->stop


. $requiredkey
$car constructor .


. .
Engine . :
;)( $car->engine = new Engine
) E Engine (.

.
. :
PHP Code:
$car->speed=400;//should have been $car ->setSpeed(400); to cause // a failure
;)$car->start($myKey

km/h400 .
) ( . .

) 0 (
Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

115

key$

.
.
.
engine 0 :
PHP Code:
$car->engine=0
;)($car->stop

)( stop .
Engine integer .
.



.
)(object violations
:

.
) (integrity ) ( :

.

.

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

116


. .
.

.
.
) (
. PHP
.

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

117

PHP
:
.
:
PHP Code:
#this page name is a.php
>"<form action="b.php" method="get
>"<input type="text" name="user
>"<input type="submit" name="btn
></form

b.php
a.php .
.
>?php

"print "$user
?<

.
get
post

PHP : HTML
:
PHP Code:

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

118

PHP

<form action="<?php print $PHP_SELF?>" method="POST">


name: <input type="text" name="user">
</form>

$PHP_SELF

.

:
PHP Code:
<?php
$num_guess=42;
$msg=" ";
if (! Isset($guess))
{
$msg="welcome To This Little Game";
}
elseif ($guess>$num_guess)
{
$msg="number $guess is Big! Try Smaller number.";
}
elseif($guess<$num_$guess)
{
$msg="Number $guess is Small ! Try Big Number";
}
else // vagti mosavi bashe
{
$msg="Well Done You Win";
}
?>

Mysite: Http://Bashiry.250free.com

Email: m.bashiry@gmail.com

PHP

119

: ! isset
) (
.

PHP Code:
><h1
>? <?php print $msg
></h1
>"<form action="<?php print $PHP_SELF?>" method="POST
>"Guess Number : <input type="text" name="guess
></form

.


else :
PHP Code:
;)"header("Location: page.html
;exit

exit .
) . (
PHP Code:
>"<form enctype="multipart/form-data" action="<?print $PHP_SELF?>" method="POST
>"<input type="hidden" name="MAX_FILE_SIZE" value="51200
><input type="file" name="fupload"><br
>"!<input type="submit" value="upload
></form

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

120

"enctype="multipart/form-data
.

51200
. 50
.

.

tmp/php3d3ef/ fupload$
Test.gif fuploadname$
51200 fuploadsize$
Image/gif fupload type$


PHP Code:
>1: <html
>2: <head
>3: <title>Listing 9.15 A file upload script</title
>4: </head
5: <?php
;"6: $file_dir = "/home/mehdi/htdocs/uploads
;"7: $file_url ="http:// www.safary.com/mehdi/uploads
) ) 8: if ( isset( $fupload
{ 9:
;"10: print "path: $fupload<br>\n
;"11: print "name: $fupload_name<br>\n
;"12: print "size: $fupload_size bytes<br>\n
;"13: print "type: $fupload_type<p>\n\n
) "14: if ( $fupload_type == "image/gif

121

PHP

15: {
16: copy ( $fupload, "$file_dir/$fupload_name") or die ("Couldn't copy");
17:
18: print "<img src=\"$file_url/$fupload_name\"><p>\n\n";
19: }
20: }
21: ?>
22: <body>
23: <form enctype="multipart/form-data" action="<?php print $PHP_SELF?>"
method="POST">
24: <input type="hidden" name="MAX_FILE_SIZE" value="51200">
25: <input type="file" name="fupload"><br>
x<input type="submit" value="Send file!">
27: </form>
28: </body>
29: </html>

$fupload, "$file_dir/$fupload_name"
copy Couldn't

.

PHP

122


)(:

DBM
.
.
:

DBM

:
)( dbmopen
:

:
PHP Code:

PHP

123

;) "$db = dbmopen( "./products", "c" ) or die( "Couldn't open DBM

: die .
.
PHP Code:
;)dbmclose($db

:
:
PHP Code:
<?php
;)"$db=dbmopen("./users","c") or die("Could not open DBM
;)"dbminsert($db,"PersianTools.com","30000
;)"dbminsert($db,"IranVig.com","6000
;)"dbminsert($db,"CodeVig.com","100
;)dbmclose($db
>?

:
PHP Code:
<?php
;)"$db=dbmopen("./users","c") or die("Could not open DBM
;)"dbmreplace($db,"PersianTools.com","464420
;)"dbmreplace ($db,"IranVig.com","62351
;)"dbmreplace ($db,"CodeVig.com","6463
;)dbmclose($db
>?

124

PHP

PHP Code:
$users=dbmfetch($db,"PersianTools.com");

:
PHP Code:
<table border=1 cellpadding ="5">
<tr>
<td align="center"> <b>product</b></td>
<td align="center"> <b>price</b> </td>
</tr>
<?php
$db = dbmopen( "./users", "c" ) or die( "Couldn't open DBM" );
$key = dbmfirstkey( $db );
while ( $key != "" )
{
$value = dbmfetch( $db, $key );
print "<tr><td align = \"left\"> $key </td>";
print "<td align = \"right\"> \$$value </td></tr>";
$key = dbmnextkey( $db, $key );
}
dbmclose( $db);
?>
</table>



PHP Code:
if ( dbmexists( $db, "PersianTools.com" ) )
print dbmfetch( $db, " PersianTools.com " );


PHP Code:
dbmdelete($db,"PersianTools.com");

125

PHP

:
PHP Code:
$array = array( 1, 2, 3, 4 );
$db = dbmopen( "./ test", "c" ) or die("Couldn't open test DBM");
dbminsert( $db, "arraytest", $array );
print gettype( dbmfetch( $db, "arraytest" ) );

:
PHP Code:
<?php
$products = array("PersianTools.com" => array( users=>"2200",stats=>"1500", color=>"blue" ),
"Iranvig.com" => array( users=>"2213",stats=>"1234",color=>"blue" )
);
$db = dbmopen( "./test", "c" ) or die("Couldn't open products DBM");
while ( list ( $key, $value ) = each ( $products ) )
dbmreplace( $db, $key, serialize( $value ) );
dbmclose( $db );
?>
</table>



.
PHP Code:
<?php
$dbh = dbmopen( "./users", "c" ) or die("Couldn't open test DBM");
if ( isset ( $delete ) )
{
while ( list ( $key, $val ) = each ( $delete ) )
{
unset( $prices[$val]);
dbmdelete( $dbh, $val );
}

126

PHP

}
if ( isset ( $prices ) )
{
while ( list ( $key, $val ) = each ( $stats ) )
dbmreplace( $dbh, $key, $val );
}
if ( ! empty( $name_add ) && ! empty( $stats_add ) )
dbminsert( $dbh, "$name_add", "$stats_add" );
?>
<html>
<body>
<form action="<? print $PHP_SELF; ?>" action="POST">
<table border="1">
<tr>
<td>delete</td>
<td>Users</td>
<td>stats</td>
</tr>
<?php
$key = dbmfirstkey( $dbh );
while ( $key != "" )
{
$price = dbmfetch( $dbh, $key );
print "<tr><td><input type='checkbox' name=\"delete[]\" ";
print "value=\"$key\">$/td>";
print "<td>$key</td>";
print "<td> <input type=\"text\" name=\"prices[$key]\" ";
print "value=\"$stat\"> </td></tr>";
$key = dbmnextkey( $dbh, $key );
}
dbmclose( $dbh );
?>

py

y@g

PHP

127
><tr
><td>&nbsp;</td

><td><input type="text" name="name_add"></td


><td><input type="text" name="stats_add"></td
></tr
><tr
>"<td colspan=3 align="right
>"<input type="submit" value="amend
></td
></tr
></table
></form

:

.

PHP

128


:
MySQL DataBase .
: LIMIT
LIMIT Select
DataBase Query .
LIMIT
LIMIT :
SELECT * FROM table LIMIT 5

Select 5 Table LIMIT



SQL ) (0
). (1
SELECT * FROM table LIMIT 5, 5

SQL 6 10 DataBase .
LIMIT
.
:$start

PHP

129

DataBase
. LIMIT
$start
.
PHP :
<?php
;"\$query = "SELECT * FROM table LIMIT \" . $start . \", 10
>?

query query 10 table start select


. start ) 10 (
URL
:
<?php
;if(!isset($start)) $start = 0
>?

start$ .

UPLOAD :MySQL
mysqldump txt
)Shell > mysqldump --opt DBname TBname > (c:\data.txt or /home/user/data.txt

PHP

130

1 table DB database table

- 2 database DB
)Shell > mysqldump --opt --databases DB1 DB2 > (c:\data.txt or /home/user/data.txt

- 3 database
)Shell > mysqldump --opt --all-databases > (c:\data.txt or /home/user/data.txt

table txt host


FTP.
MySQL
Shell > mysql database < data.sql

:
password & username MySQL
Shell > Mysql h host -u user -p database < backup-file.sql

:
host Shell .
PHP :
<?php
;)shell_exec(Mysql h host -u user -p database < backup-file.sql
>?

PHP

131

SQLite :
MYSQL RDBMS
SQLite .
MYSQL 2 3 .
SQLite PHP PEAR . PEAR SQLite

Pear SQLite download


.

[root@zaemis www]# pear download SQLite


)File SQLite-1.0.2.tgz downloaded (362412 bytes
[root@zaemis www]# pear install SQLite-1.0.2.tgz

SQLite DB PEAR .

<?php
;"\require_once "DB.php
;"\require_once \"DB/SQLite.php
;)($db = new DB_sqlite
>?

windows dll
download.
SQLite binary for Windows: snaps.php.net/win32/PECL_STABLE/php_sqlite.dll

php.ini ) php version 5


(.

PHP

132

); load the SQLite extension (UNIX


extension=sqlite.so
; windows users will want to use this version instead
; extension=php_sqlite.dll

:
SQLite MySQL . MySQL
SQLite ID password
.
(DSN (Data Source Name DSN .
. DSN ()connect .

<?php
($DSN = array
"database\" => getcwd() . \"/dbase/mydbase.db\",
\"mode\"=> 0644
;)
;)$db->connect($DSN
>?

permission
mode .
)( disconnect .

<?php
;)($db->disconnect

133

PHP

?>

:
MySQL . query()
.

<?php
$query =
"INSERT INTO guestbook (fname, lname, email, comments)
VALUES ('John', 'Smith', 'jsmith@example.org', 'Great Website!')\"
;
$result = $db->query();
?>

fetchRow()
. mysql_fetch_row

<?php
$query = "SELECT * FROM guestbook\";
$result = $db->query($query);
while (list($fname, $lname, $email, $comment) = $db->fetchRow(
$result))
{
echo \"<p><a href=\\"mailto:$email"
>$fname $lname</a> said:<br />\";
echo nl2br(htmlspecialchars(stripslashes($comment))) .
\"</p>\";
}

134

PHP

?>

:
: SQLite

<?php
require_once "DB.php\";
require_once \"DB/SQLite.php\";
$db = new DB_sqlite();
$DSN = array(\"database\" => getcwd() . \"/dbase/mydbase.db\");
$db->connect($DSN);
$query = \"SELECT * FROM guestbook\";
$result = $db->query($query);
while (list($fname, $lname, $email, $comment) = $db->fetchRow(
$result))
{
echo \"<p><a href=\\"mailto:$email"
>$fname $lname</a> said:<br />\";
echo nl2br(htmlspecialchars(stripslashes($comment))) .
\"</p>\";
}
$db->disconnect();
?>

SQL
. SQLite SQL92
( GRANT REVOKE ) embedded
( data type )

Mysite: Http://Bashiry.250free.com

Email: m.bashiry@gmail.com

PHP

135

SQL SQLite :
(http://www.sqlite.org/lang.html) http://www.sqlite.org/lang.html

SQLite .

http://www.sqlite.org/omitted.html) http://www.sqlite.org/omitted.html
http://www.sqlite.org/cvstrac/wiki?p=UnsupportedSql
http://www.sqlite.org/cvstrac/wiki?p=UnsupportedSql

SQLite: A Lightweight Alternative


by Timothy Boronczyk
The Swiss army knife of data storage
Wez Furlong

provided by Shilan

MySQL :
:
MySQL
.
PHP mSQL, ,FilePro ,dBase :

.MySQL,Oracle, PostgreSQL,Sybase
) Access (SQL Server PHP ODBC (Open

(DataBase Connectivity ) (ODBC Drivers .


MySQL DBMS
.

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

136

:MySQL
MySQL .

?<
;)$Conn=mysql_connect($Location,$Username,$Password
>?

. mysql_connect . ) (Host
. .
browser .

>?)<? mysql_select_db($database


. .

: MySQL

)<? if (mysql_creat_db($database,$Conn
;){ print(The database,$database,was { successfully created!<BR>n
else
};){ print(" The database,$database, could not be created! <BR>\n

>?

MySQL

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

137
?<

;)mysql_creat_db($database,$Conn
>?

. :
.
.

: MySQL
SQL .
query SQL query
:
;)mysql_db_query($database,$Query

. :

;"<?$Query=text for the query goes here in SQL format


))if (mysql_db_query($database,$Query
}
;)print( The query was successfully executed!<BR>\n
{
else
}
;)print(The query could not be executed!<BR>\n
>? {

:
query :

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

138

?<
$Query=CREATE table $Tablename(id INT PRIMARY KEY ,
;))Firstname char(12), Lastname char(15
>?

query ) id key Primary(


Firstname 12 Lastname 15 .

:
q uery :

>?;)<?$Query=INSERT into $Tablename(value1, value2, value3,

q uery
. query.

:
:

?<
;$query = "SELECT * FROM $Tablename
;)$result = mysql_query($query
;)$numrows = mysql_num_rows($result
))while($row = mysql_fetch_array($result

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

139
{

;echo \"You have $numrows user(s) in the database


;]echo \"ID number : $row[id
;]echo \"firstname: $row[firstname
;]echo \"Lastname: $row[lastname
}
>?

query Table
query Table
numrows ) while loop Table(
.

:
query UPDATE .
:

<?$Query=UPDATE $Tablename SET firstname=mina WHERE


>? ;firstname=maryam

query Table mina maryam


.

:
>?;)<?mysql_close($Conn
MySQL . MySQL

.
Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

140

query .
PHP q uery .
.
;) ( <?mysql_db_query >?;) ( <?mysql_query .
h elp
PHP .
Database
) Security (.
D atabase D atabase
.

) (:
Database Table !!.
Database Table Error
Mysql_Front Database Table .

) (Sh_javanmardi@srtnet.net

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

141

PHP

;)( Include .
.
: a.php
b.php
:
PHP Code:
// file name is b.php
<?php
;)"include("a.php
>?
//will Print message in a.php


.
.
:
PHP Code:
<?php
//this file name is a.php
;)$ret=(4+4
;return $ret
>?
PHP Code:
<?php
//this file name is b.php
;$flag=true

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

142

{ )if($flag
;)"$resualt=include("a.php
;"print " The Sum Of (4+4) Is $resualt
}
>?


a.php .
.

:
!
:
PHP Code:
<?php
))"if (file_exists("a.php
;"print "The File Exists
>?


PHP Code:
//Check if it's a file
<?php
)"if(is_file("a.php
;"print"yes this is file
>?
/*-----------------------------*/
//Check if Current Path is a dir
<?php
))"if(is_dir("/tmp
;"print"/tmp is valid
>?

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

143

is_readable , is_writeable , is_executeable



.

;)"Print filesize("a.php

.
; )( fileatime
a.php
:
PHP Code:
<?php
;)"$lasttime=fileatime("a.php
;"print "The File last time accessed in ".date("D d M Y g:i A",$lasttime).".
// Will Print Sat 14 jan 2006 10:30 Pm
>?

;)( filemtime ; )( fileatime


. ; )( filectime
.

:
;)" touch("file-path.txt

;)" unlink("file-path.txt .
:
.

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

144

:

. .
;)'f=fopen("file.txt",'r$


;)'f=fopen("file.txt",'w$

).(Append
;)'f=fopen("file.txt",'a$



PHP Code:
))'If ($fp=fopen("file.txt",'w
{
// codehaie marboot be viraiesh file
}

:
PHP Code:
;)"($fp=fopen("file.txt",'w')) or die("Could Not open file

Could Not open file


.
:
;)(fopen
Code //
;)(fclose

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

145


.
:
PHP Code:
<?php
;"$filename = "test.txt
;)"$fp = fopen( $filename, "r" ) or die("Couldn't open $filename
) ) while ( ! feof( $fp
{
;) $line = fgets( $fp, 1024
;">print "$line<br
}
>?

; )( feof
. ) fgets($fp,1024
1024 .
16 16
PHP Code:
<?php
;"$filename = "test.txt
;)"$fp = fopen( $filename, "r" ) or die("Couldn't open $filename
) ) while ( ! feof( $fp
{
;) $chunk = fread( $fp, 16
;">print "$chunk<br
}
>?

;) fread( $fp,16 16 .
)( fseek
PHP Code:

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

146

PHP

<?php
$filename = "test.txt";
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename");
$fsize = filesize($filename);
$halfway = (int)( $fsize / 2 );
fseek( $fp, $halfway );
$chunk = fread( $fp, ($fsize $halfway) );
print $chunk;
?>

.
fgets(); fgetc();
.

fopen("file.txt",'w');
Or
fopen("file.txt",'a');

fwrite();
. fputs();
:
PHP Code:
<?php
$filename = "test.txt";
$fp = fopen( $filename, "w" ) or die("Couldn't open $filename");
fwrite( $fp, "Hello world\n" );
fclose( $fp );
print "Appending to $filename<br>";
$fp = fopen( $filename, "a" ) or die("Couldn't open $filename");
fputs( $fp, "And Hello To You\n" );
fclose( $fp );
?>

Mysite: Http://Bashiry.250free.com

Email: m.bashiry@gmail.com

PHP

147

;)( flock

PHP Code:
---- Sharing

---- Exclusive

---- Release

:
mkdir() , rmdir .
.

PHP Code:
<?php
;"$dirname = "testdir
;) $dh = opendir( $dirname
) while ( gettype( $file = readdir( $dh )) != boolean
{
) ) "if ( is_dir( "$dirname/$file
;")print "(D
;">print "$file<br
}
;) closedir( $dh
>?

)( opendir .
) (D
)( closedir .

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

148

readdir


.

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

149

mail PHP
PHP
HTML ) (Attachment .
PHP PHP .
) ( outlook .
php.ini editor .

.
php.ini ] [mail function SMTP
SMTP mail.softhome.net

php.ini
:
:
][mail function
; Setup for Windows systems
SMTP = smtp.my.isp.net
sendmail_from = me@myserver.com

][mail function
; Setup for Linux systems
sendmail_path = /usr/sbin/sendmail -t
sendmail_from = me@myserver.com

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

150

restart
!

): (Plain Email
PHP !
! :

;)'mail('recipient@some.net','Subject',Your message here.

' 'recipient@some.net ' 'Subject 'Your message here.

.
PHP .
HTML .
mail system php.ini
) (reject } To {
PHP .
@
.
mail ) true false
( :

{ ))if (@mail($to, $subject, $message


;)'>echo('<p>Mail sent successfully.</p
{ } else
;)'>echo('<p>Mail could not be sent.</p
}

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

151

.
nonexistant.user@hotmail.com
mail true
PHP .

" " . :

mail('recipient1@some.net, recipient2@some.net',
;)''An email to two people', 'Message goes here.

mail header
!

HTML header :
PHP !
HTML !

HTML header .
header : ) . (message body
:

>Return-Path: <sender@elsewhere.com
Delivered-To: you@some.net
Received: ...several lines like this...
>From: Sender <sender@elsewhere.com
>To: You <you@some.net

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

152

Subject: A Simple Message


Date: Mon, 11 Feb 2002 16:08:19 -0500
Organization: Sender's Company
X-Mailer: Microsoft Outlook, Build 10.0.2616
?Hi there! <tap> <tap> Is this thing on

header . header

.
header header ) From:, To:, Subject:,

(Date:, etc ) (value . header


mail program mail server . header
-X ) X-Mailer:
(
: ) (value header
. .
) (blank line header
. .
mail PHP header PHP

header .
header X-Mailer: PHP 4.x .

mail('recipient@some.net', 'Subject', 'Your message here.',


;)''X-Mailer: PHP 4.x

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

153

From )
From php.ini ( . header From
:

mail('recipient@some.net', 'Subject', 'Your message here.',


;)""From: sender@some.net\nX-Mailer: PHP 4.x

header \n
) " " PHP
\n (.

Header
name <email>. :

mail('recipient@some.net', 'Subject', 'Your message here.',


"To: The Receiver <recipient@some.net>\n" .
"From: The Sender <sender@some.net>\n" .
;)""X-Mailer: PHP 4.x

To
header To: header .
Header CC: Bcc: :

mail('recipient@some.net, someone@some.net, metoo@some.net',


'Subject', 'Your message here.',
"To: The Receiver <recipient@some.net>\n" .
"From: The Sender <sender@some.net>\n" .
"cc: Interested <someone@some.net>\n" .

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

154

"Bcc: Me Too <metoo@some.net>\n" .


;)""X-Mailer: PHP 4.x

To cc Bcc

)
!(
:
mail PHP PHP 4,1,0 :Cc
:cc : :CC ...
!
:Bcc .
:Bcc header :Bcc
!

HTML!
: header
HTML.
mail('recipient@some.net', 'Subject',
'<html><body><p>Your <i>message</i> here.</p></body></html>',
"To: The Receiver <recipient@some.net>\n" .
"From: The Sender <sender@some.net>\n" .
"MIME-Version: 1.0\n" .
;)""Content-type: text/html; charset=iso-8859-1

Email: m.bashiry@gmail.com

Mysite: Http://Bashiry.250free.com

PHP

155

HTML Mime-Version: Content-type:


.
Mime-Version: standard extended mail format ).
Mime Multipurpose Internet Mail Extensions (
Content-type . Content-type: HTML
.

Text HTML ):(Mixed Messages


text html
html
.
) (mixed messages .


mixed message
) text .(html

) (mixed messag PHP


:

Date: Mon, 11 Feb 2002 16:08:19 -0500


>To: The Receiver <recipient@some.net
>From: The Sender <sender@some.net
Subject: A simple mixed message
MIME-Version: 1.0
;Content-Type: multipart/alternative
"boundary="==Multipart_Boundary_xc75j85x

156

PHP

This is a multi-part message in MIME format.


--==Multipart_Boundary_xc75j85x
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
This is the text portion of the mixed message.
--==Multipart_Boundary_xc75j85x
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html>
<body>
<p>This is the <b>HTML portion</b> of the mixed message.</p>
</body>
</html>
--==Multipart_Boundary_xc75j85x--

Header . MIME-Version: 1.0 message header


: Content-type: .

Content-Type: multipart/alternative;
boundary="==Multipart_Boundary_xxc75885"

multipart/alternative Content-type:
)
.(
. boundary set Content-Type

PHP

157

header Content-type:
header
.

" "==Multipart_Boundary_xc75j85x .
) boundary mail
text
(html
underline
boundary message
.

" "This is a multi-part message in MIME format.


!

boundary :

--==Multipart_Boundary_xc75j85x
"Content-Type: text/plain; charset="iso-8859-1
Content-Transfer-Encoding: 7bit
This is the text portion of the mixed message.

boundary dash
) (-- .

PHP

158

boundary text . message header


Content-Type Encoding . text Content-Type
) text/plain charset (iso-8859-1 Encoding bit7 )(plain ASCII text
header .
html :

--==Multipart_Boundary_xc75j85x
"Content-Type: text/html; charset="iso-8859-1
Content-Transfer-Encoding: 7bit
><html
><body
><p>This is the <b>HTML portion</b> of the mixed message.</p
></body
></html
header text Content-Type text/html

. ) (body html boundary :

--==Multipart_Boundary_xc75j85x--

) boundary dash (
) text (html
.
boundary
boundary :

PHP

159

;))($semi_rand = md5(time
;"$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x

unix timestamp MD5 .


boundary .
mixed message
PHP .
message boundary
.

):(File Attachments
mixed message Content-

Type message ) multipart/mixed .(multipart/alternative


: " Content-Type message " Content-

Type mail
Content-Type ) text html attachment Content-Type
mail Content-Type multipart/mixed(
header Content-Disposition
attachment :
Content-Disposition: attachment

PHP

160

form submit )
( .

:
http://www.webmasterbase.com/examples/phpemail/phpemail.zip

submit ) register_globals=off
(

// Read POST request params into global vars


;]'= $_POST['to
;]'= $_POST['from

$to
$from

;]'$subject = $_POST['subject
;]'$message = $_POST['message

upload $_FILES
.
// Obtain file upload vars
;]'= $_FILES['fileatt']['tmp_name

$fileatt

;]'$fileatt_type = $_FILES['fileatt']['type
;]'$fileatt_name = $_FILES['fileatt']['name

$to $from valid )


( ) regular expressions (.

header from :
;"$headers = "From: $from

) $header mail (

161

PHP

. upload path $fileatt


: is_uploaded_file
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

. $data
: multipart/mixed header

// Generate a boundary string


$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

:
text

// Add a multipart boundary above the plain message


$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

PHP

162

Base64 )
( . Base64 encoding
. PHP Base64 encoding :

// Base64 encode the file data


;))$data = chunk_split(base64_encode($data

. :

// Add file attachment to the message


$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
;""--{$mime_boundary}--\n
}

attachment
.

mail :

// Send the message


;)$ok = @mail($to, $subject, $message, $headers
{ )if ($ok
;">echo "<p>Mail sent! Yay PHP!</p
{ } else

PHP

163

;">echo "<p>Mail could not be sent. Sorry!</p


}

:
.
mail .
:

header

html

html plain text .

....


html .
encapsulate.
RFC for MIME extensions
(RFC 2045) .

PHP

164


:

.
Log

PHP .

.
MySQL
. Session
.
PHP .

(Session Logging) : 1
:
<?php
;)(session_start
{))'if(!session_is_registered('counted
;]'$agent = $_SERVER['HTTP_USER_AGENT
;]'$uri = $_SERVER['REQUEST_URI
;]'$user = $_SERVER['PHP_AUTH_USER
;]'$ip = $_SERVER['REMOTE_ADDR
;]'$ref = $_SERVER['HTTP_REFERER
;)'$dtime = date('r
{)"\"\

==if($ref

165

PHP

$ref = \"None\";
}
if($user == \"\"){
$user = \"None\";
}
$entry_line = \"$dtime - IP: $ip | Agent: $agent | URL: $uri | Referrer: $ref | Username: $user
n\";
$fp = fopen(\"logs.txt\", \"a\");
fputs($fp, $entry_line);
fclose($fp);
session_register('counted');
}
?>

:
<?session_start(); ?>

Session PHP
.
<?if(!session_is_registered('counted')){ ?>

Counted Session Session


.
<?
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$user = $_SERVER['PHP_AUTH_USER'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$dtime = date('r');
?>

PHP

166

PHP
.
.
>? ;]'<? $agent = $_SERVER['HTTP_USER_AGENT

.
>? ;]'<? $uri = $_SERVER['REQUEST_URI

URL .
>? ;]'<? $ip = $_SERVER['REMOTE_ADDR

IP .
>? ;]'<? $ref = $_SERVER['HTTP_REFERER

.
>? ;)'<? $dtime = date('r

$dtime
.
?<
{)if(!$ref
;"\$ref = \"None
}
{)if(!$user
;"\$user = \"None
>? }


None .

PHP

167

<?php
;"\$entry_line = \"$dtime - IP: $ip | Agent: $agent | URL: $uri | Referrer: $ref | Username: $user n
;)"\$fp = fopen(\"logs.txt\", \"a
;)fputs($fp, $entry_line
;)fclose($fp
;)'session_register('counted
>?

.
$entry_line
.
fopen
$fp . fputs
.
fclose ;)' session_register('counted
.
1 . 2
.

(Logging All Page) : 2


1
Session . 2
Log.

<?php
;]'$agent = $_SERVER['HTTP_USER_AGENT
;]'$uri = $_SERVER['REQUEST_URI

168

PHP

$user = $_SERVER['PHP_AUTH_USER'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$dtime = date('r');
if($ref == \"\"){
$ref = \"None\";
}
if($user == \"\"){
$user = \"None\";
}
$entry_line = \"$dtime - IP: $ip | Agent: $agent | URL: $uri | Referrer: $ref | Username: $user
n\";
$fp = fopen(\"logs.txt\", \"a\");
fputs($fp, $entry_line);
fclose($fp);
?>

.
<?php
session_start();
if(!session_is_registered('counted')){
session_register('counted');
}
?>

. Log
www/htdocs/logs.txt/ $fp

Permission CHMOD 755 .


[12] .

PHP

169


:PHP
) : (

:
http://www.iranphp.net/modules/sections/index.php?op=viewarticle&artid=19

Copy Paste jalali.php


.

:
>?;"\<?php require_once "jalali.php

gregorian_to_jalali jalali_to_gregorian
.
.
-1 :
gregorian_to_jalali . 3
.
25-11-2002
.
:

PHP

170

;'<?php $gdate='2002-11-25
;) list( $gyear, $gmonth, $gday ) = preg_split ( '/-/', $gdate
>?

preg_spilit $gdate .
syntax 2 .
delimiter regex (/) slash
.
list
. .
gregorian_to_jalali :

<?php
;)list( $jyear, $jmonth, $jday ) = gregorian_to_jalali($gyear, $gmonth, $gday
>?

3
grogorian_to_jalali list
.
. :
<?php
;$jdate = $jyear."/\".$jmonth.\"/\".$jday
>?

$jdate
:

1381/9/4

171

PHP

<?php
list($gyear, $gmonth, $gday ) = preg_split ('/-/', date("Y-m-d\"));
list( $jyear, $jmonth, $jday ) = gregorian_to_jalali($gyear, $gmonth, $gday);
$jdate = $jyear.\"/\".$jmonth.\"/\".$jday;
?>

date
. $jdate
:
<?php
function get_jalali_date( $gdate='now' )
{
if ( $gdate == 'now' )
{
list($gyear, $gmonth, $gday ) = preg_split ('/-/', date("Y-m-d\"));
}
else
{
list( $gyear, $gmonth, $gday ) = preg_split ( '/-/', $gdate );
}
list( $jyear, $jmonth, $jday ) = gregorian_to_jalali($gyear, $gmonth, $gday);
return $jyear.\"/\".$jmonth.\"/\".$jday;
}
?>

PHP

172

get_jalali_date
.

.
25-11-2002
:
>?;)"\<?php $jdate = get_jalali_date("2002-11-25

:
>?;)(<?php $jdate = get_jalali_date

.
-2 :
jalali_to_gregorian .
.
.
1381/9/4 :
<?php
;"\$jdate="1381/9/4
;) list( $jyear, $jmonth, $jday ) = preg_split ( '/\//', $jdate
;)list( $gyear, $gmonth, $gday ) = jalali_to_gregorian($jyear, $jmonth, $jday
;$gdate = $gyear.\"-\".$gmonth.\"-\".$gday
>?

PHP

173

/ .
preg_split ) delimiter ( regex
/ / delimiter \ ) ( slash back
.

get_gregorian_date . .

PHP

174

PHP

PHP . open source

PHP
.
PHP .


.1
.2
.3 PHP

.1.3
.2.3
.4 PHP

.5

PHP

175

.1
PHP . open source

PHP
. PHP

.

PHP
. PHP .

C PHP
. C .
.
command line .

.2
PHP .
www.gnu.org
.
.

bison

flex

m4

autoconf

automake

PHP

libtool

gcc

make

cvs www.cvshome.org

176

PHP CVS Concurrent

Version System http://cvs.php.net .


Y X Z PHP
. PHP
php-src .

cvs -d :pserver:cvsread@cvs.php.net:/repository checkout -r php_X_Y_Z php-src

.3 PHP

.1.3
PHP )( salam
salam STRING .

PHP ext
. salam.def
" " )(
. .
:

"string salam(string arg) return "salam ARG

PHP

177

PHP ext_skel ext


.
.

./ext_skel extname=salam proto=salam.def

extname
. :

config.m4

CREDITS

EXPERIMENTAL

salam.c

salam.php

Makefile.in

php_salam.h

tests

.2.3

salam.c .
:

][/* {{{ salam_functions


*
][* every user-visible function must have an entry in salam_functions
*/

178

PHP

function_entry salam_functions[] = {
PHP_FE(confirm_salam_compiled, NULL) /* for testing; remove later */
PHP_FE(salam, NULL)
{NULL, NULL, NULL} /* must be the last line in salam_functions[] */
};
/* }}} */

salam.def salam.def
.
confirm_salam_compiled
.


. .

zend_module_entry salam_module_entry = {
STANDARD_MODULE_HEADER,
"salam",
salam_functions,
PHP_MINIT(salam),
PHP_MSHUTDOWN(salam),
PHP_RINIT(salam), /* replace with NULL if no request init code */
PHP_RSHUTDOWN(salam), /* replace with NULL if no request shutdown code */
PHP_MINFO(salam),
"0.1", /* replace with version number for your extension */
STANDARD_MODULE_PROPERTIES
};

PHP_RINIT(salam)

: NULL PHP_RSHUTDOWN(salam)

PHP

179

{ = zend_module_entry salam_module_entry
STANDARD_MODULE_HEADER,
"salam",
salam_functions,
PHP_MINIT(salam),
PHP_MSHUTDOWN(salam),
NULL,
NULL,
PHP_MINFO(salam),
"0.1", /* replace with version number for your extension */
STANDARD_MODULE_PROPERTIES
;}

) PHP_MINIT(salam ) PHP_MSHUTDOWN(salam
SUCCESS . :

{ )PHP_MINIT_FUNCTION(salam
;return SUCCESS
}
{ )PHP_MSHUTDOWN_FUNCTION(salam
;return SUCCESS
}

salam C

. return
) .
(.

)/* {{{ proto string salam(string arg


return "salam ARG" */
)PHP_FUNCTION(salam
{

180

PHP

char *arg = NULL;


int argc = ZEND_NUM_ARGS( );
int arg_len;
if (zend_parse_parameters(argc TSRMLS_CC, "s", &arg, &arg_len)
== FAILURE)
return;
// YOUR CODES SHOULD COME HERE
}
/* }}} */

PHP PHP_FUNCTION
. PHP PHP
:

PHP_FUNCTION(salam) {
char *arg = NULL, *sout;
int argc = ZEND_NUM_ARGS( );
int arg_len, len;
if (zend_parse_parameters(argc TSRMLS_CC, "s/", &arg, &arg_len)
== FAILURE)
return;
strcpy(sout, "salam ");
strcat(sout, arg);
len = strlen(sout);
RETURN_STRINGL(sout, len, 1);
}

return . len sout


arg sout sout " salam "

PHP

181

sout . sout ) RETURN_STRINGL


ZEND API
( . RETURN_STRINGL
.


. salam.c
config.m4 .
comment .
) (:

PHP_ARG_ENABLE(salam, whether to enable salam support,


)]Enable salam support

[ --enable-salam

PHP
. .

.4 PHP
PHP buildconf

PHP . :

./buildconf

. PHP
. --enable-salam
configure PHP . make PHP .

PHP

182


PHP :

./configure --enable-salam
make
make install

PHP .
. php test :
<?php
;)'echo salam('donya
>?

. " "salam donya


.
/usr/bin/php q test.php

.5
open source
.
PHP PHP
.
PHP
[11] .

183

PHP

:
www.prdev.com(1
www.majidonline.com(2
www.pardise.net(3

Matt Zandstra 24 PHP ( 4


www.PersianTools.com(5

www.barnamenevis.org(6
sitepoint.com(7
www.iranphp.net (8

http://www.omidpc.com (9

184

PHP

14 http://www.ccwmagazine.com/CCW (10
, "Programming PHP", O'reilly, Kevin, Rasmus and Tatroe, Lerdorf (11
March 2002

http://www.fekrinejat.com (12
( 13
dev.ir(14
http://www.phpmystery.com (15

You might also like