代码如下local secret = "123456"local _M = {}local function make_cookie(host, ip, url) local hash = ngx.md5(host .. ip .. secret .. url) return hashendlocal function check_cookie(host, ip, url) local cookie = ngx.var["cookie_uid"] if not cookie then ngx.log(ngx.ERR, "cookies not found") return false end if cookie ~= make_cookie(host, ip, url) then ngx.log(ngx.ERR, "cookies invalid") return false endendfunction _M.check() local ip = ngx.var.remote_addr local host = ngx.var.http_host local url = ngx.var.request_uri local cookie = make_cookie(host, ip, url) if not check_cookie(host, ip, url) then ngx.header["Set-Cookie"] = { "uid=" .. cookie } ngx.redirect(ngx.var.request_uri) endendreturn _M --
local secret = "123456"local _M = {}local function make_cookie(host, ip, url) local hash = ngx.md5(host .. ip .. secret .. url) return hashendlocal function check_cookie(host, ip, url) local cookie = ngx.var["cookie_uid"] if not cookie then ngx.log(ngx.ERR, "cookies not found") return false end if cookie ~= make_cookie(host, ip, url) then ngx.log(ngx.ERR, "cookies invalid") return false endendfunction _M.check() local ip = ngx.var.remote_addr local host = ngx.var.http_host local url = ngx.var.request_uri local cookie = make_cookie(host, ip, url) if not check_cookie(host, ip, url) then ngx.header["Set-Cookie"] = { "uid=" .. cookie } ngx.redirect(ngx.var.request_uri) endendreturn _M