国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 后端開發(fā) php教程 php 十個超級有用的PHP代碼片段

php 十個超級有用的PHP代碼片段

Jul 25, 2016 am 08:42 AM

十個超級有用的php代碼片段

[PHP]代碼

  1. 1. 發(fā)送短信
  2. 調(diào)用 TextMagic API。
  3. // Include the TextMagic PHP lib
  4. require('textmagic-sms-api-php/TextMagicAPI.php');
  5. // Set the username and password information
  6. $username = 'myusername';
  7. $password = 'mypassword';
  8. // Create a new instance of TM
  9. $router = new TextMagicAPI(array(
  10. 'username' => $username,
  11. 'password' => $password
  12. ));
  13. // Send a text message to '999-123-4567'
  14. $result = $router->send('Wake up!', array(9991234567), true);
  15. // result: Result is: Array ( [messages] => Array ( [19896128] => 9991234567 ) [sent_text] => Wake up! [parts_count] => 1 )
  16. 2. 根據(jù)IP查找地址
  17. function detect_city($ip) {
  18. $default = 'UNKNOWN';
  19. if (!is_string($ip) || strlen($ip) $ip = '8.8.8.8';
  20. $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
  21. $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
  22. $ch = curl_init();
  23. $curl_opt = array(
  24. CURLOPT_FOLLOWLOCATION => 1,
  25. CURLOPT_HEADER => 0,
  26. CURLOPT_RETURNTRANSFER => 1,
  27. CURLOPT_USERAGENT => $curlopt_useragent,
  28. CURLOPT_URL => $url,
  29. CURLOPT_TIMEOUT => 1,
  30. CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'],
  31. );
  32. curl_setopt_array($ch, $curl_opt);
  33. $content = curl_exec($ch);
  34. if (!is_null($curl_info)) {
  35. $curl_info = curl_getinfo($ch);
  36. }
  37. curl_close($ch);
  38. if ( preg_match('{
  39. City : ([^}i', $content, $regs) ) {
  40. $city = $regs[1];
  41. }
  42. if ( preg_match('{
  43. State/Province : ([^}i', $content, $regs) ) {
  44. $state = $regs[1];
  45. }
  46. if( $city!='' && $state!='' ){
  47. $location = $city . ', ' . $state;
  48. return$location;
  49. }else{
  50. return$default;
  51. }
  52. }
  53. 3. 顯示網(wǎng)頁的源代碼
  54. $lines = file('http://google.com/');
  55. foreach ($lines as $line_num => $line) {
  56. // loop thru each line and prepend line numbers
  57. echo "Line #{$line_num} : " . htmlspecialchars($line) . "
    \n";
  58. }
  59. 4. 檢查服務(wù)器是否使用HTTPS
  60. if ($_SERVER['HTTPS'] != "on") {
  61. echo "This is not HTTPS";
  62. }else{
  63. echo "This is HTTPS";
  64. }
  65. 5. 顯示Faceboo**絲數(shù)量
  66. function fb_fan_count($facebook_name){
  67. // Example: https://graph.facebook.com/digimantra
  68. $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name));
  69. echo $data->likes;
  70. }
  71. 6. 檢測圖片的主要顏色
  72. $i = imagecreatefromjpeg("image.jpg");
  73. for ($x=0;$xfor ($y=0;$y$rgb = imagecolorat($i,$x,$y);
  74. $r = ($rgb >> 16) & 0xFF;
  75. $g = ($rgb >> & 0xFF;
  76. $b = $rgb & 0xFF;
  77. $rTotal += $r;
  78. $gTotal += $g;
  79. $bTotal += $b;
  80. $total++;
  81. }
  82. }
  83. $rAverage = round($rTotal/$total);
  84. $gAverage = round($gTotal/$total);
  85. $bAverage = round($bTotal/$total);
  86. 7. 獲取內(nèi)存使用信息
  87. echo"Initial: ".memory_get_usage()." bytes \n";
  88. /* prints
  89. Initial: 361400 bytes
  90. */
  91. // http://www.baoluowanxiang.com/
  92. // let's use up some memory
  93. for ($i = 0; $i $array []= md5($i);
  94. }
  95. // let's remove half of the array
  96. for ($i = 0; $i unset($array[$i]);
  97. }
  98. echo"Final: ".memory_get_usage()." bytes \n";
  99. /* prints
  100. Final: 885912 bytes
  101. */
  102. echo"Peak: ".memory_get_peak_usage()." bytes \n";
  103. /* prints
  104. Peak: 13687072 bytes
  105. */
  106. 8. 使用 gzcompress() 壓縮數(shù)據(jù)
  107. $string =
  108. "Lorem ipsum dolor sit amet, consectetur
  109. adipiscing elit. Nunc ut elit id mi ultricies
  110. adipiscing. Nulla facilisi. Praesent pulvinar,
  111. sapien vel feugiat vestibulum, nulla dui pretium orci,
  112. non ultricies elit lacus quis ante. Lorem ipsum dolor
  113. sit amet, consectetur adipiscing elit. Aliquam
  114. pretium ullamcorper urna quis iaculis. Etiam ac massa
  115. sed turpis tempor luctus. Curabitur sed nibh eu elit
  116. mollis congue. Praesent ipsum diam, consectetur vitae
  117. ornare a, aliquam a nunc. In id magna pellentesque
  118. tellus posuere adipiscing. Sed non mi metus, at lacinia
  119. augue. Sed magna nisi, ornare in mollis in, mollis
  120. sed nunc. Etiam at justo in leo congue mollis.
  121. Nullam in neque eget metus hendrerit scelerisque
  122. eu non enim. Ut malesuada lacus eu nulla bibendum
  123. id euismod urna sodales. ";
  124. $compressed = gzcompress($string);
  125. echo "Original size: ". strlen($string)."\n";
  126. /* prints
  127. Original size: 800
  128. */
  129. echo "Compressed size: ". strlen($compressed)."\n";
  130. /* prints
  131. Compressed size: 418
  132. */
  133. // getting it back
  134. $original = gzuncompress($compressed);
  135. 9. 使用PHP做Whois檢查
  136. function whois_query($domain) {
  137. // fix the domain name:
  138. $domain = strtolower(trim($domain));
  139. $domain = preg_replace('/^http:\/\//i', '', $domain);
  140. $domain = preg_replace('/^www\./i', '', $domain);
  141. $domain = explode('/', $domain);
  142. $domain = trim($domain[0]);
  143. // split the TLD from domain name
  144. $_domain = explode('.', $domain);
  145. $lst = count($_domain)-1;
  146. $ext = $_domain[$lst];
  147. // You find resources and lists
  148. // like these on wikipedia:
  149. //
  150. // http://de.wikipedia.org/wiki/Whois
  151. //
  152. $servers = array(
  153. "biz" => "whois.neulevel.biz",
  154. "com" => "whois.internic.net",
  155. "us" => "whois.nic.us",
  156. "coop" => "whois.nic.coop",
  157. "info" => "whois.nic.info",
  158. "name" => "whois.nic.name",
  159. "net" => "whois.internic.net",
  160. "gov" => "whois.nic.gov",
  161. "edu" => "whois.internic.net",
  162. "mil" => "rs.internic.net",
  163. "int" => "whois.iana.org",
  164. "ac" => "whois.nic.ac",
  165. "ae" => "whois.uaenic.ae",
  166. "at" => "whois.ripe.net",
  167. "au" => "whois.aunic.net",
  168. "be" => "whois.dns.be",
  169. "bg" => "whois.ripe.net",
  170. "br" => "whois.registro.br",
  171. "bz" => "whois.belizenic.bz",
  172. "ca" => "whois.cira.ca",
  173. "cc" => "whois.nic.cc",
  174. "ch" => "whois.nic.ch",
  175. "cl" => "whois.nic.cl",
  176. "cn" => "whois.cnnic.net.cn",
  177. "cz" => "whois.nic.cz",
  178. "de" => "whois.nic.de",
  179. "fr" => "whois.nic.fr",
  180. "hu" => "whois.nic.hu",
  181. "ie" => "whois.domainregistry.ie",
  182. "il" => "whois.isoc.org.il",
  183. "in" => "whois.ncst.ernet.in",
  184. "ir" => "whois.nic.ir",
  185. "mc" => "whois.ripe.net",
  186. "to" => "whois.tonic.to",
  187. "tv" => "whois.tv",
  188. "ru" => "whois.ripn.net",
  189. "org" => "whois.pir.org",
  190. "aero" => "whois.information.aero",
  191. "nl" => "whois.domain-registry.nl"
  192. );
  193. if (!isset($servers[$ext])){
  194. die('Error: No matching nic server found!');
  195. }
  196. $nic_server = $servers[$ext];
  197. $output = '';
  198. // connect to whois server:
  199. if ($conn = fsockopen ($nic_server, 43)) {
  200. fputs($conn, $domain."\r\n");
  201. while(!feof($conn)) {
  202. $output .= fgets($conn,128);
  203. }
  204. fclose($conn);
  205. }
  206. else { die('Error: Could not connect to ' . $nic_server . '!'); }
  207. return $output;
  208. }
  209. 10. 通過Email發(fā)送PHP錯誤
  210. // Our custom error handler
  211. function nettuts_error_handler($number, $message, $file, $line, $vars){
  212. $email = "
  213. An error ($number) occurred on line

  214. $line and in the file: $file.
  215. $message

    ";
  216. $email .= "
    " . print_r($vars, 1) . "
    ";
  217. $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  218. // Email the error to someone...
  219. error_log($email, 1, 'you@youremail.com', $headers);
  220. // Make sure that you decide how to respond to errors (on the user's side)
  221. // Either echo an error message, or kill the entire project. Up to you...
  222. // The code below ensures that we only "die" if the error was more than
  223. // just a NOTICE.
  224. if ( ($number !== E_NOTICE) && ($number die("There was an error. Please try again later.");
  225. }
  226. }
  227. // We should use our custom function to handle errors.
  228. set_error_handler('nettuts_error_handler');
  229. // Trigger an error... (var doesn't exist)
  230. echo$somevarthatdoesnotexist;
復(fù)制代碼
php, PHP


本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

對基于PHP的API進(jìn)行版本控制的最佳實踐是什么? 對基于PHP的API進(jìn)行版本控制的最佳實踐是什么? Jun 14, 2025 am 12:27 AM

基于toversionaphp,useUrl deuseUrl specteringforclarityAndEsofRouting,單獨的codetoavoidConflicts,dremecateOldVersionswithClearCommunication,andConsiderCustomHeadeSerlySerallyWhennEnncelsy.startbyplacingtheversionIntheUrl(E.G.,epi/api/v

如何在PHP中實施身份驗證和授權(quán)? 如何在PHP中實施身份驗證和授權(quán)? Jun 20, 2025 am 01:03 AM

tosecurelyhandleauthenticationandationallizationInphp,lofterTheSesteps:1.AlwaysHashPasswordSwithPassword_hash()andverifyusingspasspassword_verify(),usepreparedStatatementStopreventsqlineptions,andStoreSeruserDatain usseruserDatain $ _sessiveferterlogin.2.implementrole-2.imaccessccsccccccccccccccccccccccccc.

PHP中的程序和面向?qū)ο蟮木幊谭独g有什么區(qū)別? PHP中的程序和面向?qū)ο蟮木幊谭独g有什么區(qū)別? Jun 14, 2025 am 12:25 AM

procemal and object-tiriendedprogromming(oop)inphpdiffersimplessintustructure,可重復(fù)使用性和datahandling.1.procedural-Progrogursmingusesfunctimesfunctionsormanized sequalized sequalized sequiential,poiperforsmallscripts.2.OpporganizesCodeOrganizescodeOdeIntsocloceSandObjects,ModelingReal-Worlden-Worlden

PHP中有哪些弱參考(弱圖),何時有用? PHP中有哪些弱參考(弱圖),何時有用? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

如何在PHP中安全地處理文件上傳? 如何在PHP中安全地處理文件上傳? Jun 19, 2025 am 01:05 AM

要安全處理PHP中的文件上傳,核心在于驗證文件類型、重命名文件并限制權(quán)限。1.使用finfo_file()檢查真實MIME類型,僅允許特定類型如image/jpeg;2.用uniqid()生成隨機(jī)文件名,存儲至非Web根目錄;3.通過php.ini和HTML表單限制文件大小,設(shè)置目錄權(quán)限為0755;4.使用ClamAV掃描惡意軟件,增強(qiáng)安全性。這些步驟有效防止安全漏洞,確保文件上傳過程安全可靠。

如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進(jìn)行交互? 如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進(jìn)行交互? Jun 19, 2025 am 01:07 AM

是的,PHP可以通過特定擴(kuò)展或庫與MongoDB和Redis等NoSQL數(shù)據(jù)庫交互。首先,使用MongoDBPHP驅(qū)動(通過PECL或Composer安裝)創(chuàng)建客戶端實例并操作數(shù)據(jù)庫及集合,支持插入、查詢、聚合等操作;其次,使用Predis庫或phpredis擴(kuò)展連接Redis,執(zhí)行鍵值設(shè)置與獲取,推薦phpredis用于高性能場景,Predis則便于快速部署;兩者均適用于生產(chǎn)環(huán)境且文檔完善。

PHP中==(松散比較)和===(嚴(yán)格的比較)之間有什么區(qū)別? PHP中==(松散比較)和===(嚴(yán)格的比較)之間有什么區(qū)別? Jun 19, 2025 am 01:07 AM

在PHP中,==與===的主要區(qū)別在于類型檢查的嚴(yán)格程度。==在比較前會進(jìn)行類型轉(zhuǎn)換,例如5=="5"返回true,而===要求值和類型都相同才會返回true,例如5==="5"返回false。使用場景上,===更安全應(yīng)優(yōu)先使用,==僅在需要類型轉(zhuǎn)換時使用。

如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? 如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? Jun 19, 2025 pm 05:13 PM

PHP中使用基本數(shù)學(xué)運(yùn)算的方法如下:1.加法用 號,支持整數(shù)和浮點數(shù),也可用于變量,字符串?dāng)?shù)字會自動轉(zhuǎn)換但不推薦依賴;2.減法用-號,變量同理,類型轉(zhuǎn)換同樣適用;3.乘法用*號,適用于數(shù)字及類似字符串;4.除法用/號,需避免除以零,并注意結(jié)果可能是浮點數(shù);5.取模用%號,可用于判斷奇偶數(shù),處理負(fù)數(shù)時余數(shù)符號與被除數(shù)一致。正確使用這些運(yùn)算符的關(guān)鍵在于確保數(shù)據(jù)類型清晰并處理好邊界情況。

See all articles