С½a
ͨ¹ýÒÔÉÏÈý²½£¬ÎÒÃÇʹÓÃPHP¿iËÙµØÔÚµ¥Ò»Ò³ÃaeÉÏʵÏÖÁ˱iµ¥ÏÔʾºÍ±iµ¥´¦ÀiµÄ¹¦ÄÜ¡£
Ée¶¨Cookies
PHPÌa¹(C)ÁËÇ¿´oµÄÉe¶¨ºÍ¶ÁÈ¡CookiesµÄ¹¦ÄÜ¡£ÔÚ´Ë£¬ÎÒÃDz»Ïe¶ÔCookies½øÐйý¶aµÄ½eÉÜ£¬µ«ÊÇÓû§Ó¦µ±ÈÏʶµ½ÔÚÉe¼ÆWEBÓ¦ÓõĹý³ÌÖÐCookies¿ÉÄÜÆðµ½µÄÖØÒª×÷Óá£
Óû§¿ÉÒÔʹÓÃPHPÌa¹(C)µÄsetcookie()º¯Êý´´½¨»oÐÞ¸ÄCookie¡£setcookie()º¯Êý×ܹ²°uÀ¨6¸o²ÎÊý£¬Äܹ»¶Ôcookie½øÐо«È*µÄ¿ØÖÆ¡£
ʹÓÃsetcookie()º¯ÊýÉe¶¨cookieµÄ×i¼oµ¥µÄ*½Ê½Îª£º
setcookie(¡®name', ¡®PETER');
ÕaÑu£¬ÔÚÓû§*ÃÎÊÕu¸oÕ¾µaÒ³ÃaeµÄÆÚ¼a£¬PHP½«»a×Ô¶¯´´½¨Ò»¸oÃuΪ$nameµÄ±aÁ¿£¬²¢½«ÖµPETER¸³Óe¸Ã±aÁ¿¡£ÎÒÃÇ°ÑÕaÖÖCookie³Æ×o»a»°Cookie£¬¼´Æa×÷ÓÃÓoΪÓû§µÄÕu¸o»a»°¹ý³Ì¡£
Èç¹uÎÒÃÇÏ£ÍuCookieÖµÔÚÃÎÊÓû§Àe¿ªÕ¾µaºoÈÔÈ»ÓÐЧµÄ»°£¬¿ÉÒÔʹÓÃsetcookie()º¯ÊýµÄÏaÓ¦²ÎÊý£¬Ée¶¨CookieµÄÓÐЧÆÚ¡£ÕaÀi£¬ÎÒÃÇÐeÒª¶ÔPHPµÄʱ¼aÉe¶¨½øÐÐÒ»ÏÂ˵Ã÷¡£PHPÊÇÒ»ÖÖ»uÓÚUnix¿ª¢³oµÄ¼¼Êo£¬Óû§ÐeÒªÓÃ×Ô1970Äe1ÔÂ1ÈÕÖ®ºoµÄÃeÊý±iʾµ±Ç°Ê±¼a¡£¶ÔÓÚûÓÐÈκÎUnixϵͳ±a³Ì¾ÑeµÄ¹a´oÆÕͨÓû§À´ËµÕaeÊÇÓеaÃþ²»×ÅÍÄÔ¡£²»¹ý£¬´o¼Ò´o¿É²»±Øµ£ÐÄ£¬ÒoΪPHPΪÎÒÃÇÌa¹(C)ÁËÒ»¸odz£¼o±aµÄ½a¾o½Ê½£¬¼´mktime()º¯Êý¡£Óû§¿ÉÒÔÔÚmktime()º¯ÊýÖа´ÕÕСʱ¡¢Ö¡¢Ãe¡¢Ô¡¢ÈÕ¡¢ÄeµÄ˳ÐoÊaÈeÓû§Ï£Íu±iʾµÄʱ¼a£¬mktime()º¯Êý½«»aµ»Ø¸Ãʱ¼a¾aÀe1970Äe1ÔÂ1ÈÕµÄÃeÊý¡£¾ÙÀýÀ´Ëµ£¬Èç¹uÎÒÃÇÏ£ÍuÉe¶¨Ò»¸oÓÐЧÆÚ³ÖÐøµ½2000ÄeµÄCookieµÄ»°£¬¿ÉÒÔʹÓÃÈçϽʽ£º
< £¿php
$y2k = mktime(0,0,0,1,1,2000);
setcookie(¡®name', ¡®PETER', $y2k);
£¿ >
Èç¹uÓû§Ï£Íu¸uÐÂÒѾ´aeÔÚµÄcookie£¬¿ÉÒÔ²ÉÓüoµ¥µÄÖ±½Ó¸²¸ÇÔÖµµÄ½¨¡£ÀýÈ磬¼´Ê¹ÎÒÃÇÒѾ°´ÕÕÉÏÊo´uÂe¶Ôcookie½øÐÐÁËÉe¶¨£¬ÈÔ¿ÉÒÔ¶ÔÆa×oÒÔϸĶ¯£º
< ?php
$y2k = mktime(0,0,0,1,1,2000);
setcookie(¡®name', ¡®JEFF', $y2k);
? >
ÕaÀiÐeÒªÌaÐÑÓû§×¢ÒaµÄÒ»µaÊÇ£¬ËaÈ»ÎÒÃǶÔcookieÖµ½øÐÐÁËÐ޸ģ¬µ«ÊÇÔÚûÓмÓÔؾ¹ýÐ޸ĵÄÒ³Ãae֮ǰ£¬PHPÖÐ$name±aÁ¿µÄÖµÈÔÈ»²»»a¸Ä±a¡£Èç¹uÓû§Ï£ÍuÔÚ¸u¸ÄcookieÖµµÄͬʱ¸u¸Ä ÏaÓ¦PHP±aÁ¿µÄÖµ£¬¿ÉÒÔ²ÉÓÃÒÔÏÂ*½Ê½£º
< £¿php
$name = ¡®JEFF';
$y2k = mktime(0,0,0,1,1,2000);
setcookie(¡®name', $name, $y2k);
£¿ > ÔÚÓÐЧÆÚ²ÎÊýÖ®ºo£¬setcookie()º¯ÊýÌa¹(C)ÁËÉeÖÿÉÒÔ¶ÁÈ¡cookieÖµµÄÒ³ÃaeµÄÂ*¾¶ºÍÓoµÄ²ÎÊý¡£³oÓÚ°²È«ÒoËصĿ¼ÂÇ£¬ÔÚĬÈÏÇe¿oÏ£¬Ö»ÓÐÓeÉe¶¨cookieµÄÒ³Ãae´¦ÓÚͬһĿ¼»oÏÂÊo×ÓĿ¼µÄÒ³Ãae²ÅÄܹ»¶ÁÈ¡ÏaÓ¦cookieÖµ¡£µ«ÊÇ£¬¸u¾ÝÐeÒª£¬ÎÒÃÇÒ²¿ÉÒÔ¶ÔÕaÒ»Ée¶¨½øÐÐÐ޸ġ£ÀýÈ磺
< £¿php
setcookie(¡®name', ¡®jeff', $y2k, ¡®~/myhome', ¡®.domain.com');
£¿ >
ͨ¹ýÉÏÊo´uÂe£¬ÎÒÃÇÉe¶¨²ÊÇλÓÚ~/myhomeĿ¼Ï£¬ÊoÓÚ.domain.comÓoµÄÒ³Ãae¶¼Äܹ»¶ÁÈ¡¸ÃcookieÖµ¡£
setcookie()º¯ÊýµÄ×iºoÒ»¸o²ÎÊý½ÏÉÙ±»Ê¹Ó᣸òÎÊý¿ÉÒÔÖ¸¶¨Ö»ÏoÔËÐа²È«Á¬½ÓÐÒe£¬ÈçSSL£¬µÄWEBþÎñÆ÷µ»ØcookieÖµ¡£Èç¹uÓû§Ï£ÍuÆo¶¯¸Ã²ÎÊý¹¦ÄÜ£¬Ö»ÐeÒª½«ÆaÖµÉeΪ1¼´¿É¡£
ʹÓÃPHPɾ³ýcookieҲdz£¼o±a£¬Óû§Ö»ÐeÒªÔÚsetcookie()º¯ÊýÖÐÊaÈeÏ£Íuɾ³ýµÄcookieµÄÃu³Æ£¬PHP¾Í»a×Ô¯Íe³ÉËuÓеÄɾ³ý¹¤×÷¡£ÀýÈç£?
< ?php setcookie(¡®name'); ? >
×iºo£¬¹ØÓÚcookie»¹ÒªËµÃ÷Ò»µa¡£¿¼Âǵ½HTTPÐÒeÖÐcookieµÄÔË×÷½Ê½£¬Óû§Ò»¶¨Òª×¢ÒaÓ¦µ±ÔÚÏÔʾÈκÎÎÄ×Ö֮ǰ¢ËÍËuÓеÄcookieÉeÖá£Èç¹uÓû§ÔÚ±aд´uÂeʱÏÈÉe¶¨ÏÔʾµÄÎÄ×ÖÈ»ºoÔÙ¢ËÍcookieµÄ»°£¬PHP¾Í»aµ¯³o±¨´iÐÅÏ¢£¬ÎÞ¨Íe³ÉcookieµÄÉeÖ᣾ÙÀýÀ´Ëµ£º
< ?php
setcookie(¡®name', ¡®jeff');
echo ¡°Hello Everyone!¡±;
? >
ÕaÖÖcookieÉeÖõĽʽÊÇÕýȵġ£µ«ÊÇÈç¹u²ÉÓÃÈçÏÂ*½Ê½£º
< ?php
echo ¡°Hello Everyone!¡±;
setcookie(¡®name', ¡®jeff');
? >
Ôo»a³oÏÖ±¨´iÐÅÏ¢£¬ÎÞ*¨Íe³ÉcookieµÄÉeÖᣠÈÕÆÚºÍʱ
PHPÌa¹(C)Á˶aÖÖ¼o±aµÄ¹¦ÄÜÒÔ*½±aÓû§ÏÔʾºÍ¿ØÖÆÈÕÆÚºÍʱ¼a¡£
Èç¹uÓû§Ï£ÍuÒÔijÖÖÐÎʽÏÔʾijһÈÕÆÚ»oʱ¼aµÄ»°£¬¿ÉÒÔʹÓÃPHPÌa¹(C)µÄdate()º¯Êý¡£date().º¯Êý°uÀ¨Á½¸o²ÎÊý£¬Ö±ðÓÃÓÚÉe¶¨ÈÕÆÚµÄÏÔʾ¸ñʽÒÔ¼°´u±iÏÔʾÈÕÆÚµÄʱ¼a´Á£¬ÆaÖУ¬Ê±¼a´Á±ØÐe²ÉÓþaÀe1970Äe1ÔÂ1ÈÕµÄÃeÊý½Ê½±iʾ¡£ÈçͬCÓiÑÔÖеÄstrftime()º¯Êý»oPerlÖеÄPOSIX::strftime()º¯Êý£¬PHPÖеÄdate()º¯Êý¾ßÓÐdz£¶aµÄ¸ñʽÉeÖÃÑ¡Ïi£¬ÕaÀi¾Í²»Ò»Ò»ËµÃ÷ÁË¡£¾ÙÀýÀ´Ëµ£¬date()º¯ÊýµÄʹÓýʽÈçÏ£º
< ?php
$birthday_stamp = mktime(10,10,0,10,20,1975);
$birthday_formatted = date(¡®F d, Y - g:i a',$birthday_stamp);
echo ¡°Peter was born on $birthday_formatted.¡±
? >
ÏÔʾ½a¹uΪ£ºPeter was born on October 10, 1975--10:10 p.m.
×ܽa
PHPÊÇÒ»ÖÖ¿iËÙ´´½¨¶¯Ì¬WEBÕ¾µaµÄÇ¿´o¹¤¾ß£¬ÆaÊiϤµÄÓi¨ç¸ñºÍÔ´´uÂe¿ªÅµÄÌØÐÔÄܹ»Ê¹Ê¹ÓÃÕßÔÚ×i¶ÌµÄʱ¼aÄÚÁ˽aºÍÕÆÎÕÆa¹¦Äܲ¢½ø¶ø¢»Ó³oÆa¾Þ´oDZÁ¦¡£Ï£Íu±¾ÎÄÄܹ»¶Ô¹a´o¶ÁÕßÓÐËuÆo*¢¡£×£´o¼ÒÔçÈÕ³ÉΪPHP¸ßÊÖ¡£