Вы находитесь на странице: 1из 48

'05 15

PHP
: dbtree phpMyAdmin
Smarty

2004 , www.phpinside.ru
/

. dbtree...................................................................................................................4
phpMyAdmin ..................................7

Smarty ....................................................................................................................11
proxy- PHP . ................................17

HOWTO PHP...................................................................................................................19
PHP............................................................................41
PHP , .................................................................................................................45


, PHP- 2006 .
.


PHP -: ,
PHP, .

PHP Inside #15


: , ,
, DD-.
PHP : -
, .
PHP: , PHP6, ,
Unicode.
-
( ).
Test driven development (TDD) 2.5 : , ,
, , , SimpleTest
Selenium.
PHP 0.5 : - ,
PHP C, Zend API.



, (30-60
.), 1 2006 (
.doc .txt ) - , c-
.
-
.

2

, , 15 -
,
.
:
2006@phpconf.ru.
, -

PHP Inside #15


, , ,
, - :
http://www.phpconf.ru/
-, ,
- (~24 .), - -
.
- -
c (Apache+MySQL+PHP5+ cvs).


1 , .
20
,
.
1
.
1 -
( , 3- ).
- - (3), (2)


: 2006@phpconf.ru.
: sp2006@phpconf.ru.
PHP-
http://www.phpconf.ru/

3

. dbtree

. dbtree
: [nw]

PHP Inside #15


-
PHP-

-
dbtree, -
( ADODB, phpGacl) PHP -
, -
.
nw: dbtree.
?
(): - phpDBTree
. , -
,
. - .
:
.
.
,
, ,
. , -
, ?
, .
, - -
.
,
PHPClub .
- , , -
, , ,
, ... , -
.
, , , ,
. (2.1) , -
, , .
-
, , !
nw: ?
: , , ,
, , . , :
, ;

4

. dbtree

,
Nested Sets, -
, ;
,

PHP Inside #15


, -
, , ,

;
, (,
) ADODB.
,
PEAR::DB -
.
, , !
nw: -
? - feauture requests ,
?
, , -
, ,
phpclasses.org. ,
.

. -
,
. ,
, , ,
, , .
. -
.
nw: ,
, ADODB. ?
?

, . C -
-
. -,
, ,
, ; -, , -

, -
, ( )
, ( ) ,
.
nw: --
? -
?
,
,
. .
.

5

. dbtree

-
. , -
. ? , ,
.

PHP Inside #15


nw: ? (, -, -
, , PHP etc)
, : - , -
. CVS, XML, MySQL,PostgreSQL, Smarty, Curl, GetText, Iconv .
nw: , .
?
, , ,
?
, - . -
, , .
nw: .
? ? ? -
?
, .
,
. , !
, .
. , , !
104 FM . ,
. ,
-
, !
,
.
, , , ! -
, !

6

phpMyAdmin


phpMyAdmin

PHP Inside #15



: Arjen Lentz
:

Arjen Lentz - -
MySQL AB

(Marc Delisle) -
phpMyAdmin, - MySQL.
MySQL, phpMyAdmin ( !) -
.
Arjen: , , ,
.
Marc:
Cegep de Sherbrook, , .
1980 , , -
, Cobol Oracle,
.. 1998 , ,
PHP. , ,
.
Arjen: MySQL
?
Marc: mSQL (Mini SQL )
( ) -
. , 1998 MySQL.
Arjen: MySQL?
Marc: .
Arjen: -, ?
Marc: ! .
Arjen: phpMyAdmin... ?
Marc: MySQL-
Webadmin, phpMyAdmin, - ,
. -
, .
(Tobias Ratschiller), - MySQL 4.1
, - 5.0,
phpMyAdmin.

7

phpMyAdmin


, -.
1998 2000 phpMyAdmin ( 1998
). 2000

PHP Inside #15


2001 phpMyAdmin -
sourceforge.net. 2001 -
2.2.0 2001.
Arjen: phpMyAdmin? ""
?
Marc: , -
" ". . - 2002
: (- 10
), (), (), phpMyAdmin
(), (), (- ,
). () .

Arjen: ,
( )
.
?
Marc:
, , 2.6.0 MIME-based .
2005 . , -
MySQL 4.1 5.0, .
Arjen: , phpMyAdmin -
? , phpMyAdmin --
MySQL -
Linux, Novell NetWare .
Marc: -
blob -
PDF. , ,
phpMyAdmin -
. , -
MySQL
-.
, , -
. 2002
: 10 phpMyAdmin ,
.
: , -
50 -
.
Arjen: ! ?
, ,
phpMyAdmin -?
Marc: , , . -

. , 2005 220 -
320 .

8

phpMyAdmin

Arjen: ! .
phpMyAdmin?
Marc: , 2005
, , - 220 - 320

PHP Inside #15


