Tải bản đầy đủ (.pdf) (20 trang)

Zend PHP Certification Study Guide- P8

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (146.27 KB, 20 trang )

124
Chapter 7 Managing Dates and Times
Also, certain time formats can befuddle
strtotime()
. For example, this code will
cause
$ts
to be set to
-1
, indicating an error:
$ts = strtotime(“Fri, 9 Jan 2004 03:26:23 +0000 GMT”);
strtotime()
has become confused because two separate time zone offsets (
+0000
and
GMT
) were both specified. If you are manually constructing all the inputs to
strtotime()
(for example, to take advantage of the
next week
/
last week
functionality) this is not
an issue, as you can test your inputs to make sure that they are all correctly handled.
However, if you are using
strtotime()
to handle arbitrarily formatted date entries (for
example, submitted free form through a web page), you should take into account the
possibility of both incomprehensible and improperly interpreted dates.
One of the best aspects of
strtotime()


is using it to handle leap years and daylight
saving time. If you need to find the time stamp for the current time tomorrow, you can-
not simply look 24 hours in the future, as it might be 23 or 25 hours if you cross a day-
light saving time boundary.
strtotime()
takes this into account, though; so to find a this
time tomorrow, you could just say
$tomorrow = strtotime(“tomorrow”)
Because you did not specify a time,
strtotime()
uses the current value.
Exam Prep Questions
1. Which of the following sentences are incorrect?
A.
date()
returns the current UNIX datestamp.
B.
date()
returns a formatted date string.
C.
date()
requires a time stamp to be passed to it.
D.
date()
returns a date array.
The correct answers are A, C, and D.
date()
takes a format string and an optional
time stamp and produces a formatted date string. If a UNIX time stamp is not
passed into

date()
, it will use the current time.
2. The ________ function will return the current UNIX time stamp.
The correct answer is
time()
.
3. Which of the following functions will output the current time as
11:26 pm
?
A. print date(‘H:m a’);
B. print date(‘G:M a’);
C. print date(‘G:i a’);
D. print strftime(‘%I:%M %p’);
The correct answers are C and D.
08 7090 ch07 7/16/04 8:44 AM Page 124
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
125
Exam Prep Questions
4. The PHP date functions are only guaranteed to work for dates after _____.
A. January 1, 1970 00:00:00
B. January 1, 1900 00:00:00
C. January 1, 1963 00:00:00
D. January 18, 2038 22:14:07
The correct answer is A.The UNIX epoch is January 1, 1970 00:00:00 UTC. On
32-bit systems, the date functions are only guaranteed to work until January 19,
2038.
5. Internally PHP handles dates and times as
A. A ‘date array’ array consisting of the year, month, day, hour, minute, and
second.
B. A UNIX time stamp representing the number of seconds since the UNIX

epoch.
C. An ISO-8601 date entity.
D. A UNIX time stamp consisting of the number of microseconds since the
UNIX epoch.
The correct answer is B. PHP stores all its dates internally as UNIX time stamps,
which are defined as the number of seconds since the UNIX epoch, January 1,
1970 00:00:00 UTC.
08 7090 ch07 7/16/04 8:44 AM Page 125
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
08 7090 ch07 7/16/04 8:44 AM Page 126
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
8
Managing Email
Introduction
Terms You’ll Need to Understand
n
sendmail wrapper
n
SMTP
n
Headers
n
MIME encoding
n
SMTP
(Windows only)
n
smtp_port
(Windows only)
n

sendmail_from
(Windows only)
n
sendmail_path
Techniques You’ll Need to Master
n
Mail functions
n
URL functions
How Email Is Delivered
If you are going to be writing and deploying PHP scripts that generate and send email
messages, you need to know something about how email gets delivered around the
Internet.This will help you better understand and support your customers when prob-
lems arise. Figure 8.1 shows the main components of the email architecture.
09 7090 ch08 7/16/04 8:45 AM Page 127
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
128
Chapter 8 Managing Email
Figure 8.1 How email is delivered.
Here are the standard terms that you will come across at some point or another.
MTA—Mail Transport Agent
When email is sent from organization to organization, it is sent from email server to
email server.The software that runs on your email server and handles sending and
receiving email from around the Internet is called the Mail Transport Agent (MTA for
short). Examples of Mail Transport Agents are
n
sendmail
n
postfix
n

qmail
n
Microsoft Exchange
n
Lotus Notes
Mail transport agents talk to each other using the SMTP network protocol.
SMTP—Simple Mail Transport Protocol
The Simple Mail Transport Protocol (SMTP) is the standard network-layer protocol for
transmitting an email message across the Internet.
Servers normally listen for incoming SMTP connections on port 25.
MX Records
When an MTA has an email message to send to another MTA, it has to convert the
address in the To:, Cc:, or Bcc: line into an IP address. Everything after the @ sign in the
address is taken to be the email domain.This is normally something such as
@php.net
.
Sends email via SMTP
May authenticate via SASL
Receives email via
IMAP or POP3
Mail Server
(runs MTA)
DNS Server
(provides MX records)
Requests MX
records via DNS
Sends email via SMTP
Personal Computer
(runs MUA)
Sends email via SMTP

May authenticate via SASL
Receives email via
IMAP or POP3
Mail Server
(runs MTA)
DNS Server
(provides MX records)
Requests MX
records via DNS
Personal Computer
(runs MUA)
Resolves hostnames via DNS
09 7090 ch08 7/16/04 8:45 AM Page 128
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
129
How Email Is Delivered
The email domain isn’t the real name of a server. It looks like a real name (and has to
follow the same rules), but it isn’t. It’s actually a special kind of DNS alias.
To receive email for your email domain, you have to add an MX record for that email
domain to your DNS server.
Note
If you don’t set up an MX record in your DNS server, the MTA will look for a matching A record instead.
MUA—Mail User Agent
The Mail User Agent (MUA) is the jargon name for an email client.
Examples of Mail User Agents are
n
Outlook Express
n
Evolution
n

