php网页延迟跳转至指定的网址

帮一个朋友写的,顺便记录下

目的/效果

当一个ip(用户)第一次访问网站时延时跳转至a.com, 如果这个ip再次访问这个页面,延时跳转b.com。

a.com,b.com和用户访问次数写入到数据库里,从数据库里获取第几次访问 来返回应该跳转的URL

最后,这个ip跳转过数据库最后一条url后,就让他访问另外设置的一个默认url

简单的三个文件

url.php

<?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
 
// 数据库信息
$server = "localhost";
$username = "name";
$password = "pass";
$db = "ipurl";

// 创建连接
$conn = mysqli_connect($server, $username, $password, $db);
// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// 获取用户IP
$ip=FALSE;
// 客户端IP 或 NONE 
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
    $ip = $_SERVER["HTTP_CLIENT_IP"];
}
// 多重代理服务器下的客户端真实IP地址(可能伪造),如果没有使用代理,此字段为空
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
    if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
    for ($i = 0; $i < count($ips); $i++) {
        if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
            $ip = $ips[$i];
            break;
        }
    }
}
// 客户端IP 或 (最后一个)代理服务器 IP 
$ip = $ip ? $ip : $_SERVER['REMOTE_ADDR'];

// 查询该ip对应的url
$sqlsel="SELECT url , url_no FROM t_url WHERE url_no = (SELECT url_no FROM t_ip WHERE ip = '".$ip."');";
$result1=mysqli_query($conn,$sqlsel);
$row=mysqli_fetch_array($result1);

if($result1==0 || $row == FALSE){ // 该ip没有对应的url,让他跳转第一个url
    $row1=mysqli_fetch_array(mysqli_query($conn,"SELECT url FROM t_url WHERE url_no = 1;"));   
    $sqlins="REPLACE INTO t_ip(ip,url_no) VALUES('".$ip."',2);";
    mysqli_query($conn,$sqlins);
    echo $row1[0];
}else{ // 该ip已存在,把他应跳转的url给他。并num+1
    $result2=mysqli_query($conn,"SELECT url_no FROM t_url order by url_no desc limit 1");
    $row2=mysqli_fetch_array($result2);

    if($row[1] ==$row2[0] ){ //已经访问到最后一条了
        echo "http://默认.com";
    }else{//还没访问到最后一条
        $sqlupd="UPDATE t_ip SET url_no=url_no+1 WHERE ip = '".$ip."';";
        mysqli_query($conn,$sqlupd);
        echo $row[0];
    }
}

mysqli_close($conn);
?>

index.html

<!doctype html>
<html>

<head>
    <script type="application/javascript">
        (function() {
            var xmlhttp;
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var xmlText = xmlhttp.responseText;
                    //3秒后跳转
                    setTimeout(() => {
                        location.href = xmlText;
                    }, 3000);
                }
            }
            xmlhttp.open("GET", "url.php", true);
            xmlhttp.send();
        })();
    </script>

</head>

<body>
    index
</body>

</html>

ipurl.sql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_ip
-- ----------------------------
DROP TABLE IF EXISTS `t_ip`;
CREATE TABLE `t_ip`  (
  `ip` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `url_no` int(8) NOT NULL,
  PRIMARY KEY (`ip`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for t_url
-- ----------------------------
DROP TABLE IF EXISTS `t_url`;
CREATE TABLE `t_url`  (
  `url_no` int(8) NOT NULL,
  `url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`url_no`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_url
-- ----------------------------
INSERT INTO `t_url` VALUES (1, 'http://baidu.com');
INSERT INTO `t_url` VALUES (2, 'http://so.com');
INSERT INTO `t_url` VALUES (3, 'http://soso.com');

SET FOREIGN_KEY_CHECKS = 1;

发表评论

邮箱地址不会被公开。 必填项已用*标注