php cookie操作的简单示例

发布时间:2020-04-16编辑:脚本学堂
本文介绍下,在php中设置与打印cookie值、测试cookie用法的例子,有需要的朋友参考下。

1,php设置与打印cookie值,代码:

<?php
setcookie( "myvalue1", "myvalue2", time()+3600, "/", "www.jb200.com", 0 );
?>
<html>
<head>
<title>设置与打印cookie的值--www.jb200.com</title>
</head>
<body>
<?php
if ( isset( $myvalue1 ) )
    print "<p>Hello again, your value is $myvalue1</p>";
 else
    print "<p>Hello you. This may be your first visit</p>";
?>
</body>
</html>

2,创建一个测试cookie,用于体验php cookie的用法。

<?php
  $string_name = " testcookie";
  $string_value = "This is a test cookie";
  $expiry_info = time()+259200;
  $string_domain = "localhost.localdomain";

  setcookie($string_name, $string_value, $expiry_info, $string_domain);
?>

3,使用多值cookie

<?php
  if (!isset ($userdetails [0] ) ) {
     setcookie ("userdetails[0]", $username);
  }
  $userdetails[1]++;
  setcookie ("userdetails[1]", $userdetails[1]);
  echo ("Hello $userdetails[0], you've seen this page".$userdetails[1].($userdetails[1] == l?" time!": "times!"));
?>