KMail
n
pine
n
mutt
n
Hotmail
A PHP script that sends email is also a type of Mail User Agent.
Mail User Agents read email directly from files on disk, via network protocols such as
POP3 or IMAP, or via proprietary network protocols (as used by Microsoft Exchange).
Mail User Agents normally send their email by connecting to a Mail Transport Agent
over the network via the SMTP network protocol. Some UNIX-based Mail User Agents
might send their email instead by executing a sendmail wrapper program.
When a Mail User Agent connects to an MTA via SMTP to send email, it might use
SASL to authenticate the user.
SASL—Simple Authentication and Security Layer
The Simple Authentication and Security Layer (SASL) is a tried and trusted way to bolt
user-authentication onto existing network protocols. SMTP has been extended (via the
SMTP AUTH command) to support SASL.
If an MTA has been configured to require authentication, only MUAs with built-in
support for the SMTP AUTH command will be able to connect to send email.
Other Emerging Technologies
Although email is as old as the Internet itself, the rise of unsolicited bulk email (com-
monly called spam), the increasing number of modern viruses that transmit themselves
via email, and the fraudulent use of genuine email addresses for criminal intent mean
that we are at the start of a period of great change in how email will be delivered in the
future.
09 7090 ch08 7/16/04 8:45 AM Page 129
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
130

Chapter 8 Managing Email
Junk email filters have become very popular.They can be added both to the MTA
and/or the MUA. It’s a fair bet that most of the email that these filters mark as ‘junk’
never gets read by a human. Junk email filters aren’t perfect; genuine email that looks
very like real junk email will also get marked as junk.When you roll out a PHP applica-
tion that sends email, you should perform some tests with your customer to make sure
that your email will get past whatever junk email filter your customer uses.
Because of repeated security holes in MUAs, the more tech-savvy businesses and users
do not accept delivery of HTML email. It’s very tempting to send HTML email—such
emails look so much nicer than plain-text email.You should ensure that any PHP appli-
cation you write that sends email always gives the user the option to choose between
plain-text email and HTML email.You should never only support HTML email.
If you write and sell PHP software that works with email, it’s important that you keep
abreast of the new technologies that are always coming out.When capturing require-
ments from your customer, always make sure that you’ve agreed what email technologies
the customer has adopted—or is planning to adopt within six months of the end of your
project.The customer will (somewhat naively) expect your PHP application to work
perfectly with whatever changes he plans to make to his email infrastructure—and will
blame you if it doesn’t.
Preparing PHP
Before you can send email from your PHP script, you must first ensure that your copy of
PHP has been correctly configured.
If You Are Using PHP on UNIX
To send email from a PHP script running on UNIX, you must have a sendmail-
compatible MTA installed on the same server that your PHP script runs on.
On UNIX systems, PHP sends email by running the command-line program
sendmail
.
sendmail
is the de facto standard MTA for UNIX systems.

If you are using an alternative to
sendmail
, it must provide a sendmail wrapper.A
sendmail wrapper is a drop-in replacement for the
sendmail
command-line program. It
must accept the
-t
and
-i
command-line switches at the very least.
When PHP is compiled, the
configure
script searches for the
sendmail
command in
/usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib
If
configure
cannot find the
sendmail
command, then sendmail support will be per-
manently disabled.The following PHP functions will either be missing entirely, or will
always return an error:
n
mail()
—will be missing
n
ezmlm_hash()
—will be missing

n
mb_send_mail()
—will always return
false
09 7090 ch08 7/16/04 8:45 AM Page 130
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
131
Preparing PHP
When this happens, you must install a sendmail wrapper, and then recompile PHP.
Once PHP is compiled with sendmail support enabled, whenever your script sends
email, PHP will use the
sendmail
command discovered by the
configure
script. If you
ever need to override this default, set the
sendmail_path
in
php.ini
to point to the
sendmail
command that you want PHP to use.
sendmail_path = ‘/usr/local/bin/sendmail’
If You Are Using PHP on Windows or Netware
Although not documented in the PHP Manual, if you set the
sendmail_path
setting in
your
php.ini
file, PHP will attempt to send email via the sendmail_wrapper—behaving

exactly as if PHP were running on UNIX.This can catch you out, so remember to look
for it when troubleshooting email problems.
If you do not have a sendmail wrapper available, PHP on Windows talks to the mail
transport agent (MTA) directly via the SMTP network protocol. PHP needs to be con-
figured to tell it where to find your MTA:
n
The
SMTP
setting in
php.ini
must be set to the hostname (or IP address) of the
email server that your MTA is running on.The default value is
localhost
.You
will probably want to change that.
n
The
smtp_port
setting in
php.ini
must be set to the port that your MTA is lis-
tening on.The default value is
25
.You probably will not need to change that.
Note
It isn’t documented in the PHP Manual, but PHP on Novell Netware uses the same code for email support as
PHP on Windows.
Caution
It is currently not possible to get PHP on UNIX to talk directly to the MTA via SMTP.
PHP on Windows does not support SASL. If your MTA is configured to require authen-

tication, you will need to change the security on your MTA to enable PHP to send
emails through successfully.
On UNIX, the MTA will automatically say that the email is from whichever user
your PHP script is running as.
This can’t be done on Windows because PHP is connecting to the MTA over the
network via SMTP. Instead, PHP will work out who the email is from by looking in
these places in this order:
n
the
from:
header line passed to the
mail()
function
n
the
sendmail_from
setting in the
php.ini
file
09 7090 ch08 7/16/04 8:45 AM Page 131
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×