您的位置:

phpcookie存值,php中setcookie参数

本文目录一览:

php怎么用cookie存储多维数组

方法一:先用serialize序列化数组,再存入COOKIE

 ,读出来时用unserialize得到原来的数组

//存入

 

$arr

=

array(1,2,3);

$arr_str

=

serialize($arr);

 

 

setcookie("a",$arr_str);

 

//取出

 

$arr_str

=

$_COOKIE['a'];

 

$arr

=

unserialize($arr_str); 

方法二:设定多键值cookie,注意必须给键值

 

$arr

=

array(1,2,3);

 

setcookie("a[0]",

$arr[0]);

 

setcookie("a[1]",

$arr[1]);

 

setcookie("a[2]",

$arr[2]);

php 将cookie的值写入文件

head

/head

body

?php

header("content-type:text/html; charset=utf-8");

//在PHP中设置COOKIE

setcookie("phpCookie","我是PHP产生的Cookie", time()+3600);

?

script language="javascript"

//JavaScript中设定Cookie值

function setCookie(name,value)

{

var Days = 1/1440; //此 cookie 将被保存 30 天

var exp = new Date(); //new Date("December 31, 9998");

exp.setTime(exp.getTime() + Days*24*60*60*1000);

document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();

}

//JS取JS保存的cookie

function getCookie2(name)

{ var temp = name + "=";

var tempLen = temp.length;

var cookieLen = document.cookie.length;

var i = 0;

while (i cookieLen) {

var j = i + tempLen;

if (document.cookie.substring(i, j) == temp) {

return getCookieVal(j);

}

i = document.cookie.indexOf(" ", i) + 1;

if (i == 0) break;

}

return null;

}

//辅助

function getCookieVal (offset) {

var endstr = document.cookie.indexOf (";", offset);

if (endstr == -1) {

endstr = document.cookie.length;

}

return unescape(document.cookie.substring(offset, endstr));

}

//js取PHP保存的Cookie值

function getCookie(sName)

{

var aCookie = document.cookie.split("; ");

for (var i=0; i aCookie.length; i++){

var aCrumb = aCookie[i].split("=");

if (encodeURIComponent(sName) == aCrumb[0])

return decodeURIComponent(aCrumb[1]);

}

return null;

}

/script

script

//显示经过解码的PHP设置的COOKIE

alert(getCookie("phpCookie"));

//使用JavaScript设置COOKIE值

setCookie("jsCookie","我是JavaScript产生的Cookie");

alert(getCookie2("jsCookie"));

/script

?php

//显示JavaScript设置的COOKIE的

if (isset($_COOKIE["jsCookie"]))

echo urldecode($_COOKIE["jsCookie"]);

echo "br /";

echo $_COOKIE["phpCookie"];

?

/body/html

php如何将一个数组保存在cookie里面

先用serialize序列化数组,再存入COOKIE,读出来时用unserialize得到原来的数组

代码:

//存入

$arr = array(1,2,3);

$arr_str = serialize($arr);

setcookie("a",$arr_str);

//取出

$arr_str = $_COOKIE['a'];

$arr = unserialize($arr_str);

2. 设定多键值cookie,注意必须给键值

$arr = array(1,2,3);

setcookie("a[0]", $arr[0]);

setcookie("a[1]", $arr[1]);

setcookie("a[2]", $arr[2]); 结果:数组所有元素都存到了。

数组长度 :3

Array ( [0] =gt; 1 [1] =gt; 2 [2] =gt; 3 )