例子 1. setcookie() send example<?php
$value = 'something from somewhere';setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
?>  
 
Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. If you don't want this, you can use setrawcookie() instead if you are using PHP 5. To see the contents of our test cookie in a script, simply use one of the following examples: <?php
// Print an individual cookie
echo $_COOKIE["TestCookie"];
echo $HTTP_COOKIE_VARS["TestCookie"];// Another way to debug/test is to view all cookies
print_r($_COOKIE);
?>  When deleting a cookie you should assure that the expiration date is in the past, to trigger the removal mechanism in your browser. Examples follow how to delete cookies sent in previous example: 例子 2. setcookie() delete example<?php
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
?>  
 
You may also set array cookies by using array notation in the cookie name. This has the effect of setting as many cookies as you have array elements, but when the cookie is received by your script, the values are all placed in an array with the cookie's name: 例子 3. setcookie() and arrays<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
        echo "$name : $value <br />\n";
    }
}
?>  which prints three : cookiethree
two : cookietwo
one : cookieone
 
 

解决方案 »

  1.   

    Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\duxinyu\桌面\new\new\login.php:2) in C:\Documents and Settings\duxinyu\桌面\new\new\login.php on line 16Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\duxinyu\桌面\new\new\login.php:2) in C:\Documents and Settings\duxinyu\桌面\new\new\login.php on line 17
    还出现这样的提示,这是什么原因呢?
      

  2.   

    setcookie之前有输出了看看是不是<?php 这里多了个空格什么的。