Chapter 19 PHP Internet & WWW

advertisement
Internet & World Wide Web
How to Program, 5/e
function getAdder($x) {

return function ($y) use ($x) {

return $x + $y;

};

}

$adder = getAdder(8);
 echo $adder(2); // prints "10"

39









function lock() {
$file = fopen('file.txt', 'r+');
retry:
if (!flock($file, LOCK_EX)) {
goto retry;
}
fwrite($file, 'Success!');
fclose($file);
return 0; }
40

<?php
namespace Foo;
class Bar {
public function Bar() {
// treated as constructor in PHP 5.3.05.3.2
// treated as regular method in PHP 5.3.3
}
}
?>
41



class Person {
public $first;
public $last;


















public function __construct($f, $l) {
$this->first = $f;
$this->last = $l;
}
public function greeting() {
return "Hello, my name is {$this->first} {$this->last}.";
}
static public function staticGreeting($first, $last) {
return "Hello, my name is {$first} {$last}.";
}
}
$him = new Person('John', 'Smith');
$her = new Person('Sally', 'Davis');
echo $him->greeting(); // prints "Hello, my name is John Smith."
echo '<br />'; echo $her->greeting(); // prints "Hello, my name is Sally Davis."
echo '<br />';
echo Person::staticGreeting('Jane', 'Doe'); // prints "Hello, my name is Jane Doe."
42




Complete Scripts (3745) Hundreds of ready-to-use PHP
programs for your web site!
Functions and Classes (300) Although not complete
scripts, these code pieces aid in development and often
with little modification can become complete programs.
Documentation (425) Information, books, tutorials,
examples and help regarding PHP programming. If you
want to learn it, here's how. Includes:
Examples and Tutorials (308)
Community (126) There is a large PHP community and
this is where you'll find resources such as jobs, chats,
message boards, developer sites and much more!Web
Hosting Need a web host for your PHP programs? Find
one here.
43

Access Counters (67) Display how many visitors you've had in a text-based
or graphical manner.
Advertisements (70) Set up banner rotations on your web page and track
their statistics.
Auctions (38) Conduct online web-based auctions using these PHP scripts.
Audio Management (39) Manage and provide audio files in different formats
for users to listen to.
Bulletin Board Message Systems (66) On-line message forums allowing for
threaded discussions.
Calendars (76) Schedule events on-line and/or allow users to post dated
information.

Chat (77) Real time chat scripts for use on the web. These often bog down a
server.
Includes: Shoutbox (19), ...
Classified Ads (210) Users can post information on buying, selling and/or
trading possessions.
………..
44



Superglobal arrays are associative arrays predefined by
PHP that hold variables acquired from user input, the
environment or the web server and are accessible in any
variable scope.
The arrays $_GET and $_POST retrieve information sent to
the server by HTTP get and post requests, respectively.
Using method = "post" appends form data to the browser
request that contains the protocol and the requested
resource’s URL. Scripts located on the web server’s
machine can access the form data sent as part of the
request.
 HTTP POST 방식으로 보내진 변수들은 URL 에 나타나지 않음
 변수들이 길이에 제한이 없음
 하지만 , 변수들이 URL 에 나타나지 않기 때문에 , 그 페이지를 즐겨찾기 하거나 북마크하는 것은
불가능함
46

$_get
 An associative array of variables passed to the
current script via the URL parameters
 Assuming the user entered http://example.com/?
name=Hannes
 <?php
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
?>
 출력은 : Hello Hannes!


$_post ?
$-request : $_GET, $_POST, $_COOKE 모두의
내용을 담음
47




Function extract creates a variable/value pair
corresponding to each key/value pair in the
associative array passed as an argument.
Business logic, or business rules, ensures that
only valid information is stored in databases.
We escape the normal meaning of a character in
a string by preceding it with the backslash
character (\).
Function die terminates script execution. The
function’s optional argument is a string, which is
printed as the script exits.
48
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
extract( $_POST );
Creates a variable/value pair for each key/value
Outline
pair in $_POST
// determine whether phone number is valid and print
// an error message if not
if ( !ereg( "^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$", $phone ) )
{
print( "<p><span class = 'error'>
Invalid phone number</span><br />
A valid phone number must be in the form
<strong>(555)555-5555</strong><br />
<span class = 'distinct'>
Click the Back button, enter a valid phone
number and resubmit.<br /><br />
Thank You.</span></p>" );
die( "</body></html>" ); // terminate script execution
}
?><!-- end PHP script -->
<p>Hi
<span class = "distinct">
<strong><?php print( "$fname" ); ?></strong>
</span>.
Thank you for completing the survey.<br />
You have been added to the
<span class = "distinct">
<strong><?php print( "$book " ); ?></strong>
</span>
mailing list.
form.php
(2 of 5)
Ensures that phone
number is in proper
format
Terminates execution
and closes the
document properly
49
Download