限制用户从同一IP访问多个页面 [英] Restrict multiple page access by a user from the same IP

查看:276
本文介绍了限制用户从同一IP访问多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,它将限制特定用户对给定IP地址的多次访问。换句话说,给定用户只能从同一IP地址访问/查看页面一次。

I am coding a script, that will restrict multiple access for a particular user from a given IP address. In other words, a given user will only be able to access/view the page one time from the same IP address.

但是如何比较传入的IP来检测这种访问?

But how do I compare the incoming IP's to detect this kind of access?

我想跟踪最多60天的IP。

I want to Track IPs for 60 days max.

推荐答案

您需要将IP存储在某个数据库或平面文件中以进行比较。

You'll need to store IPs in a database or flat file somewhere for comparison.

假设您有一个MySQL表访问例如:

Let's imagine you have a MySQL table visits such as this:

`ip` varchar(255) NOT NULL,
`date_created` datetime NOT NULL,
`last_visit` datetime NOT NULL,
`visits` int(255) NOT NULL,
PRIMARY KEY (`ip`)

使用PHP,我们可以获取用户的IP,在数据库中创建或更新表记录,然后进行一些比较。

Using PHP we can get the user's IP, create or update a table record in the database, and then do some comparison.

<?php

// Get the user's IP
$ip = getenv('REMOTE_ADDR');

// Create a database record, or update if they're been here before
$dblink = mysql_connect( 'localhost', 'username', 'password' );
mysql_select_db( 'database', $dblink );
$rs = mysql_query( "INSERT INTO visits (ip, date_created, last_visit, visits) VALUES( '$ip', NOW(), NOW(), 1 ) ON DUPLICATE KEY UPDATE `last_visit` = NOW(), visits=visits+1 ", $dblink );

// Compare database record for last visit and first visit
$rs = mysql_query( "SELECT visits, DATEDIFF( last_visit, date_created ) as sincelast FROM visits WHERE `ip` = '$ip' ");
$row = mysql_fetch_assoc( $rs );

// If this is their first visit, do one thing, otherwise, do another.
if ( $row['sincelast'] > 59 || $row['visits'] < 2 ) {
    // They visited 60+ days ago, or this is their first visit
} else {
    // This is not their first visit
}

这篇关于限制用户从同一IP访问多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