Home Backend Development PHP Tutorial PHP中使用cookie跟session

PHP中使用cookie跟session

Jun 13, 2016 pm 12:36 PM
cookie php quot rand session

PHP中使用cookie和session

cookie和session不管在java还是php中用的是比较多的,cookie可以看做是客户端技术,session则是服务端技术。像购物车还有网站自动登录都可以用cookie实现,session则比较偏向验证这一块,相比cookie安全性更高,因为session是存储在服务端的,不能随意删除或修改。下面来简单的分享下我的学习心得

1.cookie的使用

如果需要保存cookie可以直接在php页面直接使用setCookie函数来保存cookie使用方法如下

<?php setCookie("username","123456",time()&#43;120);
?>
Copy after login

第一个参数是cookie的键,第二个则是value,第三个表示该cookie什么时候过期单位是秒,time()表示当前时间。这句代码的意思是该cookie在2分钟会过期

更新cookie的方法也是一样。如果是火狐浏览器我们可以看到cookie如下


下面来删除cookie

setCookie($cookiename, '');或者 setCookie($cookiename, NULL);这两种方法都能删除cookie

至于要取得cookie就更简单了,使用$_COOKIE就能取得,批量操作cookie也可以通过这个预定义超全局数组

2.session的使用

(1)启用session

(2)把对象放入session中

 session_start();
  $_SESSION["password"]="123456";
Copy after login

两句话就可以把sessiona保存起来了,就像下面这样

要取出session也是使用$_SESSION取得,删除单个session可以使用unset($_SESSION["password"]),如果是删除全部可以使用session_destroy();


session的真正原理不是那么好理解,要理解的深入也很难,需要深入的话可以使用firebug查看http的请求和响应。当服务器创建好session之后,会给客户端浏览器返回一个

PHPSESID,这个ID就是会话唯一ID.当下次浏览器客户端要取session就会通过这个唯一ID去服务器端取出session信息。如果客户端把cookie禁用了,按照正常的代码,session就不能共享了。这里提供最简单的两种做法

第一种就是URL重写,先判断是否有PHPSESSID如果有则设置session_id(SID的值);

第二种就是修改php.ini里的session.use_trans_sid把值设为1


对于第一种PHP提供了一个常量叫SID可以直接拿来使用,因为这种情况比较极端平时基本上不会遇到。具体的代码就不写了。有需要可以给我写评论,我直接给发过去。

要注意的是php的session也可以存对象,要注意的就是使用前最好把这个对象用require_once引入就好使了


最后针对session的应用,我在网上找了一个特别简单的验证码,代码如下

<?php session_start();
    
    Header("Content-type: image/PNG");
    $im = imagecreate(44,18); 
    $back = ImageColorAllocate($im, 245,245,245); 
    imagefill($im,0,0,$back); 
    $vcodes = "";
    srand((double)microtime()*1000000);
    
    for($i=0;$i<4;$i&#43;&#43;){
    $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); 
    $authnum=rand(1,9);
    $vcodes.=$authnum;
    imagestring($im, 5, 2&#43;$i*10, 1, $authnum, $font);
    }
    $_SESSION['VCODE'] = $vcodes;

    for($i=0;$i<100;$i&#43;&#43;) 
    {
    $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
    imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); 
    }
    ImagePNG($im);
    ImageDestroy($im);
?>
Copy after login

具体怎么用就不用解释了吧得意

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles