通过PHP+Json的方式做网站访问记录功能

在同一目录下新建文件function.php和detail.json
<?php
$data = json_decode(file_get_contents("detail.json"),true);//打开json文件,并将内容转换为php数组形式
//如果收到get信息,则计数加一
if($_GET["plus"]=="1"){
    $data["count"]=$data["count"]+1;
    $json_strings = json_encode($data);
    file_put_contents("detail.json",$json_strings);//写入
    echo $data["count"];//输出计数
}else{
    echo $data["count"];//输出计数
}
?>

 

 
在html中配合ajax如下(需要jquery库)
 
$.ajax({
            type: 'get', 
            url: 'function.php',
            data: {  },
            dataType: 'text',
            success: function(res){
                $("#count").text(res);//在id为count的元素中存放信息
            }     
        });

        function count_plus(){
            $.ajax({
                type: 'get', 
                url: 'function.php?plus=1',
                data: {  },
                dataType: 'text',
                success: function(res){
                    $("#count").text(res);//在id为count的元素中存放信息
                }     
            });
        }

 

posted on 2019-11-01 16:35  Jieck  阅读(240)  评论(0编辑  收藏  举报

导航