JS对cookies、localStorage、sessionStorage三种数据操作例子

JS对cookies、localStorage、sessionStorage三种数据操作例子;

<script type="application/x-javascript">
    
    function c(str) {
        console.log(str);
    }

    var doc = document;

    var button = document.getElementById("button");

    button.addEventListener("click",function(){
        test1();
        test2();
        test3();


    },false)

    function test1(){

        cookieUtils.set("name","a001");
        cookieUtils.set("pwd","123456");
        cookieUtils.set("age","23");

        c(cookieUtils.get("pwd"));
       // c(cookieUtils.del("pwd"));

        c(doc.cookie);
    }

    function test2(){
        sessionStorageUtils.set("name","a001");
        sessionStorageUtils.set("pwd","123456");
        sessionStorageUtils.set("age","23");

        c(sessionStorageUtils.get("name"))
        c(sessionStorageUtils.get("pwd"))
        c(sessionStorageUtils.get("age"))

        //c(sessionStorageUtils.del("age"))

        c(sessionStorageUtils.get("age"))
    }

    function test3(){
        localStorageUtils.set("name","a001");
        localStorageUtils.set("pwd","123456");
        localStorageUtils.set("age","23");

        c(localStorageUtils.get("name"))
        c(localStorageUtils.get("pwd"))
        c(localStorageUtils.get("age"))

        //c(localStorageUtils.del("age"))

        c(localStorageUtils.get("age"))
    }


    var cookieUtils = {
        set:function(name,value,expires,path,domain,secure){
            if(name.length <= 0){
                alert("至少需要指定一个名字");
                return false;
            }

            var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);

            if(expires instanceof Date){
                cookieText += "; expires=" + expires.toUTCString();
            }
            if(path){
                cookieText += "; path="+path;
            }
            if(domain){
                cookieText +="; domain="+domain;
            }
            if(secure){
                cookieText +="; "+secure;
            }

            document.cookie = cookieText;
            return true;

        },
        get:function(name){
            var cookieText = encodeURIComponent(name) + "=";
            var cookieValue = "";

            var start = document.cookie.indexOf(cookieText);

            if(start > -1){
                var end = document.cookie.indexOf(";",start);
                if(end == -1){
                    end = document.cookie.length;
                }

                cookieValue = encodeURIComponent(document.cookie.substring(start + cookieText.length,end));

            }
            return cookieValue;

        },
        del:function(name){
            this.set(name,"",new Date(0));
        }

    }

    var sessionStorageUtils = {
        set:function(name,value){
            if(name.length <= 0){
                alert("至少需要指定一个名字");
                return false;
            }

            sessionStorage.setItem(name,value);

            return true;

        },
        get:function(name){
            return sessionStorage.getItem(name);
        },
        del:function(name){
            sessionStorage.removeItem(name);
            return true;
        }
    }

    var localStorageUtils = {
        set:function(name,value){
            if(name.length <= 0){
                alert("至少需要指定一个名字");
                return false;
            }

            localStorage.setItem(name,value);

            return true;
        },
        get:function(name){
            return localStorage.getItem(name);
        },
        del:function(name){
            localStorage.removeItem(name);
            return true;
        }
    }


</script>

 

 

wow.js 使用说明

1.下载文件

animate.css 下载地址: http://daneden.github.io/animate.css/   https://github.com/matthieua/WOW/tree/master/css/libs
wow.js 下载地址:  https://github.com/matthieua/WOW

2.引入文件

<link rel="stylesheet" href="animate.css">
<script src="wow.js"></script>
<script>
   new WOW().init();//wow是大写哦
</script>

3.观察你需要的效果
http://daneden.github.io/animate.css/

4.在需要动画的标签上添加,wow 和 你的动画元素,如下代码:

<div class="row">
    <div class="wow rollIn bg-blue animated" >22</div>//rollIn 动画名称
    <div class="wow bounceInDown bg-red animated" data-wow-iteration="1" data-wow-duration="2.15s">22</div>
    <div class="wow slideInLeft bg-red animated" data-wow-iteration="1" data-wow-duration="2.15s">22</div>
//data-wow-iteration重复次数    data-wow-duration动画过程时间

 

实例下载:

http://pan.baidu.com/s/1qYyA7E0

把数值格式化成大写人民币的PHP跟js代码

JS代码:

function c(str) {
console.log(str);
}

function format_price($money){
var cny = parseFloat($money).toFixed(2);

var _num = [“0″,”壹”,”贰”,”叁”,”肆”,”伍”,”陆”,”柒”,”捌”,”玖”];
var _unit = [“圆”,”整”,”角”,”分”];
var _unit2 = [“”,”拾”,”佰”,”仟”];
var _unit3 = [“”,”万”,”亿”,”万”,”亿”];

最近服务器被无耻的人一致攻击了只能在Nginx服务器上屏蔽一些IP

估计全球的IP都在攻击我网站的xmlrpc.php,于是把这个文件屏蔽,但是还是遭到恶意攻击,实在无聊,只能屏蔽掉这些IP地址;

1.查找要屏蔽的ip;

1
awk ‘{print $1}’  /www/log/nginx/log/lanxinbase.log |sort |uniq -c |sort -n

lanxinbase.log 为蓝芯科技网站的日志文件;

 

会到如下结果,前面是ip的访问次数,后面是ip,很明显我们需要把访问次数多的ip并且不是蜘蛛的ip屏蔽掉,本例当中我们屏蔽掉:

1
2
3
4
5
6
7
1 163.172.66.70
1 164.132.161.5
1 203.208.60.203
1 220.181.108.117
1 68.180.228.100
11 118.192.151.214
195 27.10.12.69
395 191.96.249.53

2.在nginx的安装目录下面,新建屏蔽ip文件,命名为blockip.conf,以后新增加屏蔽ip只需编辑这个文件即可。 加入如下内容

1
deny 191.96.249.53;

3.在nginx的配置文件nginx.conf中加入如下配置,可以放到http, server, location, limit_except语句块,需要注意相对路径,本例当中nginx.conf,blocksip.conf在同一个目录中。

1
include blockip.conf;

4.重启一下nginx的服务:service nginx restart 就可以生效了。

 

高级用法:

屏蔽ip的配置文件既可以屏蔽单个ip,也可以屏蔽ip段,或者只允许某个ip或者某个ip段访问。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 屏蔽单个ip访问
deny IP;
# 允许单个ip访问
allow IP;
# 屏蔽所有ip访问
deny all;
# 允许所有ip访问
allow all;
#屏蔽整个段即从123.0.0.1到123.255.255.254访问的命令
deny 123.0.0.0/8
#屏蔽IP段即从123.45.0.1到123.45.255.254访问的命令
deny 124.45.0.0/16
#屏蔽IP段即从123.45.6.1到123.45.6.254访问的命令
deny 123.45.6.0/24

如果你想实现这样的应用,除了几个IP外,其他全部拒绝,
那需要你在blockip.conf中这样写

1
2
3
allow 1.1.1.1;
allow 1.1.1.2;
deny all;

单独网站屏蔽IP的方法,把include blocksip.conf; 放到网址对应的在server{}语句块,
所有网站屏蔽IP的方法,把include blocksip.conf; 放到http {}语句块。

12
 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |