By GokiSoft.com| 13:34 07/05/2021|
Học PHP

[Share Code] Tìm hiểu GET/POST - REQUEST trong lập trình PHP/MySQL - T2008A

https://drive.google.com/file/d/1gIxFluAuXi86jxW4fo0rwStdIT4Doe6h/view

#welcome.php


<!DOCTYPE html>
<html>
<head>
	<title>Welcome PHP</title>
</head>
<body>
<?php
$fullname = '';
if (isset($_GET['fullname'])) {
	$fullname = $_GET['fullname'];
}
?>
	<h1>Hello <?=$fullname?></h1>
	<h1>Hello <?php echo $fullname;?></h1>
</body>
</html>


#vidu.php


<!DOCTYPE html>
<html>
<head>
	<title>Object & Array in PHP</title>
	<meta charset="utf-8">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<?php
// Phan 1: Tim hieu Array trong PHP
// Khai bao 1 mang rong.
$arr = array();
$arr = [];//Viet ngan hon.

// Them 1 phan tu vao trong mang.
$arr[] = 12;
$arr[] = 55;
array_push($arr, 16);

var_dump($arr);

// Duyet qua cac phan tu trong mang
echo '<br/>Cach 1<br/>';
for ($i = 0; $i < sizeof($arr); $i++) {
	echo '<br/>'.$arr[$i].'<br/>';
}
echo '<br/>Cach 2<br/>';
for ($i = 0; $i < count($arr); $i++) {
	echo '<br/>'.$arr[$i].'<br/>';
}

echo '<br/>Cach 3<br/>';
foreach ($arr as $value) {
	echo '<br/>'.$value.'<br/>';
}

// Xoa 1 phan tu trong mang
array_splice($arr, 1, 1);
var_dump($arr);

//Chen them 1 phan tu (gia tri 100) vao vi tri index = 1
array_splice($arr, 1, 0, 100);
var_dump($arr);

// Khai bao 1 mang gom cac phan tu co san
$arr = array(1, 6, 2, 10, 99);
$arr = [1, 6, 2, 10, 99];

// Khai bao 1 mang quan ly du lieu nhu HashMap trong Java.
// Khai bao 1 mang quan ly du lieu theo key => value
// Quan ly thong tin sinh vien gom cac thuoc tinh fullname, address, gender => vao 1 array
$std = [
	'fullname' => 'TRAN VAN A',
	'address'  => 'Ha Noi',
	'gender'   => 'Nam'
];
//Them 1 key moi vao mang $std
$std['birthday'] = '1999-02-09';
var_dump($std);
//Xoa 1 key trong mang std thi lam cach nao
unset($std['gender']);
var_dump($std);
//Lay du lieu trong mang thi lam cach nao
echo $std['fullname'];
//Su dung for de duyet qua cac key =>value trong array $std
foreach ($std as $key => $value) {
	echo '<br/>'.$key.': '.$value.'<br/>';
}

// Khai bao mang quan ly danh sach sinh vien
$stdList = [
	[
		'fullname' => 'TRAN VAN A',
		'address'  => 'Ha Noi',
		'gender'   => 'Nam'
	], [
		'fullname' => 'TRAN VAN B',
		'address'  => 'Ha Nam',
		'gender'   => 'Nam'
	], [
		'fullname' => 'TRAN VAN C',
		'address'  => 'Nam Dinh',
		'gender'   => 'Nam'
	]
];

var_dump($stdList);

$stdList[] = [
	'fullname' => 'TRAN VAN D',
	'address'  => 'Nam Dinh',
	'gender'   => 'Nam'
];

var_dump($stdList);
?>
<div class="container" style="margin-top: 25px;">
		<table class="table table-bordered">
			<thead>
				<tr>
					<th>No</th>
					<th>FullName</th>
					<th>Address</th>
					<th>Gender</th>
				</tr>
			</thead>
			<tbody>
<?php
$count = 0;
foreach ($stdList as $item) {
	echo '<tr>
				<td>'.(++$count).'</td>
				<td>'.$item['fullname'].'</td>
				<td>'.$item['address'].'</td>
				<td>'.$item['gender'].'</td>
			</tr>';
}
?>
			</tbody>
		</table>
	</div>
</body>
</html>


#test.php


<!DOCTYPE html>
<html>
<head>
	<title>MD5 & Base64 PHP</title>
	<meta charset="utf-8">
</head>
<body>
<?php
$pwd    = '123456';
$md5Pwd = md5($pwd);
echo $md5Pwd;

//md5 nhieu + Private key
define('MD5_PRIVATE_KEY', '123JHGjgd9237ksdsjh7376sgdgdgs(9sdf');
$md5Pwd = md5(md5($pwd).MD5_PRIVATE_KEY);
echo '<br/>'.$md5Pwd;

//base 64
//encode
$str          = 'Viết website quản lý sinh viên PHP & MySQL - Lập Trình PHP';
$base64Encode = base64_encode($str);
echo '<br/>'.$base64Encode;

