PHP 接收数据时会自动的地址解码

方文锋  2021-08-09 11:20:25  1450  首页学习PHP

php接收数据时会自动的地址解码,如GET,POST,cookie。

处理cookie的:


然后php写入cookie的时候如写入中文:

写入cookie的时候是进行了地址编码的

然后用js获取cookie的值

所以在前台使用js写入cookie的时候需要对值进行地址编码 encodeURIComponent() 。


自己简单的使用js操作cookie的 代码:

 function goods_car() {

}
goods_car.prototype = {
    add: function () {

    },
    del: function () {

    },
    update: function () {

    },
    select: function () {

    },
    /**
     * 设置cookie
     * @param c_key  键名
     * @param value  cookie的键值
     * @param time 设置cookie的有效时间
     * @param dir cookie的作用路径
     */
    setCookie: function (c_key, value, time, dir) {
        var t = new Date();
        var Day = time && typeof time === "number" ? time : 7;
        t.setTime(t.getTime() + Day * 24 * 60 * 60 * 1000);
        value = encodeURIComponent(value);
        document.cookie = dir ? c_key + "=" + value + ";expires=" + t.toGMTString() + ";path=" + dir : c_key + "=" + value + ";expires=" + t.toGMTString() + "path=/";
    },
    /**
     * 删除单个cookie
     * @param c_key
     * @param dir
     * @returns {string}
     */
    delCookie: function (c_key, dir) {
        dir = dir ? dir : "/";
        this.setCookie(c_key, "", 0, dir);
        this.setCookie(c_key, "", -2, dir);
        return this.getCookie(c_key) ? "删除cookie失败!" + this.getCookie(c_key) : "删除cookie成功";
    },
    /**
     * 获取单个cookie
     * @param c_key
     * @returns {string}
     */
    getCookie: function (c_key) {
        var arr = document.cookie.split("; ");
        var c_str = "";
        this.foreach(arr, function (key, value) {
            var a = value.split("=");
            if (a[0] === c_key) {
                c_str = a[1];
                return 0; //停止for循环
            }
        });
        return decodeURIComponent(c_str);
    },
    /**
     * 获取多个cookie值
     */
    getCookieAll: function () {
        var arr = document.cookie.split("; ");
        var data = {};
        this.foreach(arr, function (key, value) {
            var a = value.split("=");
            data[a[0]] = decodeURIComponent(a[1]);
        });
        return data;
    },
    foreach(obj, fn) {
        var args = arguments;
        if (this.is_array(obj)) {
            if (typeof fn === "function") {
                for (var i = 0, len = obj.length; i < len; i++) {
                    //console.log(i);
                    if (fn.call(obj, i, obj[i]) === 0) {
                        break;
                    }
                }
            }
        }
        if (this.is_object(obj)) {
            if (typeof fn === "function") {
                for (var attr in obj) {
                    if (fn.call(obj, attr, obj[attr]) === 0) {
                        break;
                    }
                }
            }
        }
    },
    is_array: function (argument) {
        return Object.prototype.toString.call(argument) === "[object Array]";
    },
    is_object: function (argument) {
        if (argument && (typeof argument === "object") && ("length" in argument)) {
            return Object.prototype.toString.call(argument) === "[object Object]";
        }
        return argument && (typeof argument === "object") && !("length" in argument) ? true : false;
    },
};
var gCar = new goods_car();