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

Bài giảng Phát triển ứng dụng Web: Bài 1 - Nguyễn Hữu Thể

81 17 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

Tiêu đề PHP Cơ Bản
Tác giả Nguyễn Hữu Thể
Trường học Trường Đại Học
Chuyên ngành Phát Triển Ứng Dụng Web
Thể loại Bài Giảng
Năm xuất bản 2025
Thành phố Hà Nội
Định dạng
Số trang 81
Dung lượng 1,62 MB

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

Nội dung

Bài giảng Phát triển ứng dụng Web: Bài 1 PHP Cơ bản cung cấp cho người học những kiến thức như: Giới thiệu PHP; Cơ chế hoạt động của WebServer; Cú pháp và Quy ước trong PHP. Mời các bạn cùng tham khảo!

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

Disk driver

3

4 5

6 7

Trang 8

PHP

Solid, PostgreSQL, SQL Server,…)

Trang 9

PHP

Trang 10

PHP + MySQL

trong Windows, Unix

Why PHP?

Trang 11

Download MySQL Database

Download Apache Server

Download WAMP, XAMPP

Trang 12

PHP Syntax

PHP

Trang 13

Comments in PHP

khối comment

Trang 14

PHP Variables

Trang 15

PHP is a Loosely Typed Language

Trang 16

Naming Rules for Variables

A-Z, 0-9, and _ )

($my_string), hoặc với chữ hoa ($myString)

Trang 17

PHP String Variables

Trang 18

echo and print

echo or echo().

echo "Hello world!<br>";

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

Trang 19

Data Types

types can do different things

Trang 20

The Concatenation Operator

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.

Trang 23

Lập trình ứng dụng mạng 23

PHP Operators

Arithmetic Operators

Trang 24

PHP Operators

Assignment Operators

Trang 25

PHP Operators

Comparison Operators

Trang 26

PHP Operators

Logical Operators

Trang 27

Conditional Statements

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

Trang 29

The if else Statement

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

Syntax

Example

Trang 30

The if elseif else Statement

Syntax

Example

Trang 31

The PHP Switch Statement

nhau

Syntax Syntax

Trang 32

The PHP Switch Statement

Trang 34

The while loop

− Thực thi một khối mã lệnh trong khi một điều kiện là đúng.

Trang 35

The while loop

Trang 36

The do while Statement

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

Syntax

init: thiết lập một giá trị khởi tạo

condition: đkiện lặp Nếu đk là TRUE, vòng lặp tiếp tục Nếu đk

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

increment: bước tăng tiếp theo của biến lặp

Trang 39

For Loops

Trang 40

foreach

Syntax

Trang 41

The foreach loop

Trang 43

PHP Functions

Trang 44

PHP Functions - Adding parameters

Trang 45

PHP Functions - Adding parameters

Trang 46

PHP Functions - Return values

Trang 47

PHP Arrays

− Một mảng lưu trữ nhiều giá trị trong một biến duy nhất.

− Mỗi phần tử trong mảng có chỉ số riêng để truy cập

− 3 kiểu mảng:

▪ Indexed arrays - Arrays with a numeric index

▪ Associative arrays - Arrays with named keys

▪ Multidimensional arrays - Arrays containing one or more 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 52

Associative Arrays

Example 1: Sử dụng một mảng để phân định lứa tuổi

Example 2

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 55

Multidimensional Arrays

more arrays.

three, four, five, or more levels deep

However, arrays more than three levels deep are hard to manage for most people.

Trang 56

Multidimensional Arrays (Two-dimensional Arrays)

− A two-dimensional array is an array of arrays (a

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

− Look at the following table:

Trang 57

Multidimensional Arrays (Two-dimensional Arrays)

▪ Get access to the elements of the $cars array

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.

BMW: In stock: 15, sold: 13.

Saab: In stock: 5, sold: 2.

Land Rover: In stock: 17, sold: 15.

Trang 58

Multidimensional Arrays (Two-dimensional Arrays)

▪ Loop to get the elements of the $cars array

<?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)

▪ Loop to get the elements of the $cars array

<?php

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

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

echo "<ul>" ;

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

echo "<li>" $cars[$row][$col] "</li>" ;

Trang 60

Multidimensional Arrays

− Tạo một mảng đa chiều, với các ID gán tự động

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

▪ krsort() - sort associative arrays in descending order, according to the key

Trang 68

PHP Forms and User Input

script

Example

Trang 69

PHP Forms and User Input

file "welcome.php"

Trang 70

PHP $_GET Function

▪ 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"?

biến và giá trị được hiển thị trong URL

Lưu ý:

▪ 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

▪ 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

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)

Gender Required Must select one

The validation rules for the form above are as follows:

Trang 78

Form Validation

$_SERVER["PHP_SELF"] : a super global variable that

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.

htmlspecialchars() : function converts special characters to

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

Validate Form Data With PHP

test_input() : check each $_POST variable

<?php

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

$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

Validate Form Data With PHP

test_input() : check each $_POST variable

Ngày đăng: 13/07/2021, 12:02

TỪ KHÓA LIÊN QUAN

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

w