Appendix I: Installation and Confi guration on Linux Install PHP PHP is responsible for generating dynamic web pages with data from MySQL.. If you did not install Apache to /usr/local
Trang 1Appendix I: Installation and Confi guration on Linux
Install PHP
PHP is responsible for generating dynamic web pages with data from MySQL Follow these steps to install PHP on your system:
1 Go to the PHP web site at www.php.net
2 Click on the Download link to go to the site ’ s downloads page
3 Scroll down to the Complete Source Code section, and click on the appropriate link to download the latest tar.gz package
4 Click any of the mirror sites to begin the download If you have difficulties downloading from one mirror, try a different mirror that may be closer to you
5 Open a console window, and change to the directory where you downloaded the tarball
6 Extract the tarball, and change to the directory it creates:
tar -vxzf php-6.0.0.tar.gz
cd php-6.0.0
7 Configure the source:
./configure with-apxs2=/usr/local/apache/bin/apxs with-mysql=/usr/local/
mysql \ prefix=/usr/local/php
Using the - - prefix switch tells the installer where to put the PHP server after it ’ s built We recommend placing it in /usr/local/php , but you may choose to specify a different value, depending on your needs or your system ’ s configuration The - - with - apxs2 option specifies the path to Apache ’ s apxs tool - - with - mysql provides the path to your MySQL installation If you did not install Apache to /usr/local/apache or MySQL to /usr/local/mysql , then you will need to change those values accordingly There are numerous configuration options for PHP, and we would almost need a chapter just to describe them all! For a complete list of configuration options, run /configure - - help
8 Compile the source:
make
9 Install PHP We use sudo to escalate our account ’ s privileges, because superuser (root) privileges are needed for this step
sudo make install
10 It is advised to run PHP with a php.ini file By default, the PHP installation provides two copies of the file with common configuration values: php.ini - dist and php
ini - recommended The php.ini - dist file is meant to be used for development purposes, while php.ini -recommended has additional security measures and should be used when your site goes live
Depending on your reason for using PHP, choose the php.ini file that best suits your needs
Trang 2Appendix I: Installation and Confi guration on Linux
For the purposes of this book, we are going to be using php.ini - dist Feel free to switch to the
php.ini - recommended file as your default once you are more familiar with how PHP behaves
sudo cp php.ini-dist /usr/local/php/lib/php.ini
11 Add PHP ’ s bin directory to your PATH environment variable This will allow you to reference
PHP and its utilities from the command line, if you ever want to, without having to explicitly
type the full path each time To temporarily set it, you can type:
PATH=$PATH:/usr/local/php/bin
You will need to edit the appropriate configuration file for your profile to make the setting
permanent Exactly how you do this is dependent upon which shell you use Bash traditionally
uses bashrc , and C Shell/tcsh uses cshrc
Configuring Apache to Use PHP
Now that both Apache and PHP are installed, there are a few more customizable options that need to be
adjusted To configure Apache to recognize a PHP file as one that needs to be parsed with the PHP
engine, you need to first locate the following lines in your httpd.conf file:
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types
#
#AddType application/x-gzip tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly Note: Not all browsers support this
#
#AddEncoding x-compress Z
#AddEncoding x-gzip gz tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
Next, you need to add the PHP module into your httpd.conf program so that Apache can properly
coordinate with PHP to serve the dynamically generated pages PHP will produce In your configuration
file, locate the following lines:
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule’ lines at this location so the
Trang 3Appendix I: Installation and Confi guration on Linux
# directives contained in it are actually available _before_ they are used
# Statically compiled modules (those listed by `httpd -l’) do not need
#LoadModule auth_digest_module modules/mod_auth_digest.so
#LoadModule usertrack_module modules/mod_usertrack.so
#LoadModule version_module modules/mod_version.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
Add the following line:
LoadModule php6_module “libphp6.so”
Oh, and remember to restart Apache after you ’ ve saved your modifications to httpd.conf , or else Apache will not be aware of your changes! You can do this by calling apachectl restart
Summar y
You ’ ve installed and configured the Apache, MySQL, and PHP trio on your Linux system Now you can proceed to Chapter 2 to get your hands dirty creating your movie review web site, as we begin
discussing PHP code
Trang 5!= (not equal to), 50, 92
‹› (not equal to), 50
!== (not equal to, OR the data types not
same), 50 (operator), 687
‘ (single quotation marks), 26
?: (ternary operator), 144–145, 688 See also
if/else statements
% (wildcard), 92
_ (wildcard), 92
‹ = (less than or equal to), 50, 92
| | (logical operator alternate), 73[ ] (regex), 237
[^ ] (regex), 237
- - $value, 73
A
abandonment, shopping-cart, 512–513abs(), 124
access logs, 627account functionality (BBS), 593–606frm_admin.php, 597–600, 610frm_forgotpass.php, 595, 596frm_index.php, 59, 594frm_login.php, 593, 596frm_useraccount.php, 600–602, 604initial login, 593–596
User Administration page, 605–606user management, 597–605acronym, recursive, 10ADD link, 156, 160Add submit button, 141admin_area.php, 397–398, 404administration GUIs, 767administration interface, 156administration page
CMS, 443–447mailing list, 472–486movie review database, 153–156administration transaction pages (BBS), 580–583
admin.php, 154–155advertising mailing lists, 470ait.com, 768
E_ALL, 249alt attributes, 119ALTER, 84ALTER TABLE, 121
AMP See Apache, MySQL, and PHP
ampersand (&), 34Analog, 634AND (logical operator), 73
Index
Trang 6And, Boolean (&&), 73, 143
announcement mailing lists, 470
BBS See bulletin board system
BCC (blind carbon copy), 353, 485, 504, 674BCNF (Boyce-Codd Normal Form), 269BDB (BerkeleyDB), 84
BerkeleyDB (BDB), 84BIGINT, 80, 753BINARY, 755BIT, 753blind carbon copy (BCC), 353, 485, 504, 674BLOB, 80, 178, 755
BMP, 176, 186, 187IMAGETYPE_BMP, 186
board See bulletin board system
Board Administration page, 610–612BOOL, 753
BOOLEAN, 753Boolean And operator (&&), 73, 143Boyce-Codd Normal Form (BCNF), 269breadcrumbs, 577–579
brightness, image, 201bulletin board system (BBS), 557–625account functionality, 593–606frm_admin.php, 597–600frm_forgotpass.php, 595, 596frm_index.php, 59, 594frm_login.php, 593, 596frm_useraccount.php, 600–602, 604initial login, 593–596
User Administration page, 605–606user management, 597–605additions
avatars, 624polls, 625quoting text, 625smilies, 624user profiles, 625BBcodes, 558, 609breadcrumbs, 577–579database, preparation, 558–564features, 558
as forum, 557frm_admin.php, 597–600, 610BBcode Administration, 612–614Board Administration, 610–612Forum Administration, 612User Administration, 605–606functionality, 606–624
And, Boolean (&&)
Trang 7pagination, 558, 573–577regex in, 558, 612–614reusable scripts, 564–573searching in, 558, 623–624settings, editing of, 606–610tables, 562–564
frm_access_levels, 562frm_admin, 562frm_bbcode, 563frm_forum, 563frm_post_count, 563frm_posts, 563–564frm_users, 564transaction pages, 580–593administration, 580–583frm_transact_admin.php, 580–583frm_transact_affirm.php, 591–593frm_transact_post.php, 584–586frm_transact_user.php, 586–590post, 584–586
removal/deletion, 591–593user–related, 586–590user authentication, 558, 579–580using (exercise), 614–622
superhero powers page, 289–293transaction page, 277–289char_transaction.php, 277–281
check boxes, 45, 135, 151
check out process, 530–550
ecomm_checkout2.php, 508, 533–537, 546–547ecomm_checkout3.php, 508, 537–543, 547–550
ecomm_checkout.php, 507, 530–532, 545–546steps in, 530, 545
testing, 543–545checkdate(), 238, 239check_effect.php, 200check_image.php, 180, 183, 200, 201captions and, 202–207
filters and, 193–197child page, 119classes, 74–75, 691–692encapsulation and, 344, 348, 349SimpleMail, 343–352, 474–476, 497class.SimpleMail.php, 344–346, 348, 474–476, 483–484
closed source model, 4closing/opening tags (< ?php ? >), 20, 685
CMS See content management system
cms_access_levels table, 410–411, 441, 445cms_admin.php, 443–444
cms_articles table, 411cms_comment.php, 459, 460cms_compose.php, 430, 447–450cms_cpanel.php, 461–463cms_footer.inc.php, 415, 418cms_forgot_password.php, 433, 436cms_header.inc.php, 414–415, 418, 436cms_http_functions.inc.php, 415, 419–420, 423cms_index.php, 423, 424, 433–434, 436cms_login.php, 432–433, 436
cms_output_functions.inc.php, 412–414,
418, 420cms_pending.php, 451–452, 456cms_review_article.php, 452–453, 456, 457cms_search.php, 466, 467
cms_transact_article.php, 426–429cms_transact_user.php, 420–423, 441, 442cms_user_account.php, 437–439
cms_users table, 411cms_view_article.php, 459, 460
code/coding practices, 21–23 See also
reusable codecomments in, 22consistency in, 22
efficiency See efficient code
line numbers in, 22colorizing images, 201comic book fan web site, 3bulletin board system, 557–625account functionality, 593–606additions, 624–625
comic book fan web site
Trang 8comic book fan web site (continued)
newsletters, 469opt–in, 503opt–out, 503removing subscriptions, 500–502signup form, 486–499
spam, 469, 502tables, 471–472types of, 469–470web site notifications, 469postcard application
confirmation and, 330–343images and, 328–329JavaScript and, 341–342
registration system See registration system
tablescreation of, 272–276normalization and, 265–269
user logins See logins
comic_character_power, 286, 287comic_rivalrly, 285, 286
commands (MySQL), 84comments, 686coding practice, 22debugging and, 23future changes and, 23PHP, 21
commit.php, 159–162checking dates/numbers, 230–233editing, 170–174
erroneous user input, 222–225switch, 162, 167
common links, 554comparison operators, 92
comic book fan web site (continued)
Trang 9cms_access_levels table, 410–411, 441, 445cms_admin.php, 443–444
cms_articles table, 411cms_comment.php, 459, 460cms_compose.php, 430, 447–450cms_cpanel.php, 461–463cms_footer.inc.php, 415, 418cms_forgot_password.php, 433, 436cms_header.inc.php, 414–415, 418, 436cms_http_functions.inc.php, 415, 419–420, 423
cms_index.php, 423, 424, 433–434, 436cms_login.php, 432–433, 436
cms_output_functions.inc.php, 412–414,
418, 420cms_pending.php, 451–452, 456cms_review_article.php, 452–453, 456, 457cms_search.php, 466, 467
cms_transact_article.php, 426–429cms_transact_user.php, 420–423, 441, 442cms_user_account.php, 437–439
cms_users table, 411cms_view_article.php, 459, 460reusable scripts, 412–420search feature, 466–467transaction pages, 420–432user control panel, 461–465user interface, 432–467contrast, of image, 201
converting image types, 188–192
cookies, 41–44, 355, 405
defined, 41, 359passing variables with, 41–44security risks, 41
sessions v., 359, 388
setting, 42–44tracking, with PHP, 388–393cookies_delete.php, 389cookies_set.php, 388–389cookies_test.php, 390cookies_view.php, 389copyrighted images, 202, 209E_CORE_ERROR, 249
E_CORE_WARNING, 249CREATE, 84
CREATE DATABASE yourdatabase, 271
CREATE TABLE, 275createimagefrom* (), 192cron jobs, 767
ctype_* functions, 235, 236, 239ctype_alnum(), 236
ctype_alpha(), 236ctype_cntrl(), 236ctype_digit(), 236ctype_graph(), 236ctype_lower(), 236ctype_print(), 236ctype_punct(), 236ctype_space(), 236ctype_upper(), 236ctype_xdigit(), 236custom error messages, 243–246
custom logs, 627custom_error.php, 252–255customers (e-commerce)characteristics, 551communication with, 555feedback, 555
personal customer service, 553privacy policy, 552
return policy, 552–553secure credit card processing, 553trust, 552–553
Trang 10comments and, 23echo and, 645–646tips, 645–647Xdebug and, 647DEC, 753
DECIMAL, 79, 753decrementing values, 73
- - $value, 73
$value - -, 73define() function, 30DELETE, 84, 162–167, 288Cascade, 164–167DELETE links, 156, 165delete.php, 164–166deletion
Cascade DELETE, 164–167
of items in cart, 521–524
of records, 162–167
of transaction pages, 591–593DESCRIBE, 84
design rules (standards), 270designing database (Comic Book), 269–271directory/file functions, 709–713
discussion forum See bulletin board system
documentation, MySQL, 102DOUBLE, 754
DOUBLE PRECISION, 754double quotation marks (“), 73do/while, 68, 690
Dreamweaver CS3, 762, 763DROP, 84
drop–down list boxes, 45, 135
E
echo, 23–26, 27, 28, 29, 685debugging and, 645–646
print v., 72, 685
ecomm_checkout2.php, 508, 533–537, 546–547ecomm_checkout3.php, 508, 537–543, 547–550ecomm_checkout.php, 507, 530–532, 545–546ecomm_customers table, 510
databases (continued)
Trang 11e-commerce, 311, 505–556
common links, 554competitive pricing, 554customers
characteristics, 551communication with, 555feedback, 555
personal customer service, 553privacy policy, 552
return policy, 552–553secure credit card processing, 553trust, 552–553
guidelines, 551–555home page, 515–517information, textual/graphical, 552navigation, 554
productsappropriate, 554ideas for, 506–507timely delivery, 555viewing information on, 517–521professional look, 553–554search feature, 554shopping cartabandonment, 512–513Cart32, 507
check out process, 530–550coding, 507
database, 508–510db_ch15 - 1.php, 507db_ch15 - 2.php, 507ecomm_checkout2.php, 508, 533–537, 546–547
ecomm_checkout3.php, 508, 537–543, 547–550
ecomm_checkout.php, 507, 530–532, 545–546
ecomm_shop.php, 507, 516–517, 519ecomm_update_cart.php, 507
ecomm_view_cart.php, 507ecomm_view_product.php, 507products, 521–524, 524–529tables, 508–514
temporary, 512–513third–party software, 507ecomm_order_details table, 512
commit.php, 170–174databases, 153–174movie, 167–174EditPlus, 762, 763edit_power.php, 289–293E_ERROR, 249
efficient code, 22functions and, 55include statement, 52–54
else statements, 51–52, 688 See also if/else
statementse-mail (PHP), 315–353error, creation of, 247–248mail(), 317, 321
errors and, 248mail functions, 724–725
mailing lists See mailing lists
PHP setup for, 316postcard applicationconfirmation implementation, 330–343images and, 328–329
sending, 316–322data collection and, 317–322headers and, 323–325HTML and, 322–325multipart messages, 326–328simple, 317
SimpleMail class, 343–352, 474–476, 497empty(), 239
empty variables, 643encapsulation, 344, 348, 349enctype attribute, 183ENDHTML, 113enemies data, referential integrity and, 287–288ENUM, 79, 755
equal to (==), 50equal to, AND data types match (===), 50error constants
E_ALL, 249E_COMPILE_ERROR, 249E_COMPILE_WARNING, 249E_CORE_ERROR, 249E_CORE_WARNING, 249E_ERROR, 249
E_NOTICE, 249E_PARSE, 249
error constants
Trang 12error constants (continued)
error e-mail, creation of, 247–248
error handling, 241–260 See also
F
fatal errors, 250, 251feature_error.php, 255–256feedback for articles (CMS), 458–460feedback, logs as, 637
fetch(), 773–774fetch mode constants, PDO, 734, 773–774
fetchAll(), 773–774
field types (MySQL), 78–82, 270–271 See also specific field types
choosing, 80–82date/time, 755numeric, 753–754
reference list, 753–756
string, 755–756
error constants (continued)
Trang 13fields, 78, 151
in forms, 151not null, 82null, 82Fifth Normal Form (5NF), 269
IMG_FILTER_SELECTIVE_BLUR, 201IMG_FILTER_SMOOTH, 201Firebird, 766, 770
First Normal Form (1NF), 266, 269
1NF (First Normal Form), 266, 269
firstprog.php
echo in, 23–26PHP/HTML in, 27–28500: Internal Server Error, 243, 246
5NF (Fifth Normal Form), 269
for getting information, 45–49linking, 145–151
multiple processing, 139–142parts of, 44–45
passing variables through, 44–49, 687user input, 135–145
forum, 557 See also bulletin board system
Forum Administration page, 612FOUND_ROWS(), 575
400: Bad Request, 243401: Authorization Required, 243403: Forbidden, 243
404: Not Found, 243, 2454NF (Fourth Normal Form), 269Fourth Normal Form (4NF), 269Friedl, Jeffrey, 238
frm_access_levels, 562frm_admin table, 562frm_admin.php, 597–600, 610BBcode Administration, 612–614Board Administration, 610–612Forum Administration, 612User Administration, 605–606frm_bbcode, 563
frm_compose.php, 617–619frm_config.inc.php, 571frm_edit_forum.php, 606–610frm_footer.inc.php, 573frm_forgotpass.php, 595, 596frm_forum, 563
frm_header.inc.php, 572–573, 579–580frm_index.php, 59, 594
frm_login.php, 593, 596frm_output_functions.inc.php, 565–571functionality, 573
pagination and, 573–577show_topic(), 574frm_post_count, 563frm_posts, 563–564frm_search.php, 619–620, 623frm_transact_admin.php, 580–583frm_transact_affirm.php, 591–593frm_transact_post.php, 584–586frm_transact_user.php, 586–590frm_useraccount.php, 600–602, 604frm_users, 564
frm_view_forum.php, 614–616frm_view_topic.php, 617
frm_view_topic.php
Trang 14FROM, 91
FrontBase, 769
full–featured error handler, 255–256
functions See also specific functions
$_GET superglobal array, 123, 127, 129get_director(), 117, 124
getimageinfo(), 186getimagesize(), 186get_leadactor(), 117, 124get_movietype(), 117, 124gettor methods, 349GIF, 176, 186IMAGETYPE_GIF, 186support, enabling, 176global, 55
H
handling errors See error handling
hashing, 367header(), 143–144problem code and, 144warning message and, 143header.php, 53–54
‘headers already sent’ error, 40, 44, 143, 644–645
headers, e-mail and, 323–325Hello World, 26, 132, 135, 342heredoc, 73, 110–114
closing tag line, error and, 113syntax, 110–114
hits/page views, 638hosting, 765
third-party, 765–768administration GUIs, 767bandwidth usage, 767configuration ability, 767online companies (list), 768pricing, 768
server access, 766–767server control, 766–767site usage, 767
supported databases, 766
FROM