$str          = 'W1NoYXJlIENvZGVdIFF14bqjbiBsw70gc8OhY2ggYuG6sW5nIFBIUCAtIHRyYW5nIHF14bqjbiB0cuG7iyBzw6FjaCBi4bqxbmcgUEhQIC0gTOG6rXAgdHLDrG5oIFBIUCAtIEMyMDEwRw==';
$base64Decode = base64_decode($str);
echo '<br/>'.$base64Decode;
?>
</body>
</html>


#register.php


<?php
require_once ('register_form.php');
?>
<!DOCTYPE html>
<html>
<head>
	<title>Registation Form * Form Tutorial</title>
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Registation Form - Input User's Detail Information</h2>
			</div>
			<div class="panel-body">
				<!-- get/post -->
				<form method="post">
					<div class="form-group">
					  <label for="usr">Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="fullname" value="<?=$fullname?>">
					</div>
					<div class="form-group">
					  <label for="email">Email:</label>
					  <input required="true" type="email" class="form-control" id="email" name="email" value="<?=$email?>">
					</div>
					<div class="form-group">
					  <label for="birthday">Birthday:</label>
					  <input type="date" class="form-control" id="birthday" name="birthday" value="<?=$birthday?>">
					</div>
					<div class="form-group">
					  <label for="pwd">Password:</label>
					  <input required="true" type="password" class="form-control" id="pwd" name="password">
					</div>
					<div class="form-group">
					  <label for="confirmation_pwd">Confirmation Password:</label>
					  <input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirmation_pwd">
					</div>
					<div class="form-group">
					  <label for="address">Address:</label>
					  <input type="text" class="form-control" id="address" name="address" value="<?=$address?>">
					</div>
					<button class="btn btn-success">Register</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>


#register_form.php


<?php
// var_dump($_GET);
// var_dump($_POST);
// var_dump($_REQUEST);
$fullname = $email = $birthday = $password = $confirmation_pwd = $address = '';
if (isset($_POST['fullname'])) {
	$fullname = $_POST['fullname'];
}
if (isset($_POST['email'])) {
	$email = $_POST['email'];
}
if (isset($_POST['birthday'])) {
	$birthday = $_POST['birthday'];
}
if (isset($_POST['password'])) {
	$password = $_POST['password'];
}
if (isset($_POST['confirmation_pwd'])) {
	$confirmation_pwd = $_POST['confirmation_pwd'];
}
if (isset($_POST['address'])) {
	$address = $_POST['address'];
}
// if (isset($_GET['fullname'])) {
// 	$fullname = $_GET['fullname'];
// }
// if (isset($_GET['email'])) {
// 	$email = $_GET['email'];
// }
// if (isset($_GET['birthday'])) {
// 	$birthday = $_GET['birthday'];
// }
// if (isset($_GET['password'])) {
// 	$password = $_GET['password'];
// }
// if (isset($_GET['confirmation_pwd'])) {
// 	$confirmation_pwd = $_GET['confirmation_pwd'];
// }
// if (isset($_GET['address'])) {
// 	$address = $_GET['address'];
// }
// if (isset($_REQUEST['fullname'])) {
// 	$fullname = $_REQUEST['fullname'];
// }
// if (isset($_REQUEST['email'])) {
// 	$email = $_REQUEST['email'];
// }
// if (isset($_REQUEST['birthday'])) {
// 	$birthday = $_REQUEST['birthday'];
// }
// if (isset($_REQUEST['password'])) {
// 	$password = $_REQUEST['password'];
// }
// if (isset($_REQUEST['confirmation_pwd'])) {
// 	$confirmation_pwd = $_REQUEST['confirmation_pwd'];
// }
// if (isset($_REQUEST['address'])) {
// 	$address = $_REQUEST['address'];
// }

// echo $fullname;
if (!empty($fullname) && !empty($password) && $password == $confirmation_pwd) {
	//Dang ky thanh cong
	header('Location: abc/welcome.php?fname='.$fullname);
	die();
}


#readme.txt


- Object & Array trong PHP
- Mã hoá trong PHP
	- md5: string -> md5 encode -> string encrypt -> khong de dich nguoc ve original string:
		- md5($password)
		- md5(md5($password) + PRIVATE_KEY)
	- base64: string -> base64 encode -> string code -> base64 decode -> orignal string.
- Form

- Dky thanh cong -> welcome.php -> Hello FullName
	(fullname <> null & $password == $confirm_pwd)


#welcome.php


<!DOCTYPE html>
<html>
<head>
	<title>Welcome PHP</title>
</head>
<body>
<?php
$fullname = '';
if (isset($_GET['fname'])) {
	$fullname = $_GET['fname'];
}
?>
	<h1>Hello <?=$fullname?></h1>
	<h1>Hello <?php echo $fullname;?></h1>
</body>
</html>


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)