10 code examples commonly used in PHP development

Source: Internet
Author: User
First, blacklist filter

Second, random color generator

Third, download the file from the Internet

Set_time_limit (0); Supports all file types//urls here: $url = ' http://somsite.com/some_video.flv '; $pi = PathInfo ($url); $ext = $pi [' extension ']; $name = $pi [' filename ']; Create a new CURL resource $ch = Curl_init (); Set URL and other appropriate options curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_header, false); curl_setopt ($ch, Curlopt_binarytransfer, true); curl_setopt ($ch, Curlopt_autoreferer, true); curl_setopt ($ch, curlopt_followlocation, true); curl_setopt ($ch, Curlopt_returntransfer, true); Grab URL and pass it to the browser $opt = curl_exec ($ch); Close CURL resource, and free up system resources curl_close ($CH); $saveFile = $name. '. '. $ext; if (Preg_match ("/[^0-9a-z._-]/i", $saveFile)) $saveFile = MD5 (Microtime (TRUE)). $ext; $handle = fopen ($saveFile, ' WB '); Fwrite ($handle, $opt); Fclose ($handle);

Iv. Alexa/google Page Rank

V. Mandatory download of files

$filename = $_get[' file ']; Get the Fileid from the URL//Query of the file id $query = sprintf ("select * from tableName WHERE ID = '%s '", mysql_real_e Scape_string ($filename)); $sql = mysql_query ($query); if (mysql_num_rows ($sql) > 0) {$row = Mysql_fetch_array ($sql);//Set Some headers header ("Pragma:public"); Header ("Exp Ires:0 "); Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0"); Header ("Content-type:application/force-download"); Header ("Content-type:application/octet-stream"); Header ("Content-type:application/download"); Header ("content-disposition:attachment; Filename= ". basename ($row [' FileName '])."; "); Header ("Content-transfer-encoding:binary"); Header ("Content-length:". FileSize ($row [' FileName ']); @readfile ($row [' FileName ']); Exit (0); }else{Header ("Location:/"); exit;}

VI. Display the user's gravator avatar with email


Seven, using Curl to get RSS subscription number

Eight, time difference calculation

Function ago ($time) {$periods = Array ("Second", "Minute", "Hour", "Day", "Week", "Month", "year", "decade"); $lengths = a Rray ("60", "60", "24", "7", "4.35", "12", "10"); $now = time (); $difference = $now-$time; $tense = "Ago"; for ($j = 0; $difference >= $lengths [$j] && $j < count ($lengths)-1; $j + +) {$difference/= $lengths [$j];} $d Ifference = round ($difference); if ($difference! = 1) {$periods [$j].= "S";} return "$difference $periods [$j] ' ago ';}

Ix. capture of pictures

Ten, check whether the website is down

The above content for PHP development commonly used in 10 code samples to do a summary, I hope to help you.

The above describes the PHP development commonly used in 10 code samples, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.