1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài giảng Phát triển ứng dụng Web (GV Nguyễn Hữu Thể)

253 9 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 253
Dung lượng 4,52 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Giới thiệu về PHP− PHP : Rasmus Lerdorf in 1994 được phát triển để phát sinh các form đăng nhập sử dụng giao thức HTTP của Unix − PHP 2 1995 : Chuyển sang ngôn ngữ script xử lý trên ser

Trang 1

PHÁT TRIỂN ỨNG DỤNG WEB

Bài 1:

PHP Cơ bản

Nguyễn Hữu Thể

Trang 2

Nội dung

▪ Giới thiệu PHP

▪ Cơ chế hoạt động của WebServer

▪ Cú pháp & Quy ước trong PHP

Trang 3

Giới thiệu về PHP

− PHP : Rasmus Lerdorf in 1994 (được phát triển để phát sinh các form

đăng nhập sử dụng giao thức HTTP của Unix)

− PHP 2 (1995) : Chuyển sang ngôn ngữ script xử lý trên server Hỗ trợ

CSDL, Upload File, biến, mảng, hàm đệ quy, câu điều kiện, …

− PHP 3 (1998) : Hỗ trợ ODBC, đa hệ điều hành , giao thức email

(SNMP, IMAP)

− PHP 4 (2000) : Parser đổi tên thành Zend Engine Bổ sung các tính

năng bảo mật cho PHP

− PHP 5 (2005) : Bổ sung Zend Engine II hỗ trợ lập trình HĐT , XML ,

SOAP cho Web Services, SQLite

− PHP 7 (2015): Cải thiện hiệu năng, tính năng ngôn ngữ mới

Trang 4

Giới thiệu về PHP – Ưu điểm 1

programming language we know (https://w3techs.com/technologies/details/pl-php/all/all)

Popular sites using PHP

Trang 5

Giới thiệu về PHP – Ưu điểm 2

Trang 6

Cơ chế hoạt động của WebServer

6 7

Trang 8

− PHP là viết tắt của PHP Hypertext Preprocessor

Solid, PostgreSQL, SQL Server,…)

Trang 9

PHP

Trang 10

PHP + MySQL

trong Windows, Unix

Trang 12

PHP Syntax

− Một khối PHP scripting bắt đầu với <?php và kết thúc bằng ?>

PHP

Trang 13

Comments in PHP

− Sử dụng / / để tạo ra một comment hoặc /**/ để tạo ra một khối comment

Trang 14

PHP Variables

− Bắt đầu với ký hiệu $

Trang 15

PHP is a Loosely Typed Language

− PHP tự động chuyển kiểu dữ liệu biến , tùy thuộc vào giá trị của nó

Trang 16

Naming Rules for Variables

− Tên biến: bắt đầu bằng một chữ cái hoặc dấu gạch dưới "_"

− Tên biến chỉ có thể chứa các ký tự chữ-số và gạch dưới (a-z,

A-Z, 0-9, and _ )

Trang 17

PHP String Variables

Trang 18

echo and print

echo or echo().

echo "Hello world!<br>";

− The print statement can be used with or without parentheses:

print or print()

print "I'm about to learn PHP!";

Trang 19

Data Types

− Variables can store data of different types, and different data

types can do different things

Trang 20

The Concatenation Operator

− Sử dụng dấu (.) để nối hai chuỗi với nhau

Trang 21

The strlen() function

Trang 22

The strpos() function

▪ Nếu kết quả được tìm thấy, hàm trả về vị trí của ký tự đầu tiên

▪ Nếu không tìm thấy, nó sẽ trả về FALSE.

− Ví dụ: tìm chuỗi "world" trong chuỗi "Hello world!":

Trang 23

PHP Operators

− Toán tử được sử dụng để tính toán giá trị biểu thức

Trang 24

PHP Operators

Trang 25

PHP Operators

Trang 26

PHP Operators

Trang 27

Conditional Statements

khác nhau dựa trên các điều kiện khác nhau

Trang 28

The if Statement

Trang 29

The if else Statement

lệnh khác nếu điều kiện sai

Trang 30

The if elseif else Statement

Trang 31

The PHP Switch Statement

nhau

Trang 32

The PHP Switch Statement

Trang 34

The while loop

Trang 35

The while loop

Trang 36

The do while Statement

− Thực hiện khối mã một lần, sau đó nó sẽ kiểm tra điều kiện, và lặp lại vòng lặp trong khi điều kiện là đúng

Trang 37

The do while Statement

Trang 38

For Loops

