php cookie使用方法学习笔记分享
在php中cookie与其它程序中没什么两样,cookie都是用来存储信息到客户端,常用用于安全性要求不高的一些应用中,如用户登录记住密码之类的,下面我来给大家介绍
PHP setcookie() 函数向客户端发送一个 HTTP cookie。cookie 是由服务器发送到浏览器的变量。cookie 通常是服务器嵌入到用户计算机中的小文本文件。每当计算机通过浏览器请求一个页面,就会发送这个 cookie。cookie 的名称指定为相同名称的变量。例如,如果被发送的 cookie 名为 "name",会自动创建名为 $user 的变量,包含 cookie 的值。
必须在任何其他输出发送前对 cookie 进行赋值。如果成功,则该函数返回 true,否则返回 false。
1 setcookie(name, value, expire, path, domain, secure)
•name 必需。规定 cookie 的名称。
•value 必需。规定 cookie 的值。
•expire 可选。规定 cookie 的有效期。
•path 可选。规定 cookie 的服务器路径。
•domain 可选。规定 cookie 的域名。
•secure 可选。规定是否通过安全的 HTTPS 连接来传输 cookie。
可以通过 $HTTP_COOKIE_VARS["user"] 或 $_COOKIE["user"] 来访问名为 "user" 的 cookie 的值。在发送 cookie 时,cookie 的值会自动进行 URL 编码。接收时会进行 URL 解码。如果你不需要这样,,可以使用 setrawcookie() 代替。
例,php设置和获取cookie
复制代码 代码如下:
setcookie('mycookie','value');
//函数原型:int setcookie(string name,string value,int expire,string path,string domain,int secure)
echo($mycookie);
echo($HTTP_COOKIE_VARS['mycookie']);
echo($_COOKIE['mycookie']);
删除Cookie
(1)调用只带有name参数的setcookie();
(2)使失效时间为time()或time-1;
复制代码 代码如下:
setcookie('mycookie');或setcookie('mycookie','');或setcookie("mycookie",false);
//setcookie('mycookie','',time()-3600);
echo($HTTP_COOKIE_VARS['mycookie']);
print_r($_COOKIE);
建议删除方法:
复制代码 代码如下:
setcookie('mycookie','',time()-3600);
PHP提供一个很好用的函数mktime()。
你只要按顺序传送给mktime()你希望表示的小时,分钟,秒数,月份,日期,及年份,
mktime()就会返回该日期自1970年1月1日的总秒数。
因此,如果需要模拟 Y2K 问题:
复制代码 代码如下:
$y2k = mktime(0,0,0,1,1,2000);
setcookie('name','value',$y2k);
setcookie('name', 'value', time+3600);
setcookie('name', 'value', $y2k, '~/myhome', '.domain.com');
获取COOKIE过期时间的办法
复制代码 代码如下:
$expire = time() + 86400; // 设置24小时的有效期
setcookie ("var_name", "var_value", $expire); // 设置一个名字为var_name的cookie,并制定了有效期
setcookie ("var_name_expire", $expire, $expire); // 再将过期时间设置进cookie以便你能够知道var_name的过期时间
注:
在发送 cookie 时,cookie 的值会自动进行 URL 编码。接收时会进行 URL 解码。
如果你不需要这样,可以使用 setrawcookie() 代替。
例,cookie来保存用户登录信息
1、数据库连接配置页面:connectvars.php
复制代码 代码如下:
//数据库的位置
define('DB_HOST', 'localhost');
//用户名
define('DB_USER', 'root');
//口令
define('DB_PASSWORD', '19900101');
//数据库名
define('DB_NAME','test') ;
?>
2、登录页面:logIn.php
复制代码 代码如下:
//插入连接数据库的相关信息
require_once 'connectvars.php';
$error_msg = "";
//判断用户是否已经设置cookie,如果未设置$_COOKIE['user_id']时,执行以下代码
if(!isset($_COOKIE['user_id'])){
if(isset($_POST['submit'])){//判断用户是否提交登录表单,如果是则执行如下代码
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$user_username = mysqli_real_escape_string($dbc,trim($_POST['username']));
$user_password = mysqli_real_escape_string($dbc,trim($_POST['password']));
if(!empty($user_username)&&!empty($user_password)){
//MySql中的SHA()函数用于对字符串进行单向加密
$query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND "."password = SHA('$user_password')";
//用用户名和密码进行查询
$data = mysqli_query($dbc,$query);
//若查到的记录正好为一条,则设置COOKIE,同时进行页面重定向
if(mysqli_num_rows($data)==1){
$row = mysqli_fetch_array($data);
setcookie('user_id',$row['user_id']);
setcookie('username',$row['username']);
$home_url = 'loged.php';
header('Location: '.$home_url);
}else{//若查到的记录不对,则设置错误信息
$error_msg = 'Sorry, you must enter a valid username and password to log in.';
}
}else{
$error_msg = 'Sorry, you must enter a valid username and password to log in.';
}
}
}else{//如果用户已经登录,则直接跳转到已经登录页面
$home_url = 'loged.php';
header('Location: '.$home_url);
}
?>
Msimatch - Log In
if(empty($_COOKIE['user_id'])){
echo '
'.$error_msg.'
';?>
}
?>
3、登入页面:loged.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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
