PHP鍙橀噺涓潵鑷狿HP涔嬪鐨勫彉閲

鏉ヨ嚜 PHP 涔嬪鐨勫彉閲忥紝HTML 琛ㄥ崟锛圙ET 鍜 POST锛夛紝褰撲竴涓〃鍗曚綋浜ょ粰 PHP 鑴氭湰鏃讹紝琛ㄥ崟涓殑淇℃伅浼氳嚜鍔ㄥ湪鑴氭湰涓彲鐢ㄣ傛湁寰堝鏂规硶璁块棶姝や俊鎭

HTML 琛ㄥ崟锛圙ET 鍜 POST锛
褰撲竴涓〃鍗曚綋浜ょ粰 PHP 鑴氭湰鏃讹紝琛ㄥ崟涓殑淇℃伅浼氳嚜鍔ㄥ湪鑴氭湰涓彲鐢ㄣ傛湁寰堝鏂规硶璁块棶姝や俊鎭紝渚嬪锛


Example #1 涓涓畝鍗曠殑 HTML 琛ㄥ崟

<form action="foo.php" method="POST">
    Name:  <input type="text" name="username"><br />
    Email: <input type="text" name="email"><br />
    <input type="submit" name="submit" value="Submit me!" />
</form>
鏍规嵁鐗瑰畾鐨勮缃拰涓汉鐨勫枩濂斤紝鏈夊緢澶氱鏂规硶璁块棶 HTML 琛ㄥ崟涓殑鏁版嵁銆備緥濡傦細


Example #2 浠庝竴涓畝鍗曠殑 POST HTML 琛ㄥ崟璁块棶鏁版嵁

<?php
// 鑷 PHP 4.1.0 璧峰彲鐢
   echo $_POST['username'];
   echo $_REQUEST['username'];
   
   import_request_variables('p', 'p_');
   echo $p_username;
// PHP 6浠ュ悗灏嗘棤鏁堛傝嚜 PHP 5.0.0 璧凤紝杩欎簺杈冮暱鐨勯瀹氫箟鍙橀噺
// 鍙敤 register_long_arrays 鎸囦护鍏抽棴銆

   echo $HTTP_POST_VARS['username'];

// 濡傛灉 PHP 鎸囦护 register_globals = on 鏃跺彲鐢ㄣ備笉杩囪嚜
// PHP 4.2.0 璧烽粯璁ゅ间负 register_globals = off銆
// 涓嶆彁鍊′娇鐢/渚濊禆姝ょ鏂规硶銆

   echo $username;
?>
浣跨敤 GET 琛ㄥ崟涔熺被浼硷紝鍙笉杩囪鐢ㄩ傚綋鐨 GET 棰勫畾涔夊彉閲忋侴ET 涔熼傜敤浜 QUERY_STRING锛圲RL 涓湪鈥?鈥濅箣鍚庣殑淇℃伅锛夈傚洜姝わ紝涓句緥璇达紝http://www.example.com/test.php?id=3 鍖呭惈鏈夊彲鐢 $_GET['id'] 璁块棶鐨 GET 鏁版嵁銆傚弬瑙 $_REQUEST 鍜 import_request_variables()銆

Note:

瓒呭叏灞鏁扮粍鍜 $_POST 浠ュ強 $_GET 涓鏍凤紝鑷 PHP 4.1.0 璧峰彲鐢ㄣ

濡備笂鎵绀猴紝鍦 PHP 4.2.0 涔嬪墠 register_globals 鐨勯粯璁ゅ兼槸 on銆侾HP 绀惧尯榧撳姳澶у涓嶈渚濊禆姝ゆ寚浠わ紝寤鸿鍦ㄧ紪鐮佹椂鍋囧畾鍏朵负 off銆

Note:

magic_quotes_gpc 閰嶇疆鎸囦护褰卞搷鍒 Get锛孭ost 鍜 Cookie 鐨勫笺傚鏋滄墦寮锛屽 (It's "PHP!") 浼氳嚜鍔ㄨ浆鎹㈡垚 (It's "PHP!")銆傛暟鎹簱鐨勬彃鍏ュ氨闇瑕佽浆涔夈傚弬瑙 addslashes()锛宻tripslashes() 鍜 magic_quotes_sybase銆

