You are on page 1of 6

BI 11: VIT NG NG NG NHP BNG PHP V MYSQL

nhng bi trc chng ta hc qua cc kin thc trng yu v PHP. Tuy nhin, c th xy dng mt website hon chnh bng nhng kin thc th qu tht khng n gin. Bi v cc kin thc qua sch v v ti liu d sao cng ch l nhng kin thc tng qut v thiu tnh khch quan thc tin. Nn khi ngi hc lp trnh bc vo giai on vit ng dng th thng rt lng tng. cng l l do ti vit bi ny hng dn cc bn dn lm quen vi cch tip cn mt ng dng PHP v MYSQL nh th no.

Trc ht chng ta phi thit k v xy dng m hnh c s d liu ng vi tng lnh bn di (xem li bi ngn ng SQL v MYSQL Cn Bn) sau mi tin hnh trin khai lp trnh. 1 mysql -hlocalhost -uroot -proot 2 mysql>create database project 3 mysql>use project; mysql>create table user( id INT(10) 4 UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password CHAR(50) NOT NULL, le velCHAR(1) NOT NULL, PRIMARY KEY(id)); 5 mysql>insert into user(username,password,level) values("admin","12345","2" ), ("abc","12345","1");

Vy l chng ta c c s d liu nh m hnh trn. Tip n chng ta to file login.php v thit k Form HTML c mn hnh ng nhp khi ngi dng truy cp.

Website hc trc tuyn www.videobook.vn

1 <form action='login.php' method='post'> 2 Username: <input type='text' name='username' size='25' /><br /> 3 Password: <input type='password' name='password' size='25' /><br /> 4 <input type='submit' name='ok' value='Dang Nhap' /> 5 </form> Tip n chng ta tin hnh kim tra d liu t form 01 <?php 02 if(isset($_POST['ok'])) 03 { 04 $u=$p=""; 05 if($_POST['username'] == NULL) 06 { 07 echo "Please enter your username<br />"; 08 } 09 else 10 { 11 $u=$_POST['username']; 12 } 13 if($_POST['password'] == NULL) 14 { 15 echo "Please enter your password<br />"; Website hc trc tuyn www.videobook.vn

16 } 17 else 18 { 19 $p=$_POST['password']; 20 } 21 } 22 ?> on code trn s kim tra xem ngi dng c tin hnh nhn nt ng nhp hay khng. V nu c th chng ta s xt tip tnh trng ngi dng c trng cc username v password hay khng. Nu c chng ta s thng bo li bn trn form, ngi s dng tin hnh nhp liu. V phin bn PHP 5.3 tr ln s yu cu ta khai bo bin trc khi s dng. V th mc nh ban u ta khi to 2 bin $u v $p mang gi tr l rng.

K n chng ta kim tra xem c tn ti hai bin $u v $p (ch khi ngi dng ng nhp thnh cng th mi c th to ra 2 bin ). Tip n chng ta tin hnh kt ni c s d liu (xem li bikt hp PHP & MYSQL trong ng dng web). 1 <?php 2 $conn=mysql_connect("localhost","root","root") or die("can't connect this database");

3 mysql_select_db("project",$conn); 4 ?> V tin hnh kim tra xem username v password ngi s dng va nhp c trng khp vi thng tin c trong c s d liu hay khng ?. Nu khng th chng ta s bo li ngay. Ngc li s tin hnh ly d liu t bng v gn Website hc trc tuyn www.videobook.vn

vo session. c th qun l phin lm vic mt cch hiu qu trn mi trang ca khu vc admin (xem li bi khi nim c bn v cookie v session). 01 <?php 02 if($u && $p) 03 { 04 $conn=mysql_connect("localhost","root","root") or die("can't connect this database");

05 mysql_select_db("project",$conn); 06 $sql="select * from user where username='".$u."' and password='".$p."'"; 07 $query=mysql_query($sql); 08 if(mysql_num_rows($query) == 0) 09 { 10 echo "Username or password is not correct, please try again"; 11 } 12 else 13 { 14 $row=mysql_fetch_array($query); 15 session_start(); 16 $_SESSION['userid'] = $row['id']; 17 $_SESSION['level'] = $row['level']; 18 } 19 } 20 ?> Nh vy code hon chnh cho ng dng (file login.php) ny l nh sau: <?php

Website hc trc tuyn www.videobook.vn

if(isset($_POST['ok'])) { $u=$p=""; if($_POST['username'] == NULL) { echo "Please enter your username<br />"; } else { $u=$_POST['username']; } if($_POST['password'] == NULL) { echo "Please enter your password<br />"; } else { $p=$_POST['password']; } if($u && $p) { $conn=mysql_connect("localhost","root","root") or die("can't connect this database"); mysql_select_db("project",$conn); $sql="select password='".$p."'"; $query=mysql_query($sql); if(mysql_num_rows($query) == 0) * from user where username='".$u."' and

Website hc trc tuyn www.videobook.vn

{ echo "Username or password is not correct, please try again"; } else { $row=mysql_fetch_array($query); session_start(); $_SESSION['id'] = $row[id]; $_SESSION['level'] = $row[level];

} } } ?> <form action='login.php' method='post'> Username: <input type='text' name='username' size='25' /><br /> Password: <input type='password' name='password' size='25' /><br /> <input type='submit' name='ok' value='Dang Nhap' /> </form>

Website hc trc tuyn www.videobook.vn