Web06_PHPAdvanced
Web06_PHPAdvanced
• Class declaration
• The constructor
• Scope
• Inheriting
• Function serialize, unserialize
• The constructor
• Declare
const TÊN_HẰNG = value;
• Access
tên_lớp::TÊN_HẰNG // ngoài lớp
seft::TÊN_HẰNG // trong lớp
• Declare
… static $thuộc_tính
• Truy xuất
tên_lớp::$thuộc_tính // ngoài lớp
seft::$thuộc_tính // trong lớp
• Khai báo
– … static function phương_thức(…)
• Truy xuất
– tên_lớp::phương_thức(…) // ngoài lớp
– seft:: phương_thức(…) // trong lớp
• Declare subclasses
class lớp_con extends lớp_cha
{
properties and methods
}
• Declare Interface
interface giao_diện
{
public function phương_thức();
…
}
$luu = serialize($ob1);
echo "$luu <br>";
$ob2 = unserialize($luu);
var_dump($ob2);
?>
2
Internet
or Intranet
Yêu cầu trang b.php 7
cookie
$_COOKIE
setcookie
Client Webserver
• Xóa cookie
setcookie(“cookieName", "", time() -3600);
$_COOKIE[fieldName]
cookie
Lưu username & password
setcookie
Client
Webserver
User 2
Session: user 1
User 1 Session: user 2
Session: user 3
User 3
session_start
Client
Webserver
if (isset($_SESSION[“sessionVar"])
echo $_SESSION[“sessionVar"];
• Cancel Session
session_destroy();
if (!isset($_SESSION[“IsLogin”]) ||
$_SESSION[“IsLogin”] == false)
header(“Location: login.htm”);
?>
{13, 2, 7}
Session 1
Cookie2
Client 1 Web Server
Session 2 Session 3
Internet
or Intranet
Client 2 Database Server
Cookie3
Client 3
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
HienLTH – KhietLTN Web Design 40
4.Process the date
<?php
// Assuming today is: March 10th, 2001, 5:16:18 pm
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day z ');
// 05-16-17, 10-03-01, 1631 1618 6 Fripm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // It is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 15:16:08 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:17 m is month
$today = date("H:i:s"); // 17:16:17
?>
HienLTH – KhietLTN Web Design 41
Using date() function
string date ( string $format [, int $timestamp] )
format
l A full textual representation of the day of the week Sunday through Saturday
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
F A full textual representation of a month, such as January or March January through December
DAYS MONTH
d - day of the month 2 digits (01-31) F - Full name of month (January - December)
j - day of the month (1-31) m - 2 digit month number (01-12)
D - 3 letter day (Mon - Sun) n - month number (1-12)
l - full name of day (Monday - Sunday) M - 3 letter month (Jan - Dec)
N - 1=Monday, 2=Tuesday, etc (1-7) t - Days in the month (28-31)
S - suffix for date (st, nd, rd)
w - 0=Sunday, 1=Monday (0-6) YEAR
z - day of the year (1=365) L - leap year (0 no, 1 yes)
o - ISO-8601 year number (Ex. 1979, 2006)
WEEK
W - week of the year (1-52)
TIME
a - am or pm
A - AM or PM
B - Swatch Internet time (000 - 999)
g - 12 hour (1-12)
G - 24 hour c (0-23)
h - 2 digit 12 hour (01-12)
H - 2 digit 24 hour (00-23)
i - 2 digit minutes (00-59) <?php
s - 2 digit seconds (00-59) echo date (‘g:i:s A’);
?>
OTHER
e - timezone (Ex: GMT, CST) 11:19:26 PM
I - daylight savings (1=yes, 0=no)
O - offset GMT (Ex: 0200)
Z - offset in seconds (-43200 - 43200)
r - full RFC 2822 formatted date
HienLTH – KhietLTN Web Design 44
Using getdate() function
timestamp
<html>
Gogle Chrome - x
File Edit View Favorites Tool Help
<head> Go
<title>My PHP</title> Address http://localhost/myweb/index.php
</head>
<body> Array ( [seconds] => 19
<? [minutes] => 35
$today = getdate(); [hours] => 3
print_r($today); [mday] => 20
?> [wday] => 3
</body> [mon] => 4
</html> [year] => 2011
[yday] => 109
[weekday] => Wednesday
[month] => April
[0] => 1303270519 )
<html>
Google Chrome - x
<head> File Edit View Favorites Tool Help
Go
<title>My PHP</title> Address http://localhost/myweb/index.php
</head>
<body> bool(true) bool(false)
<?
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
?>
</body>
</html>
1. Use include ()
2. Use include_once ()
3. Use include_path for big project
4. Check the file
5. Read and write files
6. Working with directories
fopen($filename, $mode);
fwrite($handle, $string);
fread($handle, $length);
fgets($handle);
sprintf($format);
fscanf($handle, $format);
fseek($handle, $offset);
fclose($handle);
file_get_contents($filename);
Internet
or Intranet
file
Disk
driver
Upload.php
• $_FILES[“file”]["name"]
• $_FILES[“file”]["type"]
• $_FILES[“file”]["size"]
• $_FILES[“file”]["tmp_name"]
• $_FILES["file”]["error"]
Mảng $_FILE
Element Contains Example
$ FILES['taptin']['name'] Name of uploaded file test.gif
Mã lỗi
Constant Name Value Explanation
UPLOAD_ERR_OK 0 No problem
UPLOAD_ERR_INI_SIZE 1 File size exceeds php.ini limit set in
upload_max_filesize
UPLOAD_ERR_FORM_SIZ 2 File size exceeds limit set in hidden
E element named MAX_FILE_SIZE
UPLOAD_ERR_PARTIAL 3 File only partially uploaded
UPLOAD_ERR_NO_FILE 4 File was not uploaded
67
HienLTH – KhietLTN Web Design 67
Check the error, format, file
upload size
• Note:
– $_FILES[“…”][“type”]
• “image/gif”
• “image/jpeg” → Firefox nhận đây là file
jpeg
• “image/pjpeg” → IE nhận đây là file jpeg
– $_FILES[“…”][“size”] : Kích thước file tính
theo byte
– $_FILES[“…”][“error”] : Mã lỗi khi upload File
• = 0 : Không có lỗi
• > 0 : Có lỗi
HienLTH – KhietLTN Web Design 68
7. Image
<html>
Microsoft Internet Explorer - x
<head> File Edit View Favorites Tool Help
Go
<title>My PHP</title> Address http://localhost/myweb/index.php
• Using try…catch
try
{
// do something that can go wrong
}
catch (Exception $e)
{
throw new Exception( 'Something really gone wrong', 0, $e);
}
http://php.net/manual/en/language.exceptions.php
• Website W3school
• Slide lập trình Web, ĐH KHTN, 2007
▪ Họ tên:
▪ Mã SV:
▪ Lớp:
▪ Khoá:
▪ Email:
01 02 … 19 20