đến khi một điều kiện quy định là đúng

để FALSE, vòng lặp kết thúc.

Trang 39

For Loops

Trang 40

foreach

Trang 41

The foreach loop

Trang 42

PHP Functions

Trang 43

PHP Functions

Trang 44

PHP Functions - Adding parameters

Trang 45

PHP Functions - Adding parameters

Trang 46

PHP Functions - Return values

Để cho phép hàm trả về giá trị, sử dụng lệnh return.

Trang 47

PHP Arrays

arrays

Trang 48

Indexed Arrays

▪ Chỉ mục được gán tự động (chỉ số bắt đầu từ 0)

▪ Chỉ mục được gán bằng tay

Trang 49

$cars = array ( "Volvo" , "BMW" , "Toyota" );

echo "I like " $cars[ 0 ] ", " $cars[ 1 ] " and " $cars[ 2 ] "." ;

Trang 50

Indexed Arrays - Get The Length of an Array

Trang 51

Indexed Arrays - Print all the values of an indexed array

Trang 52

Associative Arrays

Trang 53

Associative Arrays - EX

<?php

$age = array ( "Peter" => "35" , "Ben" => "37" , "Joe" => "43" );

echo "Peter is " $age[ 'Peter' ] " years old." ;

?>

Peter is 35 years old.

Trang 54

Associative Arrays - Print all the values

<?php

$age = array ( "Peter" => "35" , "Ben" => "37" , "Joe" => "43" );

foreach ($age as $x => $x_value) {

echo "Key=" $x ", Value=" $x_value;

echo "<br>" ;

}

?>

Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43

Trang 56

Multidimensional Arrays (Two-dimensional Arrays)

three-dimensional array is an array of arrays of arrays).

Trang 57

two-Multidimensional Arrays (Two-dimensional Arrays)

echo $cars[ 0 ][ 0 ] ": In stock: ".$cars[ 0 ][ 1 ] ", sold: ".$cars[ 0 ][ 2 ] ".<br>";

echo $cars[ 1 ][ 0 ] ": In stock: ".$cars[ 1 ][ 1 ] ", sold: ".$cars[ 1 ][ 2 ] ".<br>";

echo $cars[ 2 ][ 0 ] ": In stock: ".$cars[ 2 ][ 1 ] ", sold: ".$cars[ 2 ][ 2 ] ".<br>";

echo $cars[ 3 ][ 0 ] ": In stock: ".$cars[ 3 ][ 1 ] ", sold: ".$cars[ 3 ][ 2 ] ".<br>";

?>

Volvo: In stock: 22, sold: 18.

Trang 58

Multidimensional Arrays (Two-dimensional Arrays)

<?php

//displaying array elements at once using for loop.

echo "<b>Array displayed using nested for loop:</b><br>" ;

for ( $r = 0 ; $r < count ( $cars ); $r ++ ){

for ( $c = 0 ; $c < count ( $ cars [ $r ]); $c ++ ){

echo $ cars [ $r ][ $c ] " " ; }

echo "<br>" ;

}

Trang 59

Multidimensional Arrays (Two-dimensional Arrays)

<?php

