Cookie
COOKIE
Dùng để lưu thông tin của người dùng.
Cookie được lưu ở máy client, browser
quản lý
Mỗi biến cookie có 1 thời gian quá hạn.
Vượt qua thời điểm đó, browser sẽ xóa
biến cookie
Mỗi lần thực hiện request 1 trang, browser
sẽ gửi lại các biến cookie chưa hết hạn
Được đặt trước thẻ <html>
2
COOKIE
Tạo cookie
Cú pháp
setcookie(“tên”, giá-trị [, thời
điểm quá hạn])
Sử dụng cookie
$_COOKIE là dãy biến toàn cục sẵn có trong
php
Cú pháp sử dụng biến cookie
$_COOKIE[“TenBien”]
3
COOKIE
setcookie.php
$value = "something from somewhere";
setcookie("TestCookie", $value, time() +
3600); /* hết hạn trong 1 giờ */
?>
viewcookie.php
if(isset($_COOKIE["TestCookie"]==true))
echo $_COOKIE["TestCookie"];
?>
4
COOKIE
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['cookie'])){
foreach ($_COOKIE['cookie'] as $name => $value){
$name = htmlspecialchars($name);
$value = htmlspecialchars($value);
echo "$name : $value
\n";
}
}
?>
5
COOKIE – ví dụ
Form đăng nhập
action="xuly.php">
Tên
<input type="text" name="txtten"/>
Mật khẩu
<input type="password" name="txtmatkhau" />
<input type="checkbox" name="chknho" />
Nhớ thông tin này
<input type="submit" name="btngui" value="Gửi"/>
</form>
6
COOKIE – ví dụ
xuly.php
if(isset($_POST["chknho"]) == true){
setcookie("ten",$_POST["txtten"],time()+3600)
;
setcookie("matkhau",$_POST["txtmatkhau"],time
()+3600);
}
else{
setcookie("ten",$_POST["txtten"],-1);
setcookie("matkhau",$_POST["txtmatkhau"],-1);
}
?>
7
Session
SESSION
Session là đối tượng trên server, chứa
thông tin của từng user
Mỗi user có vùng session riêng biệt
$_SESSION là 1 dãy toàn cục có sẵn
trong php.
9
SESSION
Tạo session
$_SESSION["sessionName"] = value;
$_SESSION["sessionName"][ ]=
array(…);
Đọc giá trị session
if (isset($_SESSION[sessionName"])
echo $_SESSION["sessionName"];
Xóa bỏ session
unset($_SESSION["sessionName"]);
session_destroy();
10
SESSION
VD
session_start();
$_SESSION["login"]=1;
$_SESSION["name"]="abc";
header("Location: index.php");
?>
11
Ứng dụng Session
Làm thế nào để ngăn không cho người
dùng truy cập vào các trang web nếu chưa
đăng nhập?
Ý tưởng: dùng các biến Session để lưu
trạng thái đăng nhập của người dùng
$_SESSION[“Login”] = 0/1: lưu trạng thái
đăng nhập
$_SESSION[ “Username”]: lưu tên đăng nhập
$_SESSION[ “Authentication”]: lưu loại quyền
đăng nhập
…
12
Ứng dụng Session
Tạo trang login.htm yêu cầu người dùng đăng nhập
Tạo trang validateuser.php xử lí thông tin đăng nhập từ
trang login
Kết nối với CSDL, kiểm tra xem thông tin đăng nhập có
đúng hay không
Nếu không đúng thì cho chuyển hướng đến trang
login.htm
Nếu đúng thì dùng một biến Session để lưu trạng thái
login thành công lại.
• Ví dụ $_SESSION["IsLogin“] = 1;
Tạo trang logout.php là trang xử lý khi người dùng logout
Reset trạng thái login là chưa đăng nhập
• Ví dụ unset($_SESSION[“IsLogin”]);
13
Tập tin, thư mục
Quản lý file & thư mục
Hàm thao tác trên file
res fopen(string $filename, string
$mode)
bool fclose(res $handle)
int fpassthru (res $handle)
gửi toàn bộ nội dung file đang mở cho browser
(binary)
int readfile (string $filename)
gửi toàn bộ nội dung file đang mở cho browser (text)
string fread(res $handle, int $length)
đọc một khối dữ liệu dài tối đa length (binary)
int fwrite(res $handle, string $str [,
int $length])
ghi nội dung của chuỗi ra file
15
Quản lý file & thư mục
Hàm thao tác trên thư mục
bool chdir ( string $directory ):
Chuyển thư mục hiện hành
string getcwd (): Trả về thư mục hiện hành
resource opendir (string $path): Mở
một thư mục trước khi đọc nội dung
void closedir (res $dir_handle): Đóng
thư mục đã mở trước đó
bool mkdir ( string $pathname): Tạo thư
mục
bool rmdir ( string $dirname): Xóa thư
mục
16
VD hàm xóa thư mục
function removeDirectory($dir){
$s = DIRECTORY_SEPARATOR;
$dir = opendir($dir);
while(($file=readdir($dir))!==false){
if (is_file($dir . $s . $file)) {
unlink($dir . $s .$file);
} else if(is_dir($dir . $s .$file)
&&($file != ".")&&($file != "..")) {
removeDirectory($dir .$s. $file);
} }
closedir($dir); rmdir($dir);
printf("Directory %s removed", $dir);
}
?>
17
File Upload
<html>
<head><title> Form for Uploading a File </title>
</head>
<body>
A simple form for uploading a file