Home Backend Development PHP Tutorial 老大,小弟有些迷惑的东西

老大,小弟有些迷惑的东西

Jun 13, 2016 pm 01:30 PM
lock search session time

请教各位老大,小弟有些迷惑的东西。
小弟的系统(一个cms)中有个搜索页面是这样的 以下代码是为了防止用户频繁的搜索
$lockfile = './time.lock.inc'; //time.lock.inc 内容为: 1337659312
$lasttime = file_get_contents($lockfile);

if(!empty($lasttime) && ($lasttime + $cfg_search_time) > time())
{
  ShowMsg('请勿频繁操作','-1');
  exit();
}
每次搜索完后会更新当前的时间到 time.lock.inc

这段代码其实是防止了所有的用户在指定$cfg_search_time内不能同时进行2次以上的操作,但是这样肯定不妥。
原因如下(不知是否妥当),请各位老大帮忙分析分析:
假如$cfg_search_time=1 同时有20人在并发搜索 理想情况中间不乱费一丝时间(服务器及网速消耗时间忽略不计)需要20秒

因为是这样 我想做下改进 在time.lock.inc存储2个信息 一个IP 一个时间戳
$dataiplist0=>array('14.453.22.55'=>1337659312,...);
?>
然后每次都获取一次IP 在每次搜索的时候针对指定IP来进行检测 同一IP1秒之内只能搜索一次,这样就可以并发搜索了,但是这里就有个问题 假如一个内网段的用户都是用同一个外网IP上网,这样就可能将整个内网都当成一个用户在处理了,不知道这些问题各位大哥遇到过没,怎么处理。

最后弱弱的问下,这样能有效的控制用户恶意骚扰吗,如果不能,各位大大是否能给个好的建议。。。




------解决方案--------------------
保存array到这个文件,
以session id为key,值为上次搜索的时间戳。
------解决方案--------------------
这样子,因为搜索的人应该不会很多,当然个别(动作)论坛除外
可以使用memcache 来保存数据

用ip+用户名+搜索时间的组合方式来判断


------解决方案--------------------
你一开始的锁定是针对所有用户的,所以用文件锁是正确的

后来你又想只锁定单个用户,那么用 session 就可以了
------解决方案--------------------
session控制即可, 如果要防黑客另当别论, session对它们毫无作用, 不提交cookie就是了.
------解决方案--------------------
新建一表,把搜索的用户放进去。判断是否有记录或者时间在一定范围内。就不让再查了

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 Article Tags

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)

How to set session timeout in SpringBoot Session How to set session timeout in SpringBoot Session May 15, 2023 pm 02:37 PM

How to set session timeout in SpringBoot Session

How to solve session failure How to solve session failure Oct 18, 2023 pm 05:19 PM

How to solve session failure

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to PHP Session cross-domain problem

What should I do if the php session disappears after refreshing? What should I do if the php session disappears after refreshing? Jan 18, 2023 pm 01:39 PM

What should I do if the php session disappears after refreshing?

What is the default expiration time of session php? What is the default expiration time of session php? Nov 01, 2022 am 09:14 AM

What is the default expiration time of session php?

Monotonic clock processing of time package Monotonic clock processing of time package Aug 04, 2023 pm 05:45 PM

Monotonic clock processing of time package

How to correctly read and write Session data in multiple files with PHP How to correctly read and write Session data in multiple files with PHP Mar 23, 2023 am 11:12 AM

How to correctly read and write Session data in multiple files with PHP

How to implement SMS login in Redis shared session application How to implement SMS login in Redis shared session application Jun 03, 2023 pm 03:11 PM

How to implement SMS login in Redis shared session application

See all articles