.
- -
, .
Arjen: -
! , -
phpMyAdmin ?
Marc: :) 7-10 .
,
phpMyAdmin , ,
. . -
http://phpmyadmin.net.
, ,
- 250- .
Arjen: Open Source ?
Marc: , phpMyAdmin :)
Arjen: ;-)
, phpMyAdmin
? ?
Marc: ... - ( -
) . , -

.
, .
Arjen: MySQL?
Marc: , phpMyAdmin -
MySQL, -
phpMyAdmin :)
Arjen: . - , -
?
Marc: , SHOW
CREATE TABLE SHOW FULL FIELDS, .
Arjen: , INFORMATION_SCHEMA MySQL 5.0
?
Marc: , ,
, !
Arjen: . MySQL AB - ?
Marc: phpMyAdmin ,
MySQL AB.
phpMyAdmin
MySQL.
MySQL , phpMyAdmin
MySQL AB. MySQL AB.

9

phpMyAdmin

, MySQL ,
- . MySQL AB.
, --
, MySQL.

PHP Inside #15


Arjen: .
.
Marc: , LAMP :)
Arjen: - . -
- .

10

Smarty

Smarty

PHP Inside #15


:

Smarty , ,
-

, -
. , . -
Smarty ,
,
.
, Smarty,
, -
. , -
, ,
, -
.


Smarty
. -
.
?
, -
, , - , -
- .
, , -,
, -, -
. - (), -
- , , -
, , ,
.
?
1. () ,
, , -
-
;
2. , , -
, ;
3.
,
.
.

11

Smarty


, , ,

PHP Inside #15


, , .
? Smarty . , -
.
Smarty:
<?php
/** Smarty */
define('SMARTY_DIR', '/smarty/');

/** Smarty */
define('TEMPLATES', '/templates/');

/** Smarty */
define('TEMPLATES_C', '/templates_c/');

require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty;
$smarty->compile_check = TRUE;
$smarty->force_compile = TRUE;
$smarty->template_dir = TEMPLATES;
$smarty->compile_dir = TEMPLATES_C;
$smarty->plugins_dir[] = LIBS_PATH;
$smarty->caching = FALSE;
?>

, Smarty . ,
?
, :
1. adodb ;
2. session ;
3. errors ;
4. variables $_GET $_POST.
, (
). , -
:
<?php
require_once(CLASSES_DIR . '/variables.class.php');
$vars = new Variables();

require_once(CLASSES_DIR . '/errors.class.php');
$errors = new Errors($smarty);

require_once(CLASSES_DIR . '/security.class.php');
$security = new Security($adodb);

// ...
?>

-
.

12

Smarty

Smarty:
<?php

PHP Inside #15


$smarty->register_object('adodb', $adodb);

$smarty->register_object('vars', $vars);

$smarty->register_object('errors', $errors);

$smarty->register_object('security', $security);

?>

Smarty register_object().
, . -
.


Smarty - ,
. -
{component}. Smarty -
:
<?php
// Smarty function Component
//
// @author Feskov Kuzma

function smarty_function_component($params, &$smarty) {


$adodb = &$smarty->get_registered_object('adodb');
$vars = &$smarty->get_registered_object('vars');
$errors = &$smarty->get_registered_object('errors');
$security = &$smarty->get_registered_object('security');

if (empty($params['name'])) {
$params['name'] = 'site_view';
}
if (is_file(ADMIN_LIBS_PATH . '/' . $params['name'] . '.component.php')) {
require(ADMIN_LIBS_PATH . '/' . $params['name'] . '.component.php');
} else {
echo 'Component <strong>' . $params['name'] . '</strong> not found';
}
unset($adodb, $errors, $security, $vars);
}
?>

, . -, -
{component}, -, (get_registered_object())
.
. ,
{component}. -
, ,
{component} , . ,
, 'site_view'.

13

Smarty

,
? , -
:

PHP Inside #15


{component name=' ' var1=' '
var2=' '}

, , name, var1, var2


. Name, , ,
, -
, , . , -
, , ,
, . -
, -
$params, , -
.
, :
name.component.php

name name.
, -
. name , -
, .
, -
Smarty $smarty.
, (assign) $smarty, -
, () , ,
- ,
, , , -
, - .
-
-
. , , ,
, .
, .
- , , -
, .


, .
, , -
, .
,
, -
- .
, .
5 .

14

Smarty

:
{component name='news' action='view' count=5 tpl='news_mainpage.tpl'}

PHP Inside #15


name , action ,
, count , tpl
, .
, :
<?php
// News component
//
// @author Feskov Kuzma

//
//
require_once(CLASSES_DIR . '/news.class.php');
$news = new News($adodb);

