You are on page 1of 9

ARRAY TRONG PHP

V d 1:
<?php
$giohang=array();
$diem=array(0,5,7);
print_r($diem); echo "<br>"; //print_r: in ra dang mang
echo $diem[1]. "<br/>";
?>
V d 2:
$diem[]=9;
print_r($dem); echo "<br>";
V d 3:
$sanpham=array("idSP"=> 5,"TenSP"=> "Go","Gia"=>12000);
$sanpham["SoLuong"]=3;
print_r($sanpham); echo "<br>";
echo $sanpham["TenSP"],"<br>";
?>
V d 4:
//mang co chi so la so moi dung duoc vong lap FOR
<?php
$soluong=array(8,10,9) ;
print_r($soluong); echo "<br>";
for ( $i=0 ; $i<count($soluong) ; $i++ ) {
echo "Gi tr th $i l $soluong[$i]<br>";
}
?>
V d 5:
//mang co chi so la chu thi dung vong lap WHILE
//reset
<?php
$sanpham=array("idSP" => 5, "TenSP" => "Go","Gia" => 12000, "SoLuong" => 3);
print_r($sanpham); echo "<br>";
reset($sanpham);
while (key($sanpham) != null) {
echo key($sanpham) . " = " . current($sanpham) . "<br/>";
next($sanpham);
}
?>

V d 6:
<?php
$sanpham = array("idSP"=>5, "TenSP"=>"Go","Gia"=>12000, "SoLuong"=>3);
print_r($sanpham); echo "<br>";
reset($sanpham); // a con tr v phn t u
?>
<table border=1 width=200 cellspacing=0 cellpadding=4>
<?php while (key($sanpham) != null) { ?>
<tr>
<td><?php echo key($sanpham); ?> </td>
<td><?php echo current($sanpham); ?> </td>
</tr>
<?php next($sanpham); // prev: lui tri, next: pt k ?>
<?php } ?>
</table>
V d 7:
<?php
$daySP = array("5"=>"Mm","3"=>"Mui","1"=>"Go","4"=>"Da");
print_r($daySP);
?>
<table border=1 width=200 cellspacing=0 cellpadding=4>
<tr> <th>idSP</th> <th> TnSP </th> </tr>
<?php foreach ($daySP as $k => $v ) { ?>
<tr> <td> <?=$k?> </td> <td> <?=$v?> </td> </tr>
<?php } ?>
</table>
V d 8: array_sum
<?php
$x = array(2, 4, 6, 8);
print_r($x); echo "<br>";
echo "sum(x) = " . array_sum($x) . "<br/>";

$y = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
print_r($y); echo "<br>";
echo "sum(y) = " . array_sum($y) . "<br/>";
?>

