You are on page 1of 4

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.
mysql
-hlocalhost -uroot -proot
mysql>create database project;
mysql>use project;
mysql>create table user( id INT(

1
2
3
4
5
mysql -hlocalhost -uroot -proot
mysql>create database project;
mysql>use project;
mysql>create table user( id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL, password CHAR(50) NOT NULL, level
CHAR(1) NOT NULL, PRIMARY KEY(id));
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.


<f orm action='login.php' method
Username: <input type='text' na
Password: <input type='passw
<input type='submit' name='ok' v
</f orm>

1
2
3
4
5
<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>
Tip n chng ta tin hnh kim tra d liu t form
<?php
if (isset($_POST['ok']))
{
$u=$p="";
if ($_POST['username'] == NULL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
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'];
}
}
?>
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 bi kt hp PHP & MYSQL trong ng dng web).
<?php
$conn=mysql_connect("localho
mysql_select_db("project",$con
?>

1
2
3
4
<?php
$conn=mysql_connect("localhost","root","root") or die("can't connect this database");
mysql_select_db("project",$conn);
?>
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 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).
<?php
if ($u && $p)
{
$conn=mysql_connect("localho
mysql_select_db("project",$con

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
if($u && $p)
{
$conn=mysql_connect("localhost","root","root") or die("can't connect this database");
mysql_select_db("project",$conn);
$sql="select * from user where username='".$u."' and password='".$p."'";
$query=mysql_query($sql);
if(mysql_num_rows($query) == 0)
{
echo "Username or password is not correct, please try again";
}
else
{
$row=mysql_fetch_array($query);
session_start();
$_SESSION['userid'] = $row['id'];
$_SESSION['level'] = $row['level'];
}
}
?>
Nh vy code hon chnh cho ng dng (file login.php) ny l nh sau:
<?php
if (isset($_POST['ok']))
{
$u=$p="";
if ($_POST['username'] == NULL

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
if(isset($_POST['ok']))
{
$u=$p="";
if($_POST['username'] == NULL)
{
echo "Please enter your username<br />";
}
else
{
$u=$_POST['username'];
}
if($_POST['password'] == NULL)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{
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 * from user where username='".$u."' and password='".$p."'";
$query=mysql_query($sql);
if(mysql_num_rows($query) == 0)
{
echo "Username or password is not correct, please try again";
}
else
{
$row=mysql_fetch_array($query);
session_start();
$_SESSION['userid'] = $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>
bi sau, chng ta s tip tc tm hiu ng dng kt hp PHP & MYSQL cho vic thm
mt thnh vin nh th no.

You might also like