By GokiSoft.com| 20:51 13/05/2021|
Học PHP

[Share Code] Tìm hiểu Cookie - Lập trình PHP/MySQL


#cookie.php


<?php
//Them/sua/xoa cookie -> cung dung chung 1 lenh
setcookie('fullname', 'TRAN VAN DIEP', time() + 300, '/');
setcookie('birthday', '21/05/1988', time() + 300, '/');


#delete-cookie.php


<?php
setcookie('fullname', 'TRAN VAN DIEP', time() - 30, '/');


#read-cookie.php


<?php
// var_dump($_COOKIE);
//lay du lieu trong cookie -> lay can than nhu $_GET, $_POST
$fullname = $birthday = '';

if(isset($_COOKIE['fullname'])) {
	$fullname = $_COOKIE['fullname'];
}

if(isset($_COOKIE['birthday'])) {
	$birthday = $_COOKIE['birthday'];
}

echo $fullname.'-'.$birthday;


#readme.txt


- Nội dung kiên thức
	- overview kiến thức
	- Yêu cầu giải đáp vấn đề gì
	- Bài test: 60 phút -> check kiến thức
		- Kiến thức căn bản: PHP & CSDL
		- Kiến thức nâng cao 1 chút -> cách chúng ta vận dụng như nào.

==============================================================
Mục 1: overview kiến thức
	- Cookie -> Cách dùng để lưu chữ dữ liệu -> giống như localStorage
		- Hiểu về cookie & biết được cách lưu trữ dữ liệu
		- Thêm/sửa/xóa cookie:
			- JS
			- Backend: PHP
Mục 2: Bài tập

===============================================================
BT2290:
B1. Xây dựng database truoc -> database: BT2290
create table products (
	id int primary key auto_increment,
	title varchar(250),
	thumbnail varchar(500),
	content text,
	created_at datetime,
	updated_at datetime
);

create table orders (
	id int primary key auto_increment,
	fullname varchar(100),
	phone_number varchar(20),
	address varchar(200),
	order_date datetime
);

create table order_details (
	id int primary key auto_increment,
	order_id int references orders (id),
	product_id references products (id),
	num int,
	price float
);

B2. Fake data
- insert into -> products: 25 ban ghi la ok

B3. Phat trien chuc nang
- products.php -> html/css/js/bootstrap/jquery
			   -> select * from products (phan trang) -> lay dc du lieu va hien thi ra la ok
			   -> click 1 product -> detail.php?id=?
- detail.php -> Thiet ke giao dien -> follow theo 1 trang ban hang -> lam theo
			 -> id tu $_GET -> lay du lieu trong database cua san pham nay $product
			 -> Hien thi noi dung
			 -> add to cart -> button -> luu cookie
			 Giai phap luu cart (bang cookie)
			 	cart => data gio hang -> string

			 	js/php
			 		cart = [
			 			{
			 				"id": ???,
			 				"num": ???
			 			},{
			 				"id": ???,
			 				"num": ???
			 			},
			 		]
			 		- Them du lieu moi
			 		- TH da ton tai -> update num cho san pham tuong ung
			 		cart -> json tring -> cookie
			 		lay du lieu json string tu cookie -> convert array -> su dung (js/php)
			 			js: JSON.parse & JSON.stringify
			 			php: json_encode & json_decode
- cart.php: code bang php
	- su dung php -> doc cart trong Cookie
	- idList => mang chua id san pham trong gio hang
	- select * from products where id in idList (String)
	- Hien thi ra giao dien html/css/js...
- checkout.php: code php
	- Tham khao thiet ke tren trang thuong mai
	- Code nhu trang cart.php
	- Form nhap thong tin ship.
	- Submit du lieu -> form: thong tin ship hang
	- Xu ly data
		- $_POST: add vao bang orders -> order id cua don hang
		- $_COOKIE: cart -> san pham + so luong -> luu vao trong order_details

	- Xu ly xong data -> xoa du lieu khoi cookie => Thanh cong va xong.


Tags:

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

5

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