Simple operation examples of session and cookie in Yii framework study notes
- 2021-12-09 08:40:51
- OfStack
This article illustrates the session and cookie operations of the Yii Framework Learning Note. Share it for your reference, as follows:
session operation
<?php
namespace app\controllers;
use yii\web\Controller;
class HelloController extends Controller{
public function actionIndex(){
// Put session As an object
$session = \yii:$app->session;
if($session->isActive){
echo "session isactive";
}else{
$session->open();
}
$session->set("user"," Zhang 3");
$session->get("user");
$session->remove("user");
// Put session As an array
$session['user'] = " Zhang 3";
unset($session["user"]);
}
}
?>
cookie operation
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\web\Cookie;
class HelloController extends Controller{
// Deposit cookie And modifications cookie
$cookies = \YII::$app->response->cookies;
$cookie_data = array('name'=>"user","value"=>"zhangsan");
$cookies->add(new Cookie($cookie_data));// With 'cookieValidationKey' Encrypted string based on
$cookies->remove("id");// Remove ID Adj. cookie
// Take out cookie Value of
$cookies = \YII::$app->request->cookies;
$cookies->getValue("user");
// If cookie Nonexistent
$cookies->getValue("user",20);// Return 20
}
?>
More readers interested in Yii can check the topics of this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Basic Tutorial of Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
I hope this article is helpful to PHP programming based on Yii framework.