for ($row = 0 ; $row < count ( $cars ) ; $row++) {

echo "<p><b>Row number $row</b></p>";

Trang 60

Multidimensional Arrays

Example: Hiển thị một giá trị từ

mảng ở trên:

Trang 61

▪ sort() - sort arrays in ascending order

▪ rsort() - sort arrays in descending order

▪ asort() - sort associative arrays in ascending order, according to the

value

▪ ksort() - sort associative arrays in ascending order, according to the key

▪ arsort() - sort associative arrays in descending order, according to the value

Trang 62

Sorting Arrays - Ascending Order

Trang 63

Sorting Arrays - Descending Order

Trang 64

Sorts an associative array in ascending order: value

<?php

$age = array ( "Peter" => "35" , "Ben" => "37" , "Joe" => "43" );

asort($age);

foreach ($age as $x => $x_value) {

echo "Key=" $x ", Value=" $x_value;

echo "<br>" ;

}

?>

Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43

Trang 65

Sorts an associative array in ascending order: key

<?php

$age = array ( "Peter" => "35" , "Ben" => "37" , "Joe" => "43" );

ksort($age);

foreach ($age as $x => $x_value) {

echo "Key=" $x ", Value=" $x_value;

echo "<br>" ;

}

?>

Key=Ben, Value=37 Key=Joe, Value=43 Key=Peter, Value=35

Trang 66

Sorts an associative array in descending order: value

<?php

$age = array ( "Peter" => "35" , "Ben" => "37" , "Joe" => "43" );

arsort($age);

foreach ($age as $x => $x_value) {

echo "Key=" $x ", Value=" $x_value;

echo "<br>" ;

}

?>

Key=Joe, Value=43 Key=Ben, Value=37 Key=Peter, Value=35

Trang 67

Sorts an associative array in descending order: key

<?php

$age = array ( "Peter" => "35" , "Ben" => "37" , "Joe" => "43" );

krsort($age);

foreach ($age as $x => $x_value) {

echo "Key=" $x ", Value=" $x_value;

echo "<br>" ;

}

?>

Key=Peter, Value=35 Key=Joe, Value=43 Key=Ben, Value=37

Trang 68

PHP Forms and User Input

script

Trang 69

PHP Forms and User Input

file "welcome.php"

− Nội dung file "welcome.php":

Trang 70

PHP $_GET Function

− Hàm $_GET: thu thập các giá trị trong form với method="get"

▪ Có thể nhìn thấy tất cả mọi người (hiển thị trong thanh địa chỉ của trình duyệt)

▪ Có giới hạn về số lượng thông tin gửi.

Trang 72

When to use method="get"?

− Khi sử dụng method="get" trong các form HTML, tất cả các tên biến và giá trị được hiển thị trong URL

▪ Không nên sử dụng khi gửi mật khẩu hoặc các thông tin nhạy cảm khác!

▪ Không nên sử dụng khi giá trị vượt quá 2000 ký tự.

dấu trang Điều này có thể hữu ích trong một số trường hợp

Trang 73

PHP $_POST Function

− Hàm $_POST: thu thập các giá trị trong form với method="post"

▪ Vô hình với người khác

▪ Không có giới hạn về số lượng thông tin gửi.

Trang 74

PHP $_POST Function

Trang 76

When to use method="post"?

▪ Vô hình cho người khác

▪ Không có giới hạn về số lượng thông tin để gửi.

thể đánh dấu trang

Trang 77

Form Validation

processing PHP forms

− Validation of form data is

important to protect your form

from hackers and spammers.

Field Validation Rules

Name Required + Must only contain letters and whitespace

E-mail Required + Must contain a valid email address (with @ and ) Comment Optional Multi-line input field (textarea)

The validation rules for the form above are as follows:

Trang 78

Form Validation

returns the filename of the currently executing script

▪ $_SERVER["PHP_SELF"] sends the submitted form data to the

page itself, instead of jumping to a different page

▪ This way, the user will get error messages on the same page as the form.

HTML entities

▪ This means that it will replace HTML characters like < and > with &lt; and &gt;

▪ This prevents attackers from exploiting the code by injecting

HTML or Javascript code (Cross-site Scripting attacks) in forms.

Trang 79

Form Validation

test_input() : check each $_POST variable

<?php

$name = $email = $gender = $comment = $website = "" ;

if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) {

$name = test_input( $_POST [ "name" ]);

$email = test_input( $_POST [ "email" ]);

$comment = test_input( $_POST [ "comment" ]);

$gender = test_input( $_POST [ "gender" ]);

Trang 80

< body >

<?php

$name = $email = $gender = $comment = $website = "" ;

if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) {

$name = test_input( $_POST ["name"]);

$email = test_input( $_POST ["email"]);

$comment = test_input( $_POST ["comment"]);

$gender = test_input( $_POST ["gender"]);

< h2 > PHP Form Validation Example </ h2 >

< form method ="post" action =" <?php echo htmlspecialchars( $_SERVER ["PHP_SELF"]);?>" >

Name: < input type ="text" name = "name" > < br >< br >

E-mail: < input type ="text" name = "email" > < br >< br >

Comment:

< textarea name ="comment" rows = "5" cols = "40" ></ textarea >

< br >< br >

Gender:

< input type ="radio" name = "gender" value = "female" > Female

< input type ="radio" name = "gender" value = "male" > Male

< input type ="radio" name = "gender" value = "other" > Other < br >< br >

< input type ="submit" name = "submit" value = "Submit" >

Trang 81

Form Validation

test_input() : check each $_POST variable

Trang 82

PHÁT TRIỂN ỨNG DỤNG WEB

Bài 2:

PHP Nâng cao

Nguyễn Hữu Thể

Trang 84

PHP Date() Function

gian

Trang 85

PHP Date() - Format the Date

▪ d - Đại diện các ngày của tháng (01-31)

▪ m - Đại diện một tháng (01-12)

▪ Y - Đại diện một năm (bốn chữ số)

− Các ký tự: "/", ".", hoặc "-" cũng có thể được chèn vào

Trang 86

PHP Date() - Adding a Timestamp

thời gian

dụng

Trang 87

PHP Date() - Adding a Timestamp

Trang 88

PHP Include File

menu (thay vì cập nhật các liên kết trên tất cả các trang

Trang 90

PHP include() Function

− file "menu.php", nên sử dụng trên tất cả các trang

Trang 91

PHP include() Function

bản sẽ tiếp tục thực hiện.

Trang 92

PHP require() Function

dừng lại.

Trang 93

PHP require() Function

thực hiện sau khi xuất hiện lỗi.

Trang 94

PHP File Handling

định mode của file được mở ra

Trang 95

PHP File Handling

Trang 96

PHP File Handling

Trang 97

Closing a File

− Hàm fclose(): đóng một file đã mở

Trang 98

Check End-of-file

Hàm feof() kiểm tra nếu kết thúc file (“end-of-file” - EOF).

Trang 99

Reading a File Line by Line

− Hàm fgets(): đọc một dòng từ một file

− Sau khi gọi hàm này, con trỏ file đã di chuyển đến dòng kế tiếp

Trang 100

Reading a File Character by Character

− Hàm fgetc(): đọc một ký tự đơn từ một file

− Sau khi gọi hàm này, con trỏ file di chuyển đến ký tự tiếp theo

Trang 101

PHP File Upload

− Upload files to the server

− First, ensure that PHP is configured to allow file uploads

− In your "php.ini" file, search for the file_uploads directive, and set it to On:

file_uploads = On

Trang 102

PHP File Upload - Create The HTML Form

they want to upload

<html>

<body>

< form action ="upload_process.php" method ="post" enctype ="multipart/form-data">

Select image to upload:

</form>

</body>

</html>

Trang 103

PHP File Upload - Create The Upload File PHP

if(isset( $_POST [ "submit" ])) {

$check = getimagesize( $_FILES [ "fileToUpload" ][ "tmp_name" ]);

Trang 104

File Upload - Check if File Already Exists

− First, we will check if the file already exists in the "uploads" folder If it does, an error message is displayed, and

Trang 105

File Upload - Limit File Size

"fileToUpload".

− Check the size of the file If the file is larger than 500KB, an error message is displayed, and $uploadOk is set to 0

// Check file size

if ( $_FILES [ "fileToUpload" ][ "size" ] > 500 000) {

echo "Sorry, your file is too large." ;

$uploadOk = 0;

}

Trang 106

File Upload - Limit File Type

and GIF files

$uploadOk to 0

// Allow certain file formats

if ($imageFileType != "jpg" && $imageFileType != "png" &&

$imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed." ;

$uploadOk = 0;

}

Trang 107

File Upload - Limit File Type

if ( isset ( $_POST [ "submit" ])) {

$check = getimagesize( $_FILES [ "fileToUpload" ][ "tmp_name" ]);

if ( $_FILES [ "fileToUpload" ][ "size" ] > 500000 ) {

echo "Sorry, your file is too large." ;

$uploadOk = 0 ;

}

if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed." ;

if (move_uploaded_file( $_FILES [ "fileToUpload" ][ "tmp_name" ], $target_file)) {

echo "The file " basename( $_FILES [ "fileToUpload" ][ "name" ]) " has been uploaded." ;

} else {

The complete "upload.php"

Trang 108

PHP Cookies

▪ Một cookie là một file nhỏ mà server nhúng trên máy tính của người dùng

▪ Mỗi lần máy tính yêu cầu một trang với một trình duyệt, nó sẽ gửi kèm theo các cookie.

▪ Hàm setcookie() được sử dụng để thiết lập một cookie.

setcookie(name, value, expire, path, domain, secure, httponly);

Trang 109

PHP Cookies – Tạo cookie

− Tạo ra một cookie có tên là "user" và gán giá trị "Alex Porter" Thiết lập cho cookie này sẽ hết hạn sau một giờ:

gửi cookie, và tự động giải mã khi nhận được

Trang 110

PHP Cookies – Ví dụ

− Thiết lập thời gian hết hạn của cookie bằng cách sử dụng giây

− Trong ví dụ trên, thời gian hết hạn của cookie là một tháng

(60 sec * 60 min * 24 hours * 30 days)

Trang 111

How to Retrieve a Cookie Value?

Trang 112

How to Retrieve a Cookie Value?

Ngày đăng: 30/10/2021, 05:13

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm