Home > php教程 > php手册 > body text

10个典型实用的PHP代码片段

WBOY
Release: 2016-06-13 08:53:31
Original
944 people have browsed it

10个典型实用的PHP代码片段

本文将介绍10个经常会用到的PHP代码片段,包括黑名单过滤、随机颜色生成器、从网上下载文件、Alexa/Google Page Rank、强制下载文件、用Email显示用户的Gravator头像、用cURL获取RSS订阅数、截取图片、检查网站是否宕机。

一、黑名单过滤

<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> is_spam(</span><span class="vars">$text</span><span>, </span><span class="vars">$file</span><span>, </span><span class="vars">$split</span><span> = </span><span class="string">':'</span><span>, </span><span class="vars">$regex</span><span> = false){ </span></span></li><li><span>    <span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$file</span><span>, </span><span class="string">'rb'</span><span>); </span></span></li><li class="alt"><span>    <span class="vars">$contents</span><span> = </span><span class="func">fread</span><span>(</span><span class="vars">$handle</span><span>, </span><span class="func">filesize</span><span>(</span><span class="vars">$file</span><span>)); </span></span></li><li><span>    fclose(<span class="vars">$handle</span><span>); </span></span></li><li class="alt"><span>    <span class="vars">$lines</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="vars">$contents</span><span>); </span></span></li><li><span><span class="vars">$arr</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li><li class="alt"><span><span class="keyword">foreach</span><span>(</span><span class="vars">$lines</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$line</span><span>){ </span></span></li><li><span>list(<span class="vars">$word</span><span>, </span><span class="vars">$count</span><span>) = </span><span class="func">explode</span><span>(</span><span class="vars">$split</span><span>, </span><span class="vars">$line</span><span>); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$regex</span><span>) </span></span></li><li><span><span class="vars">$arr</span><span>[</span><span class="vars">$word</span><span>] = </span><span class="vars">$count</span><span>; </span></span></li><li class="alt"><span><span class="keyword">else</span><span> </span></span></li><li><span><span class="vars">$arr</span><span>[preg_quote(</span><span class="vars">$word</span><span>)] = </span><span class="vars">$count</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span>preg_match_all(<span class="string">"~"</span><span>.implode(</span><span class="string">'|'</span><span>, </span><span class="func">array_keys</span><span>(</span><span class="vars">$arr</span><span>)).</span><span class="string">"~"</span><span>, </span><span class="vars">$text</span><span>, </span><span class="vars">$matches</span><span>); </span></span></li><li class="alt"><span><span class="vars">$temp</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li><li><span><span class="keyword">foreach</span><span>(</span><span class="vars">$matches</span><span>[0] </span><span class="keyword">as</span><span> </span><span class="vars">$match</span><span>){ </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(!in_array(</span><span class="vars">$match</span><span>, </span><span class="vars">$temp</span><span>)){ </span></span></li><li><span><span class="vars">$temp</span><span>[</span><span class="vars">$match</span><span>] = </span><span class="vars">$temp</span><span>[</span><span class="vars">$match</span><span>] + 1; </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$temp</span><span>[</span><span class="vars">$match</span><span>] >= </span><span class="vars">$arr</span><span>[</span><span class="vars">$word</span><span>]) </span></span></li><li><span><span class="keyword">return</span><span> true; </span></span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span><span class="keyword">return</span><span> false; </span></span></li><li><span>} </span></li><li class="alt"><span> </span></li><li><span><span class="vars">$file</span><span> = </span><span class="string">'spam.txt'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$str</span><span> = </span><span class="string">'This string has cat, dog word'</span><span>; </span></span></li><li><span><span class="keyword">if</span><span>(is_spam(</span><span class="vars">$str</span><span>, </span><span class="vars">$file</span><span>)) </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'this is spam'</span><span>; </span></span></li><li><span><span class="keyword">else</span><span> </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'this is not spam'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>ab:3 </span></li><li><span>dog:3 </span></li><li class="alt"><span>cat:2 </span></li><li><span>monkey:2 </span></li></ol>
Copy after login

二、随机颜色生成器

<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> randomColor() { </span></span></li><li><span>    <span class="vars">$str</span><span> = </span><span class="string">'#'</span><span>; </span></span></li><li class="alt"><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span> = 0 ; </span><span class="vars">$i</span><span> < 6 ; </span><span class="vars">$i</span><span>++) { </span></span></li><li><span>        <span class="vars">$randNum</span><span> = rand(0 , 15); </span></span></li><li class="alt"><span>        <span class="keyword">switch</span><span> (</span><span class="vars">$randNum</span><span>) { </span></span></li><li><span>            <span class="keyword">case</span><span> 10: </span><span class="vars">$randNum</span><span> = </span><span class="string">'A'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>            <span class="keyword">case</span><span> 11: </span><span class="vars">$randNum</span><span> = </span><span class="string">'B'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li><span>            <span class="keyword">case</span><span> 12: </span><span class="vars">$randNum</span><span> = </span><span class="string">'C'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>            <span class="keyword">case</span><span> 13: </span><span class="vars">$randNum</span><span> = </span><span class="string">'D'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li><span>            <span class="keyword">case</span><span> 14: </span><span class="vars">$randNum</span><span> = </span><span class="string">'E'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>            <span class="keyword">case</span><span> 15: </span><span class="vars">$randNum</span><span> = </span><span class="string">'F'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li><span>        } </span></li><li class="alt"><span>        <span class="vars">$str</span><span> .= </span><span class="vars">$randNum</span><span>; </span></span></li><li><span>    } </span></li><li class="alt"><span>    <span class="keyword">return</span><span> </span><span class="vars">$str</span><span>; </span></span></li><li><span>} </span></li><li class="alt"><span><span class="vars">$color</span><span> = randomColor(); </span></span></li></ol>
Copy after login

三、从网上下载文件

<ol class="dp-c"><li class="alt"><span><span>set_time_limit(0); </span></span></li><li><span><span class="comment">// Supports all file types</span><span> </span></span></li><li class="alt"><span><span class="comment">// URL Here:</span><span> </span></span></li><li><span><span class="vars">$url</span><span> = </span><span class="string">'http://somsite.com/some_video.flv'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$pi</span><span> = </span><span class="func">pathinfo</span><span>(</span><span class="vars">$url</span><span>); </span></span></li><li><span><span class="vars">$ext</span><span> = </span><span class="vars">$pi</span><span>[</span><span class="string">'extension'</span><span>]; </span></span></li><li class="alt"><span><span class="vars">$name</span><span> = </span><span class="vars">$pi</span><span>[</span><span class="string">'filename'</span><span>]; </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// create a new cURL resource</span><span> </span></span></li><li><span><span class="vars">$ch</span><span> = curl_init(); </span></span></li><li class="alt"><span> </span></li><li><span><span class="comment">// set URL and other appropriate options</span><span> </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_URL, </span><span class="vars">$url</span><span>); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_HEADER, false); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_BINARYTRANSFER, true); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_AUTOREFERER, true); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_FOLLOWLOCATION, true); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_RETURNTRANSFER, true); </span></span></li><li class="alt"><span> </span></li><li><span><span class="comment">// grab URL and pass it to the browser</span><span> </span></span></li><li class="alt"><span><span class="vars">$opt</span><span> = curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// close cURL resource, and free up system resources</span><span> </span></span></li><li><span>curl_close(<span class="vars">$ch</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span><span class="vars">$saveFile</span><span> = </span><span class="vars">$name</span><span>.</span><span class="string">'.'</span><span>.</span><span class="vars">$ext</span><span>; </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(preg_match(</span><span class="string">"/[^0-9a-z._-]/i"</span><span>, </span><span class="vars">$saveFile</span><span>)) </span></span></li><li><span><span class="vars">$saveFile</span><span> = md5(microtime(true)).</span><span class="string">'.'</span><span>.</span><span class="vars">$ext</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span><span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$saveFile</span><span>, </span><span class="string">'wb'</span><span>); </span></span></li><li class="alt"><span>fwrite(<span class="vars">$handle</span><span>, </span><span class="vars">$opt</span><span>); </span></span></li><li><span>fclose(<span class="vars">$handle</span><span>); </span></span></li></ol>
Copy after login

四、Alexa/Google Page Rank

<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> page_rank(</span><span class="vars">$page</span><span>, </span><span class="vars">$type</span><span> = </span><span class="string">'alexa'</span><span>){ </span></span></li><li><span><span class="keyword">switch</span><span>(</span><span class="vars">$type</span><span>){ </span></span></li><li class="alt"><span><span class="keyword">case</span><span> </span><span class="string">'alexa'</span><span>: </span></span></li><li><span><span class="vars">$url</span><span> = </span><span class="string">'http://alexa.com/siteinfo/'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$url</span><span>.</span><span class="vars">$page</span><span>, </span><span class="string">'r'</span><span>); </span></span></li><li><span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span><span class="keyword">case</span><span> </span><span class="string">'google'</span><span>: </span></span></li><li><span><span class="vars">$url</span><span> = </span><span class="string">'http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info:'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$url</span><span>.</span><span class="string">'http://'</span><span>.</span><span class="vars">$page</span><span>, </span><span class="string">'r'</span><span>); </span></span></li><li><span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span><span class="vars">$content</span><span> = stream_get_contents(</span><span class="vars">$handle</span><span>); </span></span></li><li class="alt"><span>fclose(<span class="vars">$handle</span><span>); </span></span></li><li><span><span class="vars">$content</span><span> = preg_replace(</span><span class="string">"~(n|t|ss+)~"</span><span>,</span><span class="string">''</span><span>, </span><span class="vars">$content</span><span>); </span></span></li><li class="alt"><span><span class="keyword">switch</span><span>(</span><span class="vars">$type</span><span>){ </span></span></li><li><span><span class="keyword">case</span><span> </span><span class="string">'alexa'</span><span>: </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(preg_match(</span><span class="string">'~<div class="data (down|up)"><img .+? alt="10个典型实用的PHP代码片段" >(.+?) </div>~im'</span><span>,</span><span class="vars">$content</span><span>,</span><span class="vars">$matches</span><span>)){ </span></span></li><li><span><span class="keyword">return</span><span> </span><span class="vars">$matches</span><span>[2]; </span></span></li><li class="alt"><span>}<span class="keyword">else</span><span>{ </span></span></li><li><span><span class="keyword">return</span><span> FALSE; </span></span></li><li class="alt"><span>} </span></li><li><span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span><span class="keyword">case</span><span> </span><span class="string">'google'</span><span>: </span></span></li><li><span><span class="vars">$rank</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">':'</span><span>,</span><span class="vars">$content</span><span>); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$rank</span><span>[2] != </span><span class="string">''</span><span>) </span></span></li><li><span><span class="keyword">return</span><span> </span><span class="vars">$rank</span><span>[2]; </span></span></li><li class="alt"><span><span class="keyword">else</span><span> </span></span></li><li><span><span class="keyword">return</span><span> FALSE; </span></span></li><li class="alt"><span><span class="keyword">break</span><span>; </span></span></li><li><span><span class="keyword">default</span><span>: </span></span></li><li class="alt"><span><span class="keyword">return</span><span> FALSE; </span></span></li><li><span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span><span class="comment">// Alexa Page Rank:</span><span> </span></span></li><li><span><span class="func">echo</span><span> </span><span class="string">'Alexa Rank: '</span><span>.page_rank(</span><span class="string">'techug.com'</span><span>); </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">' '</span><span>; </span></span></li><li><span><span class="comment">// Google Page Rank</span><span> </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'Google Rank: '</span><span>.page_rank(</span><span class="string">'techug.com'</span><span>, </span><span class="string">'google'</span><span>); </span></span></li></ol>
Copy after login

五、强制下载文件

<ol class="dp-c"><li class="alt"><span><span class="vars">$filename</span><span> = </span><span class="vars">$_GET</span><span>[</span><span class="string">'file'</span><span>]; </span><span class="comment">//Get the fileid from the URL</span><span> </span></span></li><li><span><span class="comment">// Query the file ID</span><span> </span></span></li><li class="alt"><span><span class="vars">$query</span><span> = sprintf(</span><span class="string">"SELECT * FROM tableName WHERE id = '%s'"</span><span>,mysql_real_escape_string(</span><span class="vars">$filename</span><span>)); </span></span></li><li><span><span class="vars">$sql</span><span> = mysql_query(</span><span class="vars">$query</span><span>); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(mysql_num_rows(</span><span class="vars">$sql</span><span>) > 0){ </span></span></li><li><span><span class="vars">$row</span><span> = mysql_fetch_array(</span><span class="vars">$sql</span><span>); </span></span></li><li class="alt"><span><span class="comment">// Set some headers</span><span> </span></span></li><li><span>header(<span class="string">"Pragma: public"</span><span>); </span></span></li><li class="alt"><span>header(<span class="string">"Expires: 0"</span><span>); </span></span></li><li><span>header(<span class="string">"Cache-Control: must-revalidate, post-check=0, pre-check=0"</span><span>); </span></span></li><li class="alt"><span>header(<span class="string">"Content-Type: application/force-download"</span><span>); </span></span></li><li><span>header(<span class="string">"Content-Type: application/octet-stream"</span><span>); </span></span></li><li class="alt"><span>header(<span class="string">"Content-Type: application/download"</span><span>); </span></span></li><li><span>header(<span class="string">"Content-Disposition: attachment; filename="</span><span>.</span><span class="func">basename</span><span>(</span><span class="vars">$row</span><span>[</span><span class="string">'FileName'</span><span>]).</span><span class="string">";"</span><span>); </span></span></li><li class="alt"><span>header(<span class="string">"Content-Transfer-Encoding: binary"</span><span>); </span></span></li><li><span>header(<span class="string">"Content-Length: "</span><span>.</span><span class="func">filesize</span><span>(</span><span class="vars">$row</span><span>[</span><span class="string">'FileName'</span><span>])); </span></span></li><li class="alt"><span> </span></li><li><span>@readfile(<span class="vars">$row</span><span>[</span><span class="string">'FileName'</span><span>]); </span></span></li><li class="alt"><span><span class="func">exit</span><span>(0); </span></span></li><li><span>}<span class="keyword">else</span><span>{ </span></span></li><li class="alt"><span>header(<span class="string">"Location: /"</span><span>); </span></span></li><li><span><span class="func">exit</span><span>; </span></span></li><li class="alt"><span>} </span></li></ol>
Copy after login

六、用Email显示用户的Gravator头像

<ol class="dp-c"><li class="alt"><span><span class="vars">$gravatar_link</span><span> = </span><span class="string">'http://www.gravatar.com/avatar/'</span><span> . md5(</span><span class="vars">$comment_author_email</span><span>) . </span><span class="string">'?s=32'</span><span>; </span></span></li><li><span><span class="func">echo</span><span> </span><span class="string">'<img  src="'</span alt="10个典型实用的PHP代码片段" ><span> . </span><span class="vars">$gravatar_link</span><span> . </span><span class="string">'" />'</span><span>; </span></span></li></ol>
Copy after login

七、用cURL获取RSS订阅数

<ol class="dp-c"><li class="alt"><span><span class="vars">$ch</span><span> = curl_init(); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_URL,</span><span class="string">'https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=7qkrmib4r9rscbplq5qgadiiq4'</span><span>); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_RETURNTRANSFER,1); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_CONNECTTIMEOUT,2); </span></span></li><li class="alt"><span><span class="vars">$content</span><span> = curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li><li><span><span class="vars">$subscribers</span><span> = get_match(</span><span class="string">'/circulation="(.*)"/isU'</span><span>,</span><span class="vars">$content</span><span>); </span></span></li><li class="alt"><span>curl_close(<span class="vars">$ch</span><span>);  </span></span></li></ol>
Copy after login

八、时间差异计算

<ol class="dp-j"><li class="alt"><span><span>function ago($time) </span></span></li><li><span>{ </span></li><li class="alt"><span>   $periods = array(<span class="string">"second"</span><span>, </span><span class="string">"minute"</span><span>, </span><span class="string">"hour"</span><span>, </span><span class="string">"day"</span><span>, </span><span class="string">"week"</span><span>, </span><span class="string">"month"</span><span>, </span><span class="string">"year"</span><span>, </span><span class="string">"decade"</span><span>); </span></span></li><li><span>$lengths = array(<span class="string">"60"</span><span>,</span><span class="string">"60"</span><span>,</span><span class="string">"24"</span><span>,</span><span class="string">"7"</span><span>,</span><span class="string">"4.35"</span><span>,</span><span class="string">"12"</span><span>,</span><span class="string">"10"</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span>$now = time(); </span></li><li class="alt"><span> </span></li><li><span>$difference = $now - $time; </span></li><li class="alt"><span>$tense = <span class="string">"ago"</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">for</span><span>($j = </span><span class="number">0</span><span>; $difference >= $lengths[$j] && $j < count($lengths)-</span><span class="number">1</span><span>; $j++) { </span></span></li><li><span>$difference /= $lengths[$j]; </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span>$difference = round($difference); </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>($difference != </span><span class="number">1</span><span>) { </span></span></li><li><span>$periods[$j].= <span class="string">"s"</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">return</span><span> </span><span class="string">"$difference $periods[$j] 'ago' "</span><span>; </span></span></li><li><span>} </span></li></ol>
Copy after login

九、截取图片

<ol class="dp-c"><li class="alt"><span><span class="vars">$filename</span><span>= </span><span class="string">"test.jpg"</span><span>; </span></span></li><li><span>list(<span class="vars">$w</span><span>, </span><span class="vars">$h</span><span>, </span><span class="vars">$type</span><span>, </span><span class="vars">$attr</span><span>) = </span><span class="func">getimagesize</span><span>(</span><span class="vars">$filename</span><span>); </span></span></li><li class="alt"><span><span class="vars">$src_im</span><span> = imagecreatefromjpeg(</span><span class="vars">$filename</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$src_x</span><span> = </span><span class="string">'0'</span><span>; </span><span class="comment">// begin x</span><span> </span></span></li><li><span><span class="vars">$src_y</span><span> = </span><span class="string">'0'</span><span>; </span><span class="comment">// begin y</span><span> </span></span></li><li class="alt"><span><span class="vars">$src_w</span><span> = </span><span class="string">'100'</span><span>; </span><span class="comment">// width</span><span> </span></span></li><li><span><span class="vars">$src_h</span><span> = </span><span class="string">'100'</span><span>; </span><span class="comment">// height</span><span> </span></span></li><li class="alt"><span><span class="vars">$dst_x</span><span> = </span><span class="string">'0'</span><span>; </span><span class="comment">// destination x</span><span> </span></span></li><li><span><span class="vars">$dst_y</span><span> = </span><span class="string">'0'</span><span>; </span><span class="comment">// destination y</span><span> </span></span></li><li class="alt"><span> </span></li><li><span><span class="vars">$dst_im</span><span> = imagecreatetruecolor(</span><span class="vars">$src_w</span><span>, </span><span class="vars">$src_h</span><span>); </span></span></li><li class="alt"><span><span class="vars">$white</span><span> = imagecolorallocate(</span><span class="vars">$dst_im</span><span>, 255, 255, 255); </span></span></li><li><span>imagefill(<span class="vars">$dst_im</span><span>, 0, 0, </span><span class="vars">$white</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span>imagecopy(<span class="vars">$dst_im</span><span>, </span><span class="vars">$src_im</span><span>, </span><span class="vars">$dst_x</span><span>, </span><span class="vars">$dst_y</span><span>, </span><span class="vars">$src_x</span><span>, </span><span class="vars">$src_y</span><span>, </span><span class="vars">$src_w</span><span>, </span><span class="vars">$src_h</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span>header(<span class="string">"Content-type: image/png"</span><span>); </span></span></li><li class="alt"><span>imagepng(<span class="vars">$dst_im</span><span>); </span></span></li><li><span>imagedestroy(<span class="vars">$dst_im</span><span>); </span></span></li></ol>
Copy after login

十、检查网站是否宕机

<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> Visit(</span><span class="vars">$url</span><span>){ </span></span></li><li><span>       <span class="vars">$agent</span><span> = </span><span class="string">"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"</span><span>;</span><span class="vars">$ch</span><span>=curl_init(); </span></span></li><li class="alt"><span>curl_setopt (<span class="vars">$ch</span><span>, CURLOPT_URL,</span><span class="vars">$url</span><span> ); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_USERAGENT, </span><span class="vars">$agent</span><span>); </span></span></li><li class="alt"><span>curl_setopt (<span class="vars">$ch</span><span>, CURLOPT_RETURNTRANSFER, 1); </span></span></li><li><span>curl_setopt (<span class="vars">$ch</span><span>,CURLOPT_VERBOSE,false); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_TIMEOUT, 5); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_SSL_VERIFYPEER, FALSE); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_SSLVERSION,3); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_SSL_VERIFYHOST, FALSE); </span></span></li><li class="alt"><span><span class="vars">$page</span><span>=curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li><li><span><span class="comment">//echo curl_error($ch);</span><span> </span></span></li><li class="alt"><span><span class="vars">$httpcode</span><span> = curl_getinfo(</span><span class="vars">$ch</span><span>, CURLINFO_HTTP_CODE); </span></span></li><li><span>curl_close(<span class="vars">$ch</span><span>); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$httpcode</span><span>>=200 && </span><span class="vars">$httpcode</span><span><300) </span><span class="keyword">return</span><span> true; </span></span></li><li><span><span class="keyword">else</span><span> </span><span class="keyword">return</span><span> false; </span></span></li><li class="alt"><span>} </span></li><li><span><span class="keyword">if</span><span> (Visit(</span><span class="string">"http://www.google.com"</span><span>)) </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">"Website OK"</span><span>.</span><span class="string">"n"</span><span>; </span></span></li><li><span><span class="keyword">else</span><span> </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">"Website DOWN"</span><span>; </span></span></li></ol>
Copy after login



Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template