V` d 9: in_array
<?
$languages = array("vi", "en", "fr", "jp");
if (in_array( $_GET['lang'], $languages )==true) {
echo "Found";
} else{
echo "NotFound";
}
?>
V d 10: array_search
<?php
$array = array(0=> 'blue', 1=> 'red', 2=> 'green', 3=> 'red');
print_r($array);echo "<br>";
echo "green at: ", array_search('green', $array), "<br>";
echo "red at: ". array_search('red', $array) , "<br>";
?>


TO HM MI TRONG PHP
V d 1: Hm ct du ting vit
<?php
function stripUnicode($str){
if(!$str) return false;
$unicode = array(
'a'=>'||||||||||||||||',
'A'=>'||||||||||||||||',
'd'=>'',
'D'=>'',
'e'=>'||||||||||',
'E'=>'||||||||||',
'i'=>'||||',
'I'=>'||||',
'o'=>'||||||||||||||||',
'O'=>'||||||||||||||||',
'u'=>'||||||||||',
'U'=>'||||||||||',
'y'=>'||||',
'Y'=>'||||'
);
foreach($unicode as $khongdau=>$codau) {
$arr=explode("|",$codau);
$str = str_replace($arr,$khongdau,$str);
}
return $str;
}
?>

V d 2: Hm tr v 1 chui ngu nhin
<?php
function chuoingaunhien($sokytu){
$chuoi="ABCDEFGHIJKLMNOPQRSTUVWXYZWabcdefghijklmnopqrstuvwxyzw
0123456789";
for ($i=0; $i < $sokytu; $i++){
$vitri = mt_rand( 0 ,strlen($chuoi) );
$giatri.= substr($chuoi,$vitri,1 );
}
return $giatri;
}
?>
V d 3: Hm tr v thi im hin hnh bng ting vit
<?php
function LucNayLa() { // Chuyn gi h thng sang ting Vit
$anh = array("Mon","Tue","Wed","Thu","Fri","Sat","Sun","am","pm",":");
$viet = array ("Th hai","Th ba","Th t","Th nm","Th su","Th by",
"Ch nht", " pht, sng", " pht, chiu", " gi " );
$timenow = gmdate("D, d/m/Y - g:i a.", time() + 7*3600);
$t = str_replace( $anh, $viet, $timenow);
return $t;
}
?>

LOGIN LOGOUT HN CH TRUY CP THOT

1. To trang dangnhap.php
<form id="form1" name="form1" method="post"
action="xulydangnhap.php">
<table id=tbldn width=400 border=0 align=center cellpadding=4
cellspacing=0>
<tr> <th colspan="2" align="center">NG NHP</th> </tr>
<tr><td>Username</td>
<td><input type="text" name="username" id="username"/></td>
</tr>
<tr><td>Password</td>
<td><input type="password" name="password" id="password"
/></td>
</tr>
<tr><td>&nbsp;</td>
<td>
<input type="submit" name="btnLog" id="btnLog" value="ng
nhp"/>
<input type="checkbox" name="nho" id="nho" /> Ghi nh
</td>
</tr>
</table>
</form>

<style>
#tbldn { border:solid 3px #036; color:#036; font-weight:bold }
#tbldn th { background-color:#036; color:#6FC; padding:5px}
#username, #password{ background-color:#036; color:#6FF;
padding:3px;
border:solid 1px #990; width:280px;}
#btnLog{ background-color:#036; color:#6FF; width:120px;
padding:3px; border:solid 1px #6FF}
</style>

2. Code x l ng nhp:
<?php
session_start();
require_once("dbconn.php");
$username=$_POST['username'];
$password = $_POST['password'];
$sql = sprintf(" SELECT * FROM users
WHERE username='%s' AND password ='%s'",
$username, $password );
$user = mysql_query($sql);
if (mysql_num_rows($user)==1) {//Thnh cng //cac trang sau do co
the su dung cac bien SESSION nay ve sau
$row_user = mysql_fetch_assoc($user);
$_SESSION['kt_login_id'] = $row_user['idUser'];
$_SESSION['kt_login_user'] = $row_user['Username'];
$_SESSION['kt_login_level'] = $row_user['idGroup'];
$_SESSION['kt_HoTen'] = $row_user['HoTen'];
$_SESSION['kt_GioiTinh'] = $row_user['GioiTinh'];
header("location: index.php");
} else { //Tht bi
header("location: dangnhap.php");
}
?>
if (get_magic_quotes_gpc()== false) {
$username=trim(mysql_real_escape_string($username));//tu dong
them dau nhay vao
$password=trim(mysql_real_escape_string($password));
}

Ghi ch:
- Hm mysql_real_escape_string thng dng escape cc k t c bit trong 1 chui
trc khi c vo cu lnh SQL
- Hm get_magic_quotes_gpc() cho bit cu hnh ca tham s magic_quotes_gpc l on hay
off. Nu tham s magic_quotes_gpc l on th cc k t ' " \ trong cc dy
GET/POST/COOKIE s c escaped, ngha l s thm du \ vo trc cc k t ny


3. Hn ch truy cp :
Trang download.php
G trong body:
Y L TRANG DOWNLOAD SOFTWARE, CH HIN KHI USER LOGIN
<p> <?php echo "Cho bn " . $_SESSION['kt_login_user']; ?> </p>
<p> <a href=thoat.php> Thot </a> </p>

G trn u trang:
<?php
session_start();
if (isset($_SESSION['kt_login_id'])==false) {
$_SESSION['back']= $_SERVER['PHP_SELF'];
header("location: dangnhap.php");
}
//PHP_SELF : chua dia chi trang web hien hanh
?>

4. Thc hin chc nng quay li trang c:
Trong code x l ng nhp, thay dng lnh header("location: index.php") bng
code sau:
if (strlen($_SESSION['back'])>0){
$back = $_SESSION['back'];
unset($_SESSION['back']);// huy bo session
header("location:$back");
} else {
header("location: index.php");
}
5. M ha mt khu:
a. M ho mt khu ca cc user theo gii thut md5
b. Trong code x l ng nhp, thay dng lnh $password= $_POST['password'] bng
code:
$password=md5($_POST['password']);
6. To cookie lu li username, password khi ng nhp
a. Trong code x l ng nhp, sau khi ti cc bin session, th to cookie
if (isset($_POST['nho'])== true){
setcookie("un", $_POST['username'], time() + 60*60*24*7 );
setcookie("pw", $_POST['password'], time() + 60*60*24*7 );
} else {
setcookie("un", $_POST['username'], -1);
setcookie("pw", $_POST['password'], -1);
}
b. Hin th gi tr cookie trong 2 textfield
Text field username: gn thuc tnh value nh sau:
<?php echo $_COOKIE['un'];?>
Text field password: gn thuc tnh value nh sau:
<?php echo $_COOKIE['pw'];?>
c. Test

7. Thoat.php
<?
session_start();
unset($_SESSION['kt_login_id']);
unset($_SESSION['kt_login_user']);
unset($_SESSION['kt_login_level']);
unset($_SESSION['kt_HoTen']);
unset($_SESSION['kt_GioiTinh']);
header('location:index.php');

?>

You might also like