//
switch ($params['action']) {
// --- View news ---
case 'view':
// 5
$data = $news->NewsList($params['count']);
if (false === $data) {
//
$errors->ComponentErrPrint(' news, ' . $params
['action'], $news->ERROR);
}

// -
if(is_file(TEMPLATES . '/' . $params['tpl'])) {
//
$smarty->assign('data', $data);
$smarty->display($params['tpl']);
} else {
//
$errors->ComponentErrPrint(' news, ' . $params
['action'], ' ');
}
break;
// --- Default action ---
default:
// ,
$errors->ComponentErrPrint('news_view', ' ');
break;
}
?>

. -
.
, -
, , -
.

15

Smarty


: prepare -

PHP Inside #15


, -
() , view -
- .
, prepare
view.
<?php
// --- prepare ---
$smarty->assign('prepare_data', $prepare_data);
?>

, prepare -
(assign) , 'prepare_data'.
:
<!-- -->

<!-- prepare -->


{component name='prepare'}

<!-- view -->


{component name='view' prepare_data=$prepare_data}

, prepare,
view. view
prepare.
<?php
// --- view ---
print_r($params['prepare_data']);
?>

, view
$params, prepare_data.


,
Smarty : Smarty -
, , .
, ,
, -
, .
, ()
,
.
-
, .
, .

16

proxy- PHP .

proxy-
PHP .

PHP Inside #15



:

, -
, , -

PHP

, , ( -
), . ,
, .., ..

fopen(), include()
, PHP,
allow_url_fopen. On (-), include(), fopen()
HTTP, FTP ..
? , , , -
, . :

(.. / )
, http://www.somehost.com/, ,
. , -
. . PHP -
, ,
, . , ,
PHP . :
<?
$html = implode('', file('http://' . $_GET["site"]));
echo $html;
?>

17

proxy- PHP .

http://www.mysite.prov.ru/test.php?site=somehost.com/, -
http://somehost.com/, -
.

PHP Inside #15


, HTTP . -
(.. -
). allow_url_fopen
php.ini ( off).

URL
. ,
,
. , cURL. .
HTTP . -
, -
. -
HTTP , . :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_GET["site"]);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>

.
cURL. -,
. , ,
.

18

HOWTO PHP

HOWTO PHP
: (John Lim)
:

PHP , -
,

, PHP -
, , PHP
, PHP
. , -

PHP Inside #15


. PHP
, .
: http://phplens.com/lens/php-book/optimizing-debugging-php.php


, ,
.
. , ,
, , , ,

.
, A.php , B.php -
, . -
, A.php , , -
B.php , A.php .

19

HOWTO PHP

. -
, ,
250 HTML, .
, : hare.php,
, tortoise.php,
, . Tortoise.php ,
,
.
Hare.php 0.04 , 10 .
Tortoise.php 0.06 5 . -
100 99% . ,
( ).
10 , hare.php (10 10
100). , tortoise.php 50 -
. 11 hare.php ,
. , -

PHP Inside #15


. hare.php -
0.08 . tortoise.php
0.06 .
, :
CPU- CPU- CPU-


Hare.php 0.04 0.40 0.88 (
)
Tortoise.php 0.06 0.60 0.66

, , -
.
PHP
, ,
.


hare tortoise ( ) . -
hare , tortoise. ,
, , -
, :

, , . , , 10
, 1 .
33 , 33
.
, ,
(DNS), .

20

HOWTO PHP


, , -
HTML , , -
, . , web-,
PHP, . -
-
.

Shared Memory ( )
, -
, , .
, , -
, , , ,
.

PHP Inside #15


50-100 ,
. -
. ,
, .
, .
UNIX .
Linux -
, , .
hdperm, Linux .


, , Windows, -
. , CGI ,
, . PHP multi-threaded ( -
) (: PHP -
).
. -
, ,
( ) X-Windows. Windows-
Microsoft Find Fast ( ), screensaver'.
100% .
, -
, , ,
, . Unix: ,
SSH. ,
:
, , telnetd, inetd, atd, ftpd, lpd, sambad
sendmail
portmap NFS
xfs, fvwm, xinit, X
,
, /etc/init* /etc/rc*/init* .

21

HOWTO PHP

, . ,
.


, , -
. , -
SQL .

?
, ,
. , -

. -
.
,

PHP Inside #15


, -
, . -
, , -
,
, , .
. , -
, 100 000 , 100
, . - -
: , -
, , -
, .

PHP
, , PHP -
web-, Apache 1.3 IIS.
PHP ,
Apache 2.0 Apache 1.3, , PHP
.

Apache 1.3/2.0
Unix Windows , -
. Apache 1.3 pre-forking () -
. ,
HTTP . -, ,
. HTTP
, , . -
HTTP , -
, . -
, Apache .
, -
.

22

HOWTO PHP

, . , -
, , , PHP -
, , Apache -
. Apache
.
Apache 2.0 (multi-threaded).
, .
, PHP , GD IMAP. Apache 2.0.47
(23 2003).
Apcahe httpd.conf.
:

