在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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.