You are on page 1of 25

i Hc S Phm Tp.

H Ch Minh

LP TRNH WEB PHP

Chng 05: PROJECT

Ni dung
Thit k giao din, b cc Hin th ting Vit trong trang Web. Xy dng hm kt ni CSDL dng chung. Login. Upload/Download Trang sn phm: xem, t mua (viewer) thm, cp nht, xa (admin) Phn trang Xut bn website
2

Layout
Ty chn. t nht s c:
Banner (hnh nh cng ty, t chc, c nhn, ) Menu/Link (Cc lin kt trong trang) Main (Ni dung trang web) Footer (Bn quyn a ch)

Ting vit trong trang Web


Th meta trong file HTML/PHP
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

DreamWeaver: Ctrl + J chn Encoding: UTF8. CSDL: mysqli_query("SET NAMES utf8;");

Hm kt ni CSDL dng chung


//File: connect.php define(HOST, localhost); define("USR", "root"); define("PWD", ""); define("DBNAME","baitap"); $conn = mysqli_connect(HOST,USR,PWD) or die(Khng kt ni c!); mysqli_select_db(DBNAME);

Login Page
Xy dng file login.php dng ng nhp

Process Login Page


require(connect.php); $sql = "SELECT count(*) FROM user WHERE username='{$_POST['user']}' AND password ='".md5($_POST['pass'])."'"; $result = mysqli_query($sql); if(mysqli_num_rows($result)){ echo 'ng nhp thnh cng'; $_SESSION[user] = $_POST['user']; } else{ //echo 'ng nhp li'; header(location:login.php); }
7

Upload
Xy dng file upload.php upload file.

X l upload
Khai bo ni lu tr:
$target_path = "data/"; $target_path = $target_path.basename( $_FILES['uploadedfile']['name']);

Upload file ln server:


move_uploaded_file($_FILES['uploadedfile'][' tmp_name'], $target_path)

Lu 1 s thng tin vo CSDL


$sql = "INSERT INTO file (chude, tenfile, loaifile) VALUES ('{$_POST['chude']}', '{$target_path}','$loaifile')";
9

Download file
Cch 1: Trc tip qua th <a> Cch 2: Xy dng trang download
function DownloadFile($file) { // $file = include path if(file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } }

Gi: DownloadFile($_GET[filename]);
10

WEB SN PHM
PRESENTED BY HIEN, LUONG TRAN HY HIENLTH@HCMUP.EDU.VN

Yu cu
Ngi dng (viewer):
Xem thng tin tng chng loi sn phm. Xem chi tit 01 sn phm Mua hng

Qun tr (admin)
To 01 (loi) sn phm mi. Chnh sa/Xa thng tin 01 sn phm Gi hng

12

Phn trang
S dng bin truyn dng GET, mc nh l trang u tin nu bin cha t tr.
if (isset($_GET[page])) $page = $_GET[page]; else $page = 1;

S lng sn phm trnh by trn 01 trang: $pp = ?? Truy vn CSDL ch : LIMIT $start, $pp
Trong $start = ($page 1) * $pp;

13

XUT BN SITE
PRESENTED BY HIEN, LUONG TRAN HY HIENLTH@MATH.HCMUP.EDU.VN

Xut bn website
http://vn2k.net/signup.php in cc thng tin theo mu
Tn ng nhp (username) Mt khu (password) Email Loi site v ngn ng in m xc nhn

15

Xut bn website (tt)


Lu gi cc thng tin v website

16

Upload thng tin ln host


Bng FTP (dng Total Commander) Trc tip: Vo Cpanel File Manager

File lu tr trong th mc ny 17

Upload thng tin ln host

Chn Upload

Bm y Upload

18

Upload thng tin ln host dng Total Commander

Bm nt FTP

19

Upload thng tin ln host dng Total Commander

Session: in vo 1 ci tn ty Hostname, username, password in thng tin ca bn

20

Upload thng tin ln host dng Total Commander

Chn v connect

21

Upload thng tin ln host dng Total Commander

Thc hin chn file v copy (F5)

22

To v s dng CSDL
Chn MySQL Database trong ca s CPanel

Import Database

Ca s PHPMyAdmin
23

B m bng c file
<? $CountFile = "Counter.log"; $CF = fopen ($CountFile, "r"); $Hits = fread ($CF, filesize ($CountFile) ); fclose ($CF); $Hits++; //Hin th ra mn hnh $CF = fopen ($CountFile, "w"); fwrite ($CF, $Hits); fclose ($CF); echo ($Hits); ?>

24

Cu hi v tho lun

25

You might also like