Академический Документы
Профессиональный Документы
Культура Документы
Curs PHP
Curs PHP
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, .
, (30-60
.), 1 2006 (
.doc .txt ) - , c-
.
-
.
2
, , 15 -
,
.
:
2006@phpconf.ru.
, -
1 , .
20
,
.
1
.
1 -
( , 3- ).
- - (3), (2)
: 2006@phpconf.ru.
: sp2006@phpconf.ru.
PHP-
http://www.phpconf.ru/
3
. dbtree
. dbtree
: [nw]
-
dbtree, -
( ADODB, phpGacl) PHP -
, -
.
nw: dbtree.
?
(): - phpDBTree
. , -
,
. - .
:
.
.
,
, ,
. , -
, ?
, .
, - -
.
,
PHPClub .
- , , -
, , ,
, ... , -
.
, , , ,
. (2.1) , -
, , .
-
, , !
nw: ?
: , , ,
, , . , :
, ;
4
. dbtree
,
Nested Sets, -
, ;
,
5
. dbtree
-
. , -
. ? , ,
.
6
phpMyAdmin
phpMyAdmin
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
8
phpMyAdmin
Arjen: ! .
phpMyAdmin?
Marc: , 2005
, , - 220 - 320
9
phpMyAdmin
, MySQL ,
- . MySQL AB.
, --
, MySQL.
10
Smarty
Smarty
Smarty , ,
-
, -
. , . -
Smarty ,
,
.
, Smarty,
, -
. , -
, ,
, -
.
Smarty
. -
.
?
, -
, , - , -
- .
, , -,
, -, -
. - (), -
- , , -
, , ,
.
?
1. () ,
, , -
-
;
2. , , -
, ;
3.
,
.
.
11
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
$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
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
,
? , -
:
name name.
, -
. name , -
, .
, -
Smarty $smarty.
, (assign) $smarty, -
, () , ,
- ,
, , , -
, - .
-
-
. , , ,
, .
, .
- , , -
, .
, .
, , -
, .
,
, -
- .
, .
5 .
14
Smarty
:
{component name='news' action='view' count=5 tpl='news_mainpage.tpl'}
//
//
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 -
, prepare -
(assign) , '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
, , ( -
), . ,
, .., ..
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/, -
.
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
. , -
, ,
.
. , ,
, , , ,
.
, 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
, ,
.
hare tortoise ( ) . -
hare , tortoise. ,
, , -
, :
, , . , , 10
, 1 .
33 , 33
.
, ,
(DNS), .
20
HOWTO PHP
, , -
HTML , , -
, . , web-,
PHP, . -
-
.
Shared Memory ( )
, -
, , .
, , -
, , , ,
.
, , 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
, , 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
,
:
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 .
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).
, , 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
.
26
HOWTO PHP
opcode?
Zend Accelerator. , Zend Engine.
. : http://zend.com.
,
,
PHP .
Turck MMCache (http://turck-mmcache.sourceforge.net/) .
eAccelerator, mmcache .
27
HOWTO PHP
,
PHP , ,
. , HTML
HTML. -
, -
HTML. HTML PHP Zend Engine -
PHP . -
. PEAR Cache, .
Smarty.
, , HTML, , . -
:
HTML , 50-80%, -
.
, .
#
#
#
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
if ($data = $cache->get($id)) {
} else {
}
?>
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
, -
.
, ,
$time = getmicrotime();
#
#
#
echo "
, -
: 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/
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
, -
"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
. , Apache,
, 0% . . -
9.07 (3.29 5 , 1.79 15 ).
, . -
, , ,
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 , .
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 . , -
, ,
while for, - -
. , :
1
, :
for ($j = 0;$j < sizeof($arr);$j++)
echo $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() .
, .
( 8.7 ):
function FormatRow(&$recordSet)
{
$arr = $recordSet->fetchRow();
return '<strong>' . $arr[0] . '</strong><em>' . $arr[1] . '</em>';
}
ob_start();
, $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();
, .
( -
), 0.1 ( 8.4 ):
ob_start();
ob_start();
$t = getmicrotime();
##
## Code to connect and query database omitted
##
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 , -
.
, ( )
, . ,
.
( ):
,
.
, -
.
(, $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 .
, , , .
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 .
:
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 , ,
, .
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 - :
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).
, , -
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- -
, ,
.
,
(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()));
?>
, .
44
PHP ,
PHP ,
:
PHP -
.
? !
( Zannussi)
PHP5. . ,
. -
, .
:
PHP4 PHP5;
-;
.
Windows + Apache/2.0.
,
Apache. SAPI,
, , .
, PHP
Apache , , SAPI .
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.
.
,
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-%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.
-
. WinNT Apache, -
. ,
, ,
.
*nix -,
. , -
, Zend/PHPTeam.
, (Apache/2.0.52 + PHP/4.3.11 +
PHP/5.1b1) , .
PHP/5.1b1.