SESSION is a commonly used thing in php. We often use it to record global page information. If the user logs in, background management, and another commonly used one is the shopping cart class. Let me introduce it to you. one time.
Regarding the application of SESSION in PHP, it is essential and one of the most important functions. SESSION is called "session" in network applications. We usually understand it as storing the information required for a specific user session. In this way, When the user jumps between website pages, the stored SESSION value is not lost, but survives throughout the user session. In layman's terms, when user A goes online, an ID (a) value will be created and saved. If your ID (A) value is not logged out, the website will remember your ID (A) the next time you go online. ) value, at this time you can call your ID (A) value online. For example, you are welcome to visit your ID (A) value again.
It is very simple to apply the SESSION value in PHP. Just enter session_start() at the top to start the session. Then you can use SESSION. This is just an application method for small websites. In fact, SESSION itself is also There are many attributes, such as SESSION cycle, calling SESSION, SESSION data validity period, SESSION save, SESSION logout, etc. If you have these attributes, it seems to be a relatively standardized SESSION application session.
The following is a complete Session class, which integrates the most basic attribute values of Session. Among them, opening, closing and cleaning are in line with PHP programming specifications, which is also a good habit. A small note, if the website does not use the Session class extensively, there is basically no need to use the SESSION class.
代码如下 |
复制代码 |
/**
* 文件描述 Session类
* =================================================================
* 文件名称 session.class.php
* -----------------------------------------------------------------
* 适用环境: PHP5.2.x / mysql 5.0.x
* -----------------------------------------------------------------
* 作 者 04ie。com
* -----------------------------------------------------------------
* 创建时间 2010-2-1
* =================================================================
*/
class Session
{
/**
* session默认有效时间
* @access public
* @var ineger $_expiry
*/
public $_expiry = 3600;
/**
* 有效域名
* @access public
* @var string $_domain
*/
public $_domain = '.phpfamily.cn';
//初始化
public function __construct()
{
ini_set('session.use_trans_id', 0);
ini_set('session.gc_maxlifetime', $this->_expiry);
ini_set('session.use_cookie', 1);
ini_set('session.cookie_path', '/');
ini_set('session.cookie_domain', $this->_domain);
session_module_name('user');
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
session_start();
}
/**
* 打开session
* @access public
* @param string $savePath
* @param string $sName
* @return true
*/
public function open($savePath, $sName)
{
$this->_conn = mysql_connect('localhost', 'root', '');
mysql_select_db('databases');
mysql_query('SET NAMES "utf8"');
return true;
}
/**
* 关闭session
* @access public
* @return bool
*/
public function close()
{
return mysql_close($this->_conn);
}
/**
* 读取session
* @access public
* @param string $sid sessionID
* @return mixed
*/
public function read($sid)
{
$sql = "SELECT data FROM sessions WHERE sessionid='%s'";
$sql = sprintf($sql, $sid);
$res = mysql_query($sql, $this->_conn);
$row = mysql_fetch_assoc($res);
return !$row ? null : $row['data'];
}
/**
* 写入session
* @access public
* @param string $sid sessionID
* @param string $data serialize序列化后的session内容
* @return
*/
public function write($sid, $data)
{
$expiry = time() + $this->_expiry;
$sql = "REPLACE INTO sessions (sessionid,expiratio
n,data) VALUES ('%s', '%d', '%s')";
$sql = sprintf($sql, $sid, $expiry, $data);
mysql_query($sql, $this->_conn);
return true;
}
/**
* 销毁session
* @access public
* @param string $sid sessionID
* @return
*/
public function destroy($sid)
{
$sql = "DELETE FROM sessions WHERE sessionid='%s'";
$sql = sprintf($sql, $sid);
mysql_query($sql, $this->_conn);
return true;
}
/**
* 清理过期session
* @access public
* @param integer $time
* @return
*/
public function gc($time = 0)
{
$sql = "DELETE FROM sessions WHERE expiration < '%d'";
$sql = sprintf($sql, time());
mysql_query($sql, $this->_conn);
mysql_query('OPTIMIZE TABLE sessions');
return true;
}
|
Let’s look at another php session shopping cart class
The code is as follows
代码如下 |
复制代码 |
class Cart{
public function Cart() {
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
}
/*
添加商品
param int $id 商品主键
string $name 商品名称
float $price 商品价格
int $num 购物数量
*/
public function addItem($id,$name,$price,$num,$img) {
//如果该商品已存在则直接加其数量
if (isset($_SESSION['cart'][$id])) {
$this->incNum($id,$num);
return;
}
$item = array();
$item['id'] = $id;
$item['name'] = $name;
$item['price'] = $price;
$item['num'] = $num;
$item['img'] = $img;
$_SESSION['cart'][$id] = $item;
}
/*
修改购物车中的商品数量
int $id 商品主键
int $num 某商品修改后的数量,即直接把某商品
的数量改为$num
*/
public function modNum($id,$num=1) {
if (!isset($_SESSION['cart'][$id])) {
return false;
}
$_SESSION['cart'][$id]['num'] = $num;
}
/*
商品数量+1
*/
public function incNum($id,$num=1) {
if (isset($_SESSION['cart'][$id])) {
$_SESSION['cart'][$id]['num'] += $num;
}
}
/*
商品数量-1
*/
public function decNum($id,$num=1) {
if (isset($_SESSION['cart'][$id])) {
$_SESSION['cart'][$id]['num'] -= $num;
}
//如果减少后,数量为0,则把这个商品删掉
if ($_SESSION['cart'][$id]['num'] <1) {
$this->delItem($id);
}
}
/*
删除商品
*/
public function delItem($id) {
unset($_SESSION['cart'][$id]);
}
/*
获取单个商品
*/
public function getItem($id) {
return $_SESSION['cart'][$id];
}
/*
查询购物车中商品的种类
*/
public function getCnt() {
return count($_SESSION['cart']);
}
/*
查询购物车中商品的个数
*/
public function getNum(){
if ($this->getCnt() == 0) {
//种数为0,个数也为0
return 0;
}
$sum = 0;
$data = $_SESSION['cart'];
foreach ($data as $item) {
$sum += $item['num'];
}
return $sum;
}
/*
购物车中商品的总金额
*/
public function getPrice() {
//数量为0,价钱为0
if ($this->getCnt() == 0) {
return 0;
}
$price = 0.00;
foreach ($this->items as $item) {
$price += $item['num'] * $item['price'];
}
return sprintf("%01.2f", $price);
}
/*
清空购物车
*/
public function clear() {
$_SESSION['cart'] = array();
}
}
|
|
Copy code |
|
class Cart{
public function Cart() {
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
}<🎜>
<🎜> /*
Add product
param int $id product primary key
String $name product name
float $price product price
int $num Shopping quantity
*/
public function addItem($id,$name,$price,$num,$img) {
//If the product already exists, add its quantity directly
if (isset($_SESSION['cart'][$id])) {
$this->incNum($id,$num);
Return;
}
$item = array();
$item['id'] = $id;
$item['name'] = $name;
$item['price'] = $price;
$item['num'] = $num;
$item['img'] = $img;
$_SESSION['cart'][$id] = $item;
}
/*
Modify the quantity of items in the shopping cart
int $id product primary key
int $num is the modified quantity of a certain product, that is, directly replace a certain product
The quantity is changed to $num
*/
public function modNum($id,$num=1) {
if (!isset($_SESSION['cart'][$id])) {
Return false;
}
$_SESSION['cart'][$id]['num'] = $num;
}
/*
Product quantity +1
*/
public function incNum($id,$num=1) {
if (isset($_SESSION['cart'][$id])) {
$_SESSION['cart'][$id]['num'] += $num;
}
}
/*
Product quantity-1
*/
public function decNum($id,$num=1) {
if (isset($_SESSION['cart'][$id])) {
$_SESSION['cart'][$id]['num'] -= $num;
}
//If the quantity is 0 after reduction, delete this product
if ($_SESSION['cart'][$id]['num'] <1) {
$this->delItem($id);
}
}
/*
Delete product
*/
public function delItem($id) {
unset($_SESSION['cart'][$id]);
}
/*
Get a single product
*/
public function getItem($id) {
return $_SESSION['cart'][$id];
}
/*
Check the types of items in the shopping cart
*/
public function getCnt() {
return count($_SESSION['cart']);
}
/*
Query the number of items in the shopping cart
*/
public function getNum(){
if ($this->getCnt() == 0) {
//The number of species is 0, and the number is also 0
Return 0;
}
$sum = 0;
$data = $_SESSION['cart'];
foreach ($data as $item) {
$sum += $item['num'];
}
return $sum;
}
/*
Total amount of items in shopping cart
*/
public function getPrice() {
//Quantity is 0, price is 0
if ($this->getCnt() == 0) {
Return 0;
}
$price = 0.00;
foreach ($this->items as $item) {
$price += $item['num'] * $item['price'];
}
return sprintf("%01.2f", $price);
}
/*
Clear shopping cart
*/
public function clear() {
$_SESSION['cart'] = array();
}
}
http://www.bkjia.com/PHPjc/633150.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633150.htmlTechArticleSESSION is a commonly used thing in php. We often use it to record global page information. If the user Login, backend management, and a commonly used shopping cart category, next...