MaxClients 256 ,
. 256
,

PHP Inside #15


256 HTTP .
.
StartServers 5 ,
.
MinSpareServers 5 ,
.
, 1
, 2 , 4
, ,
32 .
MaxSpareServers 10 ,
,
.
MaxRequestsPerChild 0 HTTP
.
0,
. 100
10000,
.

:
MinSpareServers 32
MaxSpareServers 64
Apache Windows . ,
, Apache . . -
: ThreadsPerChild -
50. ,
Apache.
Winodws , HTTP
, , 50. , -
, 256 1024.

23

HOWTO PHP

, :

SendBufferSize ( ),
TCP/IP .

,
. ,

. TCP/IP
.
KeepAlive (on/off) on HTTP , HTTP
.
Keep-alive ,
. keep-alive
,
socket () HTTP .

PHP Inside #15


,
(images),
.
.
KeepAliveTimeOut 15 -.

.
.
,
.
MaxKeepAliveRequests 100 - ,
.
, MaxClients ThreadsPerChild.
TimeOut 300 ,
. ,
.
LimitRequestBody 0 PUT POST. 0 .

DNS htaccess -
Apache, :
# DNS : PHP IP

HostnameLookups off

# htaccess

<Directory />

AllowOverride none

</Directory>

24

HOWTO PHP

,
FollowSymLinks SymLinksIfOwnerMatch, -
lstat():
Options FollowSymLinks
#Options SymLinksIfOwnerMatch

IIS
,
. IIS. (Performance Tab).
,
. (Performance Tab).
,
.
(Performance Tab).

PHP Inside #15


900 .
, . (Web
Site Tab)
HTTP IIS 5
, HTML .
.
.
HTTP .

(
),
(Properties). Service,
Compress application files
, Compress
static files
.

, , web-
. Home Directory Application Protection -
. , -
IIS, IIS
, , .
PHP CGI
ISAPI Application Protection hight ().
regedit.exe IIS 5, -
HKEY_LOCAL_MACHINESYSTEM
CurrentControlSetServicesInetinfoParameters.
MemCacheSize , IIS

. , ISS
50% .
,
IIS.
.

25

HOWTO PHP

MemCachedFileSize ,
.
.
262,144 (256 ).
ObjectCacheTTL ( ),

.
30,000 (30 ).
MaxPoolThreads .
, CGI
.
4. ,
PHP CGI.
ListenBackLog
(Keep alive), ISS
.

PHP Inside #15


15,
,
.
255.

Windows: IIS FastCGI


