search
Homephp教程php手册防止伪造跨站请求的小招式

  伪造跨站请求介绍

  伪造跨站请求比较难以防范,而且危害巨大,攻击者可以通过这种方式恶作剧,发spam信息,删除数据等等。这种攻击常见的表现形式有:

  伪造链接,引诱用户点击,或是让用户在不知情的情况下访问

  伪造表单,引诱用户提交。表单可以是隐藏的,用图片或链接的形式伪装。

  比较常见而且也很廉价的防范手段是在所有可能涉及用户写操作的表单中加入一个随机且变换频繁的字符串,然后在处理表单的时候对这个字符串进行检查。这个随机字符串如果和当前用户身份相关联的话,那么攻击者伪造请求会比较麻烦。

  yahoo对付伪造跨站请求的办法是在表单里加入一个叫.crumb的随机串;而facebook也有类似的解决办法,它的表单里常常会有post_form_id和fb_dtsg。

  随机串代码实现

  咱们按照这个思路,山寨一个crumb的实现,代码如下:

 

  

  class Crumb {

  CONST SALT = "your-secret-salt";

  static $ttl = 7200;

  static public function challenge($data) {

  return hash_hmac('md5', $data, self::SALT);

  }

  static public function issueCrumb($uid, $action = -1) {

  $i = ceil(time() / self::$ttl);

  return substr(self::challenge($i . $action . $uid), -12, 10);

  }

  static public function verifyCrumb($uid, $crumb, $action = -1) {

  $i = ceil(time() / self::$ttl);

  if(substr(self::challenge($i . $action . $uid), -12, 10) == $crumb

  substr(self::challenge(($i - 1) . $action . $uid), -12, 10) == $crumb)

  return true;

  return false;

  }

  }

 

  代码中的$uid表示用户唯一标识,而$ttl表示这个随机串的有效时间。

  应用示例

  构造表单

  在表单中插入一个隐藏的随机串crumb

 

  处理表单 demo.php

  对crumb进行检查

 

  

  if(Crumb::verifyCrumb($uid, $_POST['crumb'])) {

  //按照正常流程处理表单

  } else {

  //crumb校验失败,错误提示流程

  }



Statement
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
番茄小说uid在什么地方查看番茄小说uid在什么地方查看Feb 28, 2024 pm 04:25 PM

在使用番茄小说软件时,登录自己的账号这样您可以享受到更多个性化的服务和便利。登录账号后,您还能查看到自己的UID(用户唯一标识符),这是一个非常重要的信息,有时候这个uid是非常重要的,那么在番茄小说中自己的UID究竟在哪里查看呢?想要了解的用户们就快来跟着本文一起详细了解查看步骤吧!番茄小说uid在什么地方查看答案:【番茄小说】-【我的】-【反馈与帮助】-【意见反馈】-【点击5下】。具体步骤:1、首先打开番茄小说软件,进入到首页中后我们需要点击右下角的【我的】;2、然后在我的页面中我们滑动到页

DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 BlackDJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 BlackSep 04, 2024 am 06:51 AM

DJI has not confirmed any plans to introduce a new action camera yet. Instead, it seems that GoPro will get ahead of its rival this year, having teased that it will introduce two new action cameras on September 4. For context, these are expected to a

一行命令找出 Linux 中所有真实用户一行命令找出 Linux 中所有真实用户Feb 18, 2024 pm 05:50 PM

哈喽大家好,我是咸鱼。接触过Linux的小伙伴们都知道在Linux(或者说类Unix)中,有三种类型的用户:超级用户(UID为0):即root用户,拥有最高权限。系统用户(UID为1~999):系统内建用户,用于运行系统服务和守护进程。普通用户(UID为1000~60000):为了让使用者能够使用Linux系统资源而建立的,用户新建的账号一般就是普通账号。那么现在问题来了,如何快速找出Linux中的真实用户(root用户和普通用户)?正式开始之前,我们先介绍一个工具——getent。getent

深入了解Linux用户UID和GID的查看方式深入了解Linux用户UID和GID的查看方式Mar 20, 2024 pm 12:06 PM

用户UID和GID是Linux系统中用来标识用户和组的数字标识符。在使用Linux系统时,我们经常需要查看用户的UID和GID,以便管理用户和文件的权限。本文将深入介绍Linux系统中查看用户UID和GID的多种方式,并附上具体的代码示例。一、通过命令查看用户UID和GID查看当前用户的UID和GID:id运行以上命令后,系统会显示当前用户的UID、GID以

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

UID是账号吗UID是账号吗Mar 02, 2023 pm 12:01 PM

UID不是账号,而是用户身份证明即User Identification的缩写,是网络平台注册时系统自动生成的数值;UID用户在注册网络平台后,系统会自动地给你一个UID的数值,意思就是给这名用户编个号。

Linux命令:查看用户UID和GID的技巧Linux命令:查看用户UID和GID的技巧Mar 20, 2024 pm 12:33 PM

Linux操作系统是一种广泛使用的开源操作系统,它为用户提供了丰富的命令行工具,让用户可以更轻松地管理系统。在Linux系统中,每个用户都有一个唯一的用户标识符(UserID,UID)和一个组标识符(GroupID,GID)。了解用户的UID和GID对于系统管理和文件权限管理非常重要,本文将介绍几种查看用户UID和GID的技巧,以便更好地管理Linux系

New DJI Osmo action camera spotted before probable summer 2024 launch to rival recent GoPro and Insta360 releasesNew DJI Osmo action camera spotted before probable summer 2024 launch to rival recent GoPro and Insta360 releasesJul 01, 2024 am 09:49 AM

Almost a year has passed since DJI released the Osmo Action 4 (curr. $299 on Amazon). Since then, the company has focused on its other divisions, including new RS camera gimbals. On top of that, it has introduced various drones as well like the Avata

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)