PHP 涔熸噦寰楄〃鍗曞彉閲忎笂涓嬫枃涓殑鏁扮粍锛堝弬瑙佺浉鍏冲父瑙侀棶棰橈級銆備緥濡傚彲浠ュ皢鐩稿叧鐨勫彉閲忕紪鎴愮粍锛屾垨鑰呯敤姝ょ壒鎬т粠澶氶夎緭鍏ユ涓彇寰楀笺備緥濡傦紝灏嗕竴涓〃鍗 POST 缁欒嚜宸卞苟鍦ㄦ彁浜ゆ椂鏄剧ず鏁版嵁锛


Example #3 鏇村鏉傜殑琛ㄥ崟鍙橀噺

<?php
if (isset($_POST['action']) && $_POST['action'] == 'submitted') {
    echo '<pre>';

    print_r($_POST);
    echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';

    echo '</pre>';
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Name:  <input type="text" name="personal[name]"><br />
    Email: <input type="text" name="personal[email]"><br />
    Beer: <br>
    <select multiple name="beer[]">
        <option value="warthog">Warthog</option>
        <option value="guinness">Guinness</option>
        <option value="stuttgarter">Stuttgarter Schwabenbr</option>
    </select><br />
    <input type="hidden" name="action" value="submitted" />
    <input type="submit" name="submit" value="submit me!" />
</form>
<?php
}
?>
IMAGE SUBMIT 鍙橀噺鍚
褰撴彁浜よ〃鍗曟椂锛屽彲浠ョ敤涓骞呭浘鍍忎唬鏇挎爣鍑嗙殑鎻愪氦鎸夐挳锛岀敤绫讳技杩欐牱鐨勬爣璁帮細

<input type="image" src="image.gif" name="sub" />
褰撶敤鎴风偣鍑诲埌鍥惧儚涓殑鏌愬鏃讹紝鐩稿簲鐨勮〃鍗曚細琚紶閫佸埌鏈嶅姟鍣紝骞跺姞涓婁袱涓彉閲 sub_x 鍜 sub_y銆傚畠浠寘鍚簡鐢ㄦ埛鐐瑰嚮鍥惧儚鐨勫潗鏍囥傛湁缁忛獙鐨勭敤鎴峰彲鑳戒細娉ㄦ剰鍒拌娴忚鍣ㄥ彂閫佺殑瀹為檯鍙橀噺鍚嶅寘鍚殑鏄竴涓偣鑰屼笉鏄笅鍒掔嚎锛堝嵆 sub.x 鍜 sub.y锛夛紝浣 PHP 鑷姩灏嗙偣杞崲鎴愪簡涓嬪垝绾裤

HTTP Cookies
PHP 閫忔槑鍦版敮鎸 ? Netscape 瑙勮寖瀹氫箟涓殑 HTTP cookies銆侰ookies 鏄竴绉嶅湪杩滅娴忚鍣ㄧ瀛樺偍鏁版嵁骞惰兘杩借釜鎴栬瘑鍒啀娆¤闂殑鐢ㄦ埛鐨勬満鍒躲傚彲浠ョ敤 setcookie() 鍑芥暟璁惧畾 cookies銆侰ookies 鏄 HTTP 淇℃伅澶翠腑鐨勪竴閮ㄥ垎锛屽洜姝 SetCookie 鍑芥暟蹇呴』鍦ㄥ悜娴忚鍣ㄥ彂閫佷换浣曡緭鍑轰箣鍓嶈皟鐢ㄣ傚浜 header() 鍑芥暟涔熸湁鍚屾牱鐨勯檺鍒躲侰ookie 鏁版嵁浼氬湪鐩稿簲鐨 cookie 鏁版嵁鏁扮粍涓彲鐢紝渚嬪 $_COOKIE锛$HTTP_COOKIE_VARS 鍜 $_REQUEST銆傛洿澶氱粏鑺傚拰渚嬪瓙瑙 setcookie() 鎵嬪唽椤甸潰銆

