PHPÈëÃÅËÙ³É(3)

5年以前  |  阅读数:347 次  |  编程语言:PHP 

С½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¸ßÊÖ¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例