By GokiSoft.com|
18:28 15/01/2024|
Học PHP
[Share Code] Chữa bài tâp - Tìm hiểu Cookie trong PHP - C2307L
https://drive.google.com/file/d/1xg1drLzpmIhQBBoJ31YcOCRgqR1UMG6v/view
#bt1638.php
<?php
$a = $b = $cal = $result = "";
if(isset($_GET['a'])) {
$a = $_GET['a'];
}
if(isset($_GET['b'])) {
$b = $_GET['b'];
}
if(isset($_GET['cal'])) {
$cal = $_GET['cal'];
}
if($a != "" && $b != "" && $cal != "") {
switch ($cal) {
case '+':
$result = $a + $b;
break;
case '-':
$result = $a - $b;
break;
case '*':
$result = $a * $b;
break;
case '/':
$result = $a / $b;
break;
case '%':
$result = $a % $b;
break;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-latest.js"></script>
<title>Calculator Online</title>
<style>
table {
margin: auto;
margin-top: 50px;
background: #BBBBBB;
}
.pheptinh {
background: orange;
}
#c {
width: 100px;
height: 40px;
}
.btn {
width: 50px;
height: 40px;
}
.calcu {
width: 200px;
height: 30px;
}
</style>
</head>
<body>
<form method="get" name="MyForm">
<input type="text" name="a" id="a" class="a" value="" placeholder="Enter a" style="display: none;">
<input type="text" name="b" id="b" class="b" placeholder="Enter b" style="display: none;">
<input type="text" name="cal" id="cal" class="cal" placeholder="Cal" style="display: none;">
<table>
<tr>
<td colspan="4"><input class="calcu" id="calcu" type="text" value="<?=$a.$cal.$b.'='.$result?>" readonly="true"></td>
</tr>
<tr>
<td><input type="button" class="btn" value="AC"></td>
<td><input type="button" class="btn" value="+/-"></td>
<td><input type="button" class="btn" value="%"></td>
<td><input type="button" class="btn pheptinh" value="/"></td>
</tr>
<tr>
<td><input type="button" class="btn" value="7"></td>
<td><input type="button" class="btn" value="8"></td>
<td><input type="button" class="btn" value="9"></td>
<td><input type="button" class="btn pheptinh" value="*"></td>
</tr>
<tr>
<td><input type="button" class="btn" value="4"></td>
<td><input type="button" class="btn" value="5"></td>
<td><input type="button" class="btn" value="6"></td>
<td><input type="button" class="btn pheptinh" value="-"></td>
</tr>
<tr>
<td><input type="button" class="btn" value="1"></td>
<td><input type="button" class="btn" value="2"></td>
<td><input type="button" class="btn" value="3"></td>
<td><input type="button" class="btn pheptinh" value="+"></td>
</tr>
<tr>
<td colspan="2"><input type="button" class="btn" id="c" value="0"></td>
<td><input type="button" class="btn" value="."></td>
<td><input type="submit" class="btn pheptinh" value="="></td>
</tr>
</table>
</form>
<script type="text/javascript">
var a = ""
var b = ""
var cal = ""
$(function() {
//Xu ly logic sau khi tai trang thanh cong
$('.btn').click(function() {
let v = $(this).val()
// console.log(v)
switch(v) {
case 'AC':
a = ''
b = ''
cal = ''
break;
case '+':
case '-':
case '*':
case '/':
case '%':
cal = v
break;
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
// if(a.includes('.') && v == '.') {
// } else {
// a += v;
// }
// if(!a.includes('.') || (a.includes('.') && v != '.')) {
// a += v;
// }
if(cal != "") {
b += v;
if(b.split('.').length >= 3) {
b = parseFloat(b)
}
} else {
a += v;
if(a.split('.').length >= 3) {
a = parseFloat(a)
}
}
// console.log(a)
break;
}
$('#a').val(a)
$('#b').val(b)
$('#cal').val(cal)
$('#calcu').val(`${a}${cal}${b}`)
})
})
</script>
</body>
</html>
#cookie.php
<?php
// var_dump($_COOKIE);
//Them noi dung vao coookie
//fullname = TRAN VAN DIEP, 5S
// setcookie('fullname', 'TRAN VAN DIEP', time() + 30*24*60*60, '/');
//Sua noi dung cookie
// setcookie('a', 'aaaaaaaa', time() + 30*24*60*60, '/');
//Xoa noi dung
// setcookie('b', '', 0, '/');
//Doc noi dung trong cookie
if(isset($_COOKIE['fullname'])) {
$fullname = $_COOKIE['fullname'];
echo $fullname;
}
#readme.txt
Bộ nhơ trong lập trình web:
- localStorage
- Lưu trữ: client & người dùng nào thì xem đc dữ liệu của người dúng đó. Ko chia sẻ đc cho các máy người dùng khác.
- Câu hỏi: Server có xem được dữ liệu localStorage của người dùng ko?
Không
- Dung lượng lưu trữ của localStorage: giới hạn
- SQL Server & MySQL
- Dung lượng: không giới hạn -> mãi mãi
- Cookie
- Lưu trữ ở client
- Giới hạn dung lượng
- Thiết lập thời gian tồn tại -> Sau tgian nay -> Browser tự xóa key đó đi
- Người dùng máy tính có thể xem đc nội dung
- Thao tác bằng tools của trình duyệt
- Dùng JS để thêm/sửa/xóa nội dung cookie
- Server có xem được nội dung cookie của máy tính không?
ĐƯỢC
Server có thể thêm/sửa/xóa được cookie -> Báo cho client biết để lưu lại
- Session
#test.php
<?php
var_dump($_COOKIE);
#vidu.php
<?php
$username = $email = $phone_number = $address = "";
if(isset($_POST['username'])) {
$username = $_POST['username'];
setcookie('username', $username, time() + 200000, '/');
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
setcookie('email', $email, time() + 200000, '/');
}
if(isset($_POST['phone_number'])) {
$phone_number = $_POST['phone_number'];
setcookie('phone_number', $phone_number, time() + 200000, '/');
}
if(isset($_POST['address'])) {
$address = $_POST['address'];
setcookie('address', $address, time() + 200000, '/');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Form tutorial</title>
</head>
<body>
<h1>Header - Form tutorial</h1>
<form method="post">
<label>User Name:</label>
<br/>
<input type="text" placeholder="Enter user name" name="username">
<br/><br/>
<label>Email:</label>
<br/>
<input type="email" placeholder="Enter email" name="email">
<br/><br/>
<label>Phone:</label>
<br/>
<input type="tel" placeholder="Enter phone" name="phone_number">
<br/><br/>
<label>Address:</label>
<br/>
<input type="text" placeholder="Enter address" name="address">
<br/><br/>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
<button type="button">Button</button>
</form>
<h1>Footer</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)