CI框架中cookie的操作方法分析,cicookie
CI框架中cookie的操作方法分析,cicookie
本文实例讲述了CI框架中cookie的操作方法。分享给大家供大家参考。具体分析如下:
第一种设置cookie的方式:采用php原生态的方法设置的cookie的值
复制代码 代码如下:
setcookie("user_id",$user_info['user_id'],86500);
setcookie("username",$user_info['username'],86500);
setcookie("password",$user_info['password'],86500);
//echo $_COOKIE['username'];
第二种设置cookie的方式:通过CI框架的input类库设置cookie的值
复制代码 代码如下:
$this->input->set_cookie("username",$user_info['username'],60);
$this->input->set_cookie("password",$user_info['password'],60);
$this->input->set_cookie("user_id",$user_info['user_id'],60);
//echo $this->input->cookie("password");//适用于控制器
//echo $this->input->cookie("username");//适用于控制器
//echo $_COOKIE['username'];//在模型类中可以通过这种方式获取cookie值
//echo $_COOKIE['password'];//在模型类中可以通过这种方式获取cookie值
第三种设置cookie的方式:通过CI框架的cookie_helper.php辅助函数库设置cookie的值
复制代码 代码如下:
set_cookie("username",$user_info['username'],60);
set_cookie("password",$user_info['password'],60);
set_cookie("user_id",$user_info['user_id'],60);
//echo get_cookie("username");
例子自定义扩展核心控制器类
复制代码 代码如下:
class MY_Controller extends CI_Controller{
//构造函数:在构造函数中判断用户是否已经登陆,如果登陆,可进入后台控制器,返回跳转到登陆页面
public function __construct(){
parent::__construct();
$this->load->helper("url");
$this->load->model("user_model");//user_model模型类实例化对象
$this->cur_user=$this->user_model->is_login();//检测是否登陆,如果登陆,返回登陆用户信息,否则返回false
if($this->cur_user === false){
header("location:".site_url("index/login"));
}else{
//如果已经登陆,则重新设置cookie的有效期
$this->input->set_cookie("username",$this->cur_user['username'],60);
$this->input->set_cookie("password",$this->cur_user['password'],00);
$this->input->set_cookie("user_id",$this->cur_user['user_id'],60);
}
}
}
?>
希望本文所述对大家基于CI框架的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

The dangers of cookie leakage include theft of personal identity information, tracking of personal online behavior, and account theft. Detailed introduction: 1. Personal identity information is stolen, such as name, email address, phone number, etc. This information may be used by criminals to carry out identity theft, fraud and other illegal activities; 2. Personal online behavior is tracked and analyzed through cookies With the data in the account, criminals can learn about the user's browsing history, shopping preferences, hobbies, etc.; 3. The account is stolen, bypassing login verification, directly accessing the user's account, etc.

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

The effects of clearing cookies include resetting personalization settings and preferences, affecting ad experience, and destroying login status and password remembering functions. Detailed introduction: 1. Reset personalized settings and preferences. If cookies are cleared, the shopping cart will be reset to empty and products need to be re-added. Clearing cookies will also cause the login status on social media platforms to be lost, requiring re-adding. Enter your username and password; 2. It affects the advertising experience. If cookies are cleared, the website will not be able to understand our interests and preferences, and will display irrelevant ads, etc.