, ,
PHP Windows IIS FastCGI. CGI -
. , CGI
. FastCGI -
, CGI -
, -
.
FastCGI IIS ,
EasyWindows PHP Installer (http://phplens.com/phpeverywhere/easywindows).
PHP, FastCGI Turck MMCache .
PHP Apache 1.3/2.0.

PHP 4 Zend Engine


Zend Engine -
, -
PHP 4. Zeev Suranski Andi
Gutmn, Zend Engine
. PHP
:

26

HOWTO PHP

PHP Zend Engine opcode. Opcode -


. opcode
HTML . Opcode .
, .
, PHP .
.

PHP Inside #15


PHP Zend Engine opcode. Opcode
, Zend
Optimizer. , PHP
50%.
, , opcode .
, :
Zend Accelerator ( Zend Cache),
. opcode Zend Optimizer
Zend Accelerator. Opcode , -
. , ,
10 200%.

opcode?
Zend Accelerator. , Zend Engine.
. : http://zend.com.
,
,
PHP .
Turck MMCache (http://turck-mmcache.sourceforge.net/) .
eAccelerator, mmcache .

27

HOWTO PHP

Alternative PHP Cache http://apc.communityconnect.com/.


PHP Accelerator http://www.php-accelerator.co.uk/.
AfterBurner Cache http://www.bwcache.bware.it/.

,
PHP , ,
. , HTML
HTML. -
, -
HTML. HTML PHP Zend Engine -
PHP . -
. PEAR Cache, .
Smarty.
, , HTML, , . -
:

PHP Inside #15


<?php
ob_start("ob_gzhandler");
:::
:::
?>

HTML , 50-80%, -
.
, .

HTML PEAR Cache


PEAR Cache ,
HTML .
PEAR Cache HTML
. Output buffering class, -
start() end():
<?php
require_once("Cache/Output.php");

$cache = new Cache_Output("file", array("cache_dir" => "cache/") );

if ($contents = $cache->start(md5(" !"))) {

#
#
#
print $contents;
print "<p> </p>";

} else {

#
# ,
#
print "<p> :</p>"; #
print "<p>Stand and deliver</p>"; #
print $cache->end(10);
}
?>

28

HOWTO PHP

, , -
PEAR: Cache Lite (http://pear.php.net/package/Cache_Lite);
, memcached (http://www.danga.com/memcached/).

Cache -
. : , , ( :
pear/Cache/Container). Ulf Wendel , -
. -
. : cache_dir ,
filename_prefix -
. , .
- ,
(id) . , md5("
!").
start() .
, . echo

PHP Inside #15


print , , end().
end() .
end() . -
Unix ,
, 0 24 .
PEAR Cache
. Cache :
<?php
require_once("Cache.php");

$cache = new Cache("file", array("cache_dir" => "cache/") );


$id = $cache->generateID("' ");

if ($data = $cache->get($id)) {

print " .<br>: $data";

} else {

$data = " ...";


$cache->save($id, $data, $expires = 60);
print " .<br>";

}
?>

save(). -
,
generateID(). -
serialize() save().
. -
Unix , , 0
24 . -
get().

29

HOWTO PHP


-
. , , -
, ,
.
-
, , HTTP .
Unix ab (
apachebench), Apache. flood
(http://httpd.apache.org/test/flood). Windows NT/2000
Microsoft's free Web Application Stress Tool (http://webtool.rte.microsoft.com).
HTTP
, -
.
, ,

PHP Inside #15


Unix "vmstat1". -
, , . -
"top d 1"

.
Windows 2000 Performance Monitor Task Manager
.

HTTP ,
microtime(), . -
, :
<?php
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

$time = getmicrotime();

#
#
#

echo "

: " . getmicrotime() - $time . " ";


?>

, -
: APD (http://www.linuxjournal.com/article.php?sid=7213) Xdebug
(http://xdebug.derickrethans.nl/).
: http://phplens.com/phpeverywhere/node/view/52.

30

HOWTO PHP


, , -
. , 5 -
PHP , SQL .
: Apache 1.3.20, PHP 4.0.6 Red Hat 7.2 Linux. :
Pentium III 933 Mhz, 1Gb RAM. HTTP testmysql.php.
20 MySql , -
. ,
.
ab. -
ab 1000 (-n1000), 10 (-c10).
:
# ab -n1000 -c10 http://192.168.0.99/php/testmysql.php
This is ApacheBench, Version 1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-1999 The Apache Group, http://www.apache.org/

PHP Inside #15


Server Software: Apache/1.3.20
Server Hostname: 192.168.0.99
Server Port: 80

Document Path: /php/testmysql.php


Document Length: 25970 bytes

Concurrency Level: 10
Time taken for tests: 128.672 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 26382000 bytes
HTML transferred: 25970000 bytes
Requests per second: 7.77
Transfer rate: 205.03 kb/s received

Connnection Times (ms)


min avg max
Connect: 0 9 114
Processing: 698 1274 2071
Total: 698 1283 2185

, -
"top d 1". "d 1" -
, 1 . -
.
10:58pm up 3:36, 2 users, load average: 9.07, 3.29, 1.79
74 processes: 63 sleeping, 11 running, 0 zombie, 0 stopped
CPU0 states: 92.0% user, 7.0% system, 0.0% nice, 0.0% idle
CPU1 states: 95.0% user, 4.0% system, 0.0% nice, 0.0% idle
Mem: 1028484K av, 230324K used, 798160K free, 64K shrd, 27196K buff
Swap: 2040244K av, 0K used, 2040244K free 30360K cached

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND
1142 apache 20 0 7280 7280 3780 R 21.2 0.7 0:20 httpd
1154 apache 17 0 8044 8044 3788 S 19.3 0.7 0:20 httpd
1155 apache 20 0 8052 8052 3796 R 19.3 0.7 0:20 httpd
1141 apache 15 0 6764 6764 3780 S 14.7 0.6 0:20 httpd
1174 apache 14 0 6848 6848 3788 S 12.9 0.6 0:20 httpd
1178 apache 13 0 6864 6864 3804 S 12.9 0.6 0:19 httpd
1157 apache 15 0 7536 7536 3788 R 11.0 0.7 0:19 httpd

31

HOWTO PHP

1159 apache 15 0 7540 7540 3788 R 11.0 0.7 0:19 httpd


1148 apache 11 0 6672 6672 3784 S 10.1 0.6 0:20 httpd
1158 apache 14 0 7400 7400 3788 R 10.1 0.7 0:19 httpd
1163 apache 20 0 7540 7540 3788 R 10.1 0.7 0:19 httpd
1169 apache 12 0 6856 6856 3796 S 10.1 0.6 0:20 httpd
1176 apache 16 0 8052 8052 3796 R 10.1 0.7 0:19 httpd
1171 apache 15 0 7984 7984 3780 S 9.2 0.7 0:18 httpd
1170 apache 16 0 7204 7204 3796 R 6.4 0.7 0:20 httpd
1168 apache 10 0 6856 6856 3796 S 4.6 0.6 0:20 httpd
1377 natsoft 11 0 1104 1104 856 R 2.7 0.1 0:02 top
1152 apache 9 0 6752 6752 3788 S 1.8 0.6 0:20 httpd
1167 apache 9 0 6848 6848 3788 S 0.9 0.6 0:19 httpd
1 root 8 0 520 520 452 S 0.0 0.0 0:04 init
2 root 9 0 0 0 0 SW 0.0 0.0 0:00 keventd

. , Apache,
, 0% . . -
9.07 (3.29 5 , 1.79 15 ).
, . -

PHP Inside #15


2, , -
. 9.07 -
(10), ab.
, ,
798 160 , .
, , , -
. Apache (httpd).
httpd 7280 21.2% , 0.7%
. STAT : R , S ,
W , .
, -
.
9.0, ,
9.0/2 = 4.5 .
HTTP , 1.283 ,
1.283/4.5 = 0.285 .
, 2 -
( 10 ). 0.281 ,
0.285!
# ab -n100 -c2 http://192.168.0.99/php/testmysql.php

[ some lines omitted for brevity ]

Requests per second: 7.10


Transfer rate: 187.37 kb/s received

Connnection Times (ms)


min avg max
Connect: 0 2 40
Processing: 255 279 292
Total: 255 281 332

, , ,
1.283 2.566 . -
, , 2.570 .

32

HOWTO PHP

40
, 40 ,
35% . -
, MySql .
Apache.
PHP 2 , , 40 -
80 , MySql
(max_connections 100). , Apache, -
, .
20 ,
, .


, -
5.340 . , -
MySql max_connections , .

PHP Inside #15



,
. -
, , TCP/IP,
, , Apache, -
PHP .
, PHP -
, .
, ,
( ), -
MySql.

,

-
.
-
,
-
-
-

. -
-

0.5
Internet (0.25
),
.
5 ,
34 . 34/5 = 6.8
.

33

HOWTO PHP

, -
50.000 ( -
pair.com, ), 340 000
.


, ,
PHP , , PHP ,
, , PHP.
PHP . -
,
, HTML
HTTP . , PHP 0.1
, Internet 0.2 , 33% 0.3
PHP . , -

PHP Inside #15


20%, , 0.28
, . -
20% , -
.
,
. , -
1% . -
, .


, ,
while for, - -
. , :
1
, :
for ($j = 0;$j < sizeof($arr);$j++)
echo $arr[$j] . '<br>';

, :

for ($j=0, $max = sizeof($arr), $s = '';$j < $max;$j++)


$s .= $arr[$j] . '<br>';

echo $s;

, $j < sizeof($arr)
, .
, $max. , -
.
, PHP 4 -
echo , -
. , echo , -
TCP/IP HTTP .
, $s -
, .

34

HOWTO PHP

-
.
.
-
. , echo, -
15%.
ob_start();
for ($j=0, $max = sizeof($arr), $s = '';$j < $max;$j++)
echo $arr[$j] . '<br>';

, , ob_start()
. -
, , ,
HTTP .
ob_end_flush(). -
, ob_start() .

PHP Inside #15


: , -
.
2
PEAR DB recordset',
, -
echo. , -
10.2 ( -
SQL ):
function FormatRow(&$recordSet)
{
$arr = $recordSet->fetchRow();
return '<storng>' . $arr[0] . '</storng><em>' . $arr[1] . '</em>';
}

for ($j = 0;$j < $rs->numRows();$j++) {


print FormatRow($rs);
}

, .
( 8.7 ):
function FormatRow(&$recordSet)
{
$arr = $recordSet->fetchRow();
return '<strong>' . $arr[0] . '</strong><em>' . $arr[1] . '</em>';
}

ob_start();

for ($j = 0, $max = $rs->numRows();$j < $max;$j++) {


print FormatRow($rs);
}

, $max
0.5 , ob_start() - 1.0 . 1.5
.

35

HOWTO PHP

, .
, 8.5 :
function FormatRow($arr)
{
return '<strong>' . $arr[0] . '</strong><em>' . $arr[1] . '</em>';
}

ob_start();

while ($arr = $rs->fetchRow()) {


print FormatRow($arr);
}

, .
( -
), 0.1 ( 8.4 ):
ob_start();

PHP Inside #15


while ($arr = $rs->fetchRow()) {
print '<strong>' . $arr[0] . '</strong><em>' . $arr[1] . '</em>';
}

PEAR Cache, 3.5


:
require_once("Cache/Output.php");

ob_start();

$cache = new Cache_Output("file", array("cache_dir" => "cache/"));

$t = getmicrotime();

if ($contents = $cache->start(md5("this is a unique kexy!"))) {


print "<p>Cache Hit</p>";
print $contents;
} else {
print "<p>Cache Miss</p>";

##
## Code to connect and query database omitted
##

while ($arr = $rs->fetchRow()) {


print '<strong>' . $arr[0] . '</strong><em>' . $arr[1] . '</em>';
}

print $cache->end(100);
}

print (getmicrotime()-$t);

:

()
9,9 ,
SQL .

36

HOWTO PHP

9,2 ob_start()
8,7 $max ob_start()
8,5 for while, FormatRow()
ob_start()
8,4 FormatRow() ob_start()
3,5 PEAR Cache ob_start()

,
,
ob_start(), -
HTML.

-
2001

PHP Inside #15


PHP 4.0.4 pl1. , .
:
.
, -
, -
2 .
.

: PHP , -
.


, ( )
, . ,
.
( ):
,
.
, -
.
(, $this->prop++) -
.
9-10 ,
.

( , -
). PHP, , .
,
10 , -
.
,
.

37

HOWTO PHP

,
, 7-8 $localvar++. -
15 $localvar++.
11 2004: 3 .
4.3.3. 20
$localvar++, 30 $localvar++. , $localvar++
.


,
(Apache, PHP, IIS, ), , -
, , -
.
PHP .
, , , .

PHP Inside #15


PHP configure enable-inline-optimization
.
,
WHERE SQL . ADODB -
SQL ,
SQL , ,
.
HTML, .
, -
. , -
10 .
(
), -
, . -
, -
.
.
10-200% .
,
, .
ob_start() . -
5-15%. gzip -
( ).
Zend Optimizer'. -
,
, Zend Optimizer. , Zend
Optimizer , .
. ,
.
, .
, .

38

HOWTO PHP

-
(ob_start()) echo .
ob_get_contents. , -
40 , 10 .
.
, , -
.
, , -
PHP ,
RAM . configure with-
mm, PHP, session.save handler = mm php.ini.
, strpos(), preg_match()
ereg(). str_replace() preg_replace(), ,
, , ereg_replace().
switch .

PHP Inside #15


,
.
XML -
, SAX DOM.
unset() , .
.
,
().
.
PHP Java
COM , . -
COM Java.


. .
. ,
PHP .
:
Echo print. , echo , -
, print . PHP 4.3 -
, . print
echo ( ob_start ).
. -
, . ,
PHP 3, .
'var=' . $var "var=$var". PHP 4.2 -
. PHP 4.3 . 22 2004: ,
PHP 4.3 , . -
, , .

39

HOWTO PHP

?
,
. , :
function TestRef(&$a)
{
$b = $a;
$c = $a;
}
$one = 1;
ProcessArrayRef($one);

:
function TestNoRef($a)
{
$b = $a;
$c = $a;
}

PHP Inside #15


$one = 1;
ProcessArrayNoRef($one);

PHP , ,
, .
TestRef(), $b $c , ,
TestNoRef() $b $c $a
PHP . TestNoRef() -
, TestRef().
, , -
, , . ,
. -
, -
. :
function ObjRef(&$o)
{
$a =$o->name;
}
, :

$function ObjRef($o)
{
$a = $o->name;
}

: PHP 5 , -
&. PHP 5 .

40

PHP


PHP
:

, -
,
, - -.
PHP -

PHP Inside #15


?
- ,
, -
. , ( -
):
<?php
echo hello";
?>

PHP - :
Parse error: parse error, unexpected '"', expecting ',' or ';' in
d:\programms\apache\Apache\htdocs\pro.php on line 2

"" -
: d:\programms\apache\Apache\htdocs\. ,
, -
.
, , SQL- -
. -
( ), , -
. , -
, ,
.
, .
. -

. -
-
.
. , -
,
. , -
.

41

PHP

PHP :
php.ini error_log().

php.ini
php.ini , -
. , error_reporting, -
, PHP -
-.
:
http://ru.php.net/manual/en/ref.errorfunc.php#errorfunc.constants.
. -
error_reporting E_ALL PHP 4 E_ALL &
E_STRICT PHP 5 ( E_STRICT PHP 5,
E_ALL).
, , -

PHP Inside #15


. display_errors,
1, 0, . : -
1 ( , -
), 0, -
.

, .
. PHP -
- :
- .
php.ini: log_errors error_log.
log_errors
, PHP
-. , on off. -
, on, , off .
error_log
log_errors (.. on), -
error_log PHP -. -
, - .
, Linux :
error_log = /home/logs/error_log

Windows :
error_log = "D:\Temp\phplog.txt"

, - :
[01-Jan-2006 03:48:46] PHP Warning: mysql_num_rows(): supplied argument is not
a valid MySQL result resource in
d:\programms\apache\Apache\htdocs\sud\sud\classes\class.mysql.php on line 29
[01-Jan-2006 03:48:48] PHP Warning: mysql_num_rows(): supplied argument is not
a valid MySQL result resource in
d:\programms\apache\Apache\htdocs\sud\sud\classes\class.mysql.php on line 29
[01-Jan-2006 15:50:05] PHP Parse error: parse error, unexpected '"', expecting
',' or ';' in d:\programms\apache\Apache\htdocs\pro.php on line 2

42

PHP

error_log()
php.ini , PHP-
, . , -
, ini_set(),
.
, - ,
E_WARNING E_ERROR, , --
. , - , ,
- , SQL-
SELECT . , PHP- -
, ,
.

PHP Inside #15


:
error_log(" ", 3, "D:/Temp/app_log.txt");

,
(3 - -) -
-.
. , error_log()
error_log php.ini.
, error_log()
, 3. -
, -
.
, - PHP-, -
( error_log php.ini),
( error_log()), -
3, 0.
, .
error_log(" ",0);

error_log() , .
-
PHP- PHP- . ,
(Ilia Alshanetsky) (
):
<?php
function sql_failure_handler($query, $error) {

//
// SQL-
// , MySQL
$msg = htmlspecialchars( : {$query}<br> :
{$error});

//
error_log($msg,3,/home/site/logs/sql_error_log);

43

PHP

// debug
// ,
// , ,
if (defined('debug')) {
return $msg;
}
//
//
return ;
}

// ,
//,
mysql_query($query) or die(sql_query_handler($query,mysql_error()));
?>

, .

PHP Inside #15


, php.ini -
error_log()
. ,

PHP-.
,
, PHP
. ,
php.ini -
.

44

PHP ,

PHP ,
:

PHP -
.

? !
( Zannussi)

PHP5. . ,
. -

PHP Inside #15


. , -

PHP5 ,
.
PHP4 PHP5 -.
, .
SAPI .


, .
:
PHP4 PHP5;
-;
.
Windows + Apache/2.0.
,
Apache. SAPI,
, , .
, PHP
Apache , , SAPI .

SAPI ISAPI. SAPI (Server API) -


API (Apache, iPlanet ..). ISAPI (Internet Server API) -
Microsoft API - IIS.

PHP - c SAPI
(php4apache.dll PHP4 Apache/1.3 php5apache2.dll PHP5 Apache/2.0). -
SAPI - .
# PHP4
LoadModule php4_module F:/php4/sapi/php4apache2.dll
# 1.3
AddModule mod_php4.c
AddType application/x-httpd-php .php

45

PHP ,

# PHP5
LoadModule php5_module F:/php5/php5apache2.dll
# 1.3
AddModule mod_php5.c
AddType application/x-httpd-php .php

php4apache2.dll php5apache2.dll
(application/x-httpd-php) -
SAPI . -
.

, - ( )
.
PHP application/x-httpd-ze2.
.
,

PHP Inside #15


!

SAPI -. -
.

SAPI -, , -
, -
, http://www.php.net/.

, -
. , SAPI -.
# SAPI
# Apache/2.0
LoadModule php4_module F:/php4/sapi/php4apache2.dll
# Apache/1.3
LoadModule php4_module F:/php4/sapi/php4apache.dll

# SAPI *.php
AddType application/x-httpd-php .php

, - -
. , , .
php5apache2.dll (php5apache.dll -
Apache/1.3) application/x-
httpd-php application/x-httpd-ze2. php5apache2.dll
(PHP/5.1b1) , 0x000048EC 0x0000490C.
PHP/5.0.4 0x000048E8 0x00004908 -
.
SAPI Apache, -
application/x-httpd-ze2.
# Apache/2.0
LoadModule php5_module F:/php5/sapi/php5apache2.dll
# Apache/1.3
LoadModule php5_module F:/php5/sapi/php5apache.dll
AddType application/x-httpd-ze2 .php5

- *.php -
, *.php5 .

PHP ,


.
.
- .
# ,
# ini- PHP5
PHPIniDir F:/php5

php.ini , ,
F:/php5. , -
.
, PHPIniDir Apache/1.3 (
2.0). Apache/1.3,
. php5ts.dll
php.ini php-%s.ini - -

PHP Inside #15


PHP4. ze2.ini ze2-%s.ini .

php-%s.ini - , sprintf()
. %s -
SAPI. , php-cgi.ini php-apache.ini. -

.
php-%s.ini , ,
, %s.

-
, ze2.ini, php.ini.

-
PHP5 *.php5. -
F:/htdocs/php5app , -
.
<Directory F:/htdocs/php5app>
<Files "*.php">
ForceType application/x-httpd-ze2
</Files>
</Directory>

*.php
PHP5, PHP4.

Apache/2.0 ForceType core ( -),


1.3 mod_mime. ,
Apache/1.3, .

PHP ,


-
. WinNT Apache, -
. ,
, ,
.
*nix -,
. , -
, Zend/PHPTeam.
, (Apache/2.0.52 + PHP/4.3.11 +
PHP/5.1b1) , .
PHP/5.1b1.

PHP Inside #15


, . -
. , , ,
. , , .
- , , PHP5 PHP4 ( ? :)
. , , -
. -. -
, .

Вам также может понравиться