濡傛灉瑕佸皢澶氫釜鍊艰祴缁欎竴涓 cookie 鍙橀噺锛屽繀椤诲皢鍏惰祴鎴愭暟缁勩備緥濡傦細

<?php
  setcookie("MyCookie[foo]", 'Testing 1', time()+3600);
  setcookie("MyCookie[bar]", 'Testing 2', time()+3600);
?>
杩欏皢浼氬缓绔嬩袱涓崟鐙殑 cookie锛屽敖绠 MyCookie 鍦ㄨ剼鏈腑鏄竴涓崟涓鐨勬暟缁勩傚鏋滄兂鍦ㄤ粎浠呬竴涓 cookie 涓瀹氬涓硷紝鑰冭檻鍏堝湪鍊间笂浣跨敤 serialize() 鎴 explode()銆

娉ㄦ剰鍦ㄦ祻瑙堝櫒涓竴涓 cookie 浼氭浛鎹㈡帀涓婁竴涓悓鍚嶇殑 cookie锛岄櫎闈炶矾寰勬垨鑰呭煙涓嶅悓銆傚洜姝ゅ浜庤喘鐗╄溅绋嬪簭鍙互淇濈暀涓涓鏁板櫒骞朵竴璧蜂紶閫掞紝渚嬪锛

Example #4 涓涓 setcookie() 鐨勭ず渚

<?php
if (isset($_COOKIE['count'])) {
    $count = $_COOKIE['count'] + 1;
} else {
    $count = 1;
}
setcookie('count', $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
?>
鍙橀噺鍚嶄腑鐨勭偣
閫氬父锛孭HP 涓嶄細鏀瑰彉浼犻掔粰鑴氭湰涓殑鍙橀噺鍚嶃傜劧鑰屽簲璇ユ敞鎰忓埌鐐癸紙鍙ュ彿锛変笉鏄 PHP 鍙橀噺鍚嶄腑鐨勫悎娉曞瓧绗︺傝嚦浜庡師鍥狅紝鐪嬬湅锛 <?php
$varname.ext;  /* 闈炴硶鍙橀噺鍚 */
?> 杩欐椂锛岃В鏋愬櫒鐪嬪埌鏄竴涓悕涓 $varname 鐨勫彉閲忥紝鍚庨潰璺熺潃涓涓瓧绗︿覆杩炴帴杩愮畻绗︼紝鍚庨潰璺熺潃涓涓8瀛楃涓诧紙鍗虫病鏈夊姞寮曞彿鐨勫瓧绗︿覆锛屼笖涓嶅尮閰嶄换浣曞凡鐭ョ殑鍋ュ悕鎴栦繚鐣欏瓧锛'ext'銆傚緢鏄庢樉杩欎笉鏄兂瑕佺殑缁撴灉銆

鍑轰簬姝ゅ師鍥狅紝瑕佹敞鎰 PHP 灏嗕細鑷姩灏嗗彉閲忓悕涓殑鐐规浛鎹㈡垚涓嬪垝绾裤

纭畾鍙橀噺绫诲瀷
鍥犱负 PHP 浼氬垽鏂彉閲忕被鍨嬪苟鍦ㄩ渶瑕佹椂杩涜杞崲锛堥氬父鎯呭喌涓嬶級锛屽洜姝ゅ湪鏌愪竴鏃跺埢缁欏畾鐨勫彉閲忔槸浣曠绫诲瀷骞朵笉鏄庢樉銆侾HP 鍖呮嫭鍑犱釜鍑芥暟鍙互鍒ゆ柇鍙橀噺鐨勭被鍨嬶紝渚嬪锛歡ettype()锛宨s_array()锛宨s_float()锛宨s_int()锛宨s_object() 鍜 is_string()銆傚弬瑙佺被鍨嬩竴绔犮

浠ヤ笂灏辨槸PHP鍙橀噺涓潵鑷狿HP涔嬪鐨勫彉閲忕殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=60