谁可相欹 发表于 2015-2-3 23:32:57

PHP网站制作之分享几个十分有效的PHP代码片断

培训的第二阶段,开始了PHP语言语法结构和应用的学习。      1. 发送短信
  挪用 TextMagic++ API。
  // Include the TextMagic PHP lib
  require('textmagic-sms-api-php/TextMagicAPI.php');
  // Set the username and password information
  $username = 'myusername';
  $password = 'mypassword';
  // Create a new instance of TM
  $router = new TextMagicAPI(array(
  'username' => $username,
  'password' => $password
  ));
  // Send a text message to '999-123-4567'
  $result = $router->send('Wake up!', array(9991234567), true);
  // result: Result is: Array ( => Array ( => 9991234567 ) => Wake up! => 1 )
   
  2. 依据IP查找地址
  function detect_city($ip) {
  $default = 'UNKNOWN';
  if (!is_string($ip) strlen($ip) < 1 $ip == '127.0.0.1' $ip == 'localhost')
  $ip = '8.8.8.8';
  $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)';
  $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
  $ch = curl_init();
  $curl_opt = array(
  CURLOPT_FOLLOWLOCATION => 1,
  CURLOPT_HEADER => 0,
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_USERAGENT => $curlopt_useragent,
  CURLOPT_URL => $url,
  CURLOPT_TIMEOUT => 1,
  CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'],
  );
  curl_setopt_array($ch, $curl_opt);
  $content = curl_exec($ch);
  if (!is_null($curl_info)) {
  $curl_info = curl_getinfo($ch);
  }
  curl_close($ch);
  if ( preg_match('{
  City : ([^<]*)
  }i', $content, $regs) ) {
  $city = $regs;
  }
  if ( preg_match('{
  State/Province : ([^<]*)
  }i', $content, $regs) ) {
  $state = $regs;
  }
  if( $city!='' && $state!='' ){
  $location = $city . ', ' . $state;
  return $location;
  }else{
  return $default;
  }
  }
   
  3. 显示网页的源代码
  $lines = file('http://谷歌.com/');
  foreach ($lines as $line_num => $line) {
  // loop thru each line and prepend line numbers
  echo "Line #{$line_num} : " . htmlspecialchars($line) . "
\n";
  }
  4. 反省办事器是不是利用HTTPS
  if ($_SERVER['HTTPS'] != "on") {
  echo "This is not HTTPS";
  }else{
  echo "This is HTTPS";
  }
   
  5. 显示Facebook粉丝数目
  function fb_fan_count($facebook_name){
  // Example: https://graph.facebook.com/digimantra
  $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name));
  echo $data->likes;
  }
  6. 检测图片的次要色彩
  $i = imagecreatefromjpeg("image.jpg");
  for ($x=0;$x
  for ($y=0;$y
  $rgb = imagecolorat($i,$x,$y);
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> & 0xFF;
  $b = $rgb & 0xFF;
  $rTotal += $r;
  $gTotal += $g;
  $bTotal += $b;
  $total++;
  }
  }
  $rAverage = round($rTotal/$total);
  $gAverage = round($gTotal/$total);
  $bAverage = round($bTotal/$total);
   
  7. 获得内存利用信息
  echo "Initial: ".memory_get_usage()." bytes \n";
  /* prints
  Initial: 361400 bytes
  */
  // let's use up some memory
  for ($i = 0; $i < 100000; $i++) {
  $array []= md5($i);
  }
  // let's remove half of the array
  for ($i = 0; $i < 100000; $i++) {
  unset($array[$i]);
  }
  echo "Final: ".memory_get_usage()." bytes \n";
  /* prints
  Final: 885912 bytes
  */
  echo "Peak: ".memory_get_peak_usage()." bytes \n";
  /* prints
  Peak: 13687072 bytes
  */
   
  8. 利用 gzcompress() 紧缩数据
  $string =
  "Lorem ipsum dolor sit amet, consectetur
  adipiscing elit. Nunc ut elit id mi ultricies
  adipiscing. Nulla facilisi. Praesent pulvinar,
  sapien vel feugiat vestibulum, nulla dui pretium orci,
  non ultricies elit lacus quis ante. Lorem ipsum dolor
  sit amet, consectetur adipiscing elit. Aliquam
  pretium ullamcorper urna quis iaculis. Etiam ac massa
  sed turpis tempor luctus. Curabitur sed nibh eu elit
  mollis congue. Praesent ipsum diam, consectetur vitae
  ornare a, aliquam a nunc. In id magna pellentesque
  tellus posuere adipiscing. Sed non mi metus, at lacinia
  augue. Sed magna nisi, ornare in mollis in, mollis
  sed nunc. Etiam at justo in leo congue mollis.
  Nullam in neque eget metus hendrerit scelerisque
  eu non enim. Ut malesuada lacus eu nulla bibendum
  id euismod urna sodales. ";
  $compressed = gzcompress($string);
  echo "Original size: ". strlen($string)."\n";
  /* prints
  Original size: 800
  */
  echo "Compressed size: ". strlen($compressed)."\n";
  /* prints
  Compressed size: 418
  */
  // getting it back
  $original = gzuncompress($compressed);
   
  9. 利用PHP做Whois反省
  function whois_query($domain) {
  // fix the domain name:
  $domain = strtolower(trim($domain));
  $domain = preg_replace('/^http:\/\//i', '', $domain);
  $domain = preg_replace('/^www\./i', '', $domain);
  $domain = explode('/', $domain);
  $domain = trim($domain);
  // split the TLD from domain name
  $_domain = explode('.', $domain);
  $lst = count($_domain)-1;
  $ext = $_domain[$lst];
  // You find resources and lists
  // like these on wikipedia:
  //
  // http://de.wikipedia.org/wiki/Whois
  //
  $servers = array(
  "biz" => "whois.neulevel.biz",
  "com" => "whois.internic.net",
  "us" => "whois.nic.us",
  "coop" => "whois.nic.coop",
  "info" => "whois.nic.info",
  "name" => "whois.nic.name",
  "net" => "whois.internic.net",
  "gov" => "whois.nic.gov",
  "edu" => "whois.internic.net",
  "mil" => "rs.internic.net",
  "int" => "whois.iana.org",
  "ac" => "whois.nic.ac",
  "ae" => "whois.uaenic.ae",
  "at" => "whois.ripe.net",
  "au" => "whois.aunic.net",
  "be" => "whois.dns.be",
  "bg" => "whois.ripe.net",
  "br" => "whois.registro.br",
  "bz" => "whois.belizenic.bz",
  "ca" => "whois.cira.ca",
  "cc" => "whois.nic.cc",
  "ch" => "whois.nic.ch",
  "cl" => "whois.nic.cl",
  "cn" => "whois.cnnic.net.cn",
  "cz" => "whois.nic.cz",
  "de" => "whois.nic.de",
  "fr" => "whois.nic.fr",
  "hu" => "whois.nic.hu",
  "ie" => "whois.domainregistry.ie",
  "il" => "whois.isoc.org.il",
  "in" => "whois.ncst.ernet.in",
  "ir" => "whois.nic.ir",
  "mc" => "whois.ripe.net",
  "to" => "whois.tonic.to",
  "tv" => "whois.tv",
  "ru" => "whois.ripn.net",
  "org" => "whois.pir.org",
  "aero" => "whois.information.aero",
  "nl" => "whois.domain-registry.nl"
  );
  if (!isset($servers[$ext])){
  die('Error: No matching nic server found!');
  }
  $nic_server = $servers[$ext];
  $output = '';
  // connect to whois server:
  if ($conn = fsockopen ($nic_server, 43)) {
  fputs($conn, $domain."\r\n");
  while(!feof($conn)) {
  $output .= fgets($conn,128);
  }
  fclose($conn);
  }
  else { die('Error: Could not connect to ' . $nic_server . '!'); }
  return $output;
  }
   
  10. 经由过程Email发送PHP毛病

  // Our custom error handler
  function nettuts_error_handler($number, $message, $file, $line, $vars){
  $email = "
  
An error ($number) occurred on line
  $line and in the file: $file.
  
$message
";
   
  $email .= "
" . print_r($vars, 1) . "";
   
  $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  // Email the error to someone...
  error_log($email, 1, 'you@youremail.com', $headers);
  // Make sure that you decide how to respond to errors (on the user's side)
  // Either echo an error message, or kill the entire project. Up to you...
  // The code below ensures that we only "die" if the error was more than
  // just a NOTICE.
  if ( ($number !== E_NOTICE) && ($number < 2048) ) {
  die("There was an error. Please try again later.");
  }
  }
  // We should use our custom function to handle errors.
  set_error_handler('nettuts_error_handler');
  // Trigger an error... (var doesn't exist)
  echo $somevarthatdoesnotexist;
学习数据库了,MYSQL可算是PHP的黄金搭档了,不过,虽然话是这么说,你也可能恨不得把MYSQL给生吞活剥了,因为这一行一列的东东简直让自己头晕目眩。

乐观 发表于 2015-2-4 02:02:03

php是动态网站开发的优秀语言,在学习的时候万万不能冒进。在系统的学习前,我认为不应该只是追求实现某种效果,因为即使你复制他人的代码调试成功,实现了你所期望的效果,你也不了解其中的原理。

精灵巫婆 发表于 2015-2-5 13:12:42

多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。

飘灵儿 发表于 2015-2-12 08:46:23

使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。

小妖女 发表于 2015-3-2 20:20:39

不禁又想起那些说php是草根语言的人,为什么认得差距这么大呢。

飘飘悠悠 发表于 2015-3-6 03:21:28

建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。

分手快乐 发表于 2015-3-12 19:25:43

最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。

愤怒的大鸟 发表于 2015-3-18 23:19:30

使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。

若天明 发表于 2015-3-24 10:53:10

实践是检验自己会不会的真理。

谁可相欹 发表于 2015-3-25 07:19:30

其实也不算什么什么心得,在各位大侠算是小巫见大巫了吧,望大家不要见笑,若其中有错误的地方请各位大虾斧正。

再见西城 发表于 2015-3-28 17:05:09

曾经犯过一个很低级的错误,我在文件命名的时候用了一个横线\\\\\\\'-\\\\\\\' 号,结果找了好几个小时的错误,事实是命名的时候 是不能用横线 \\\\\\\'-\\\\\\\' 的,应该用的是下划线\\\\\\\'_\\\\\\\' ;

第二个灵魂 发表于 2015-3-31 09:55:35

如果你可以写完像留言板这样的程序,那么你可以去一些别人的代码了,

老尸 发表于 2015-4-5 12:26:08

首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。

变相怪杰 发表于 2015-4-7 15:27:39

兴趣是最好的老师,百度是最好的词典。

再现理想 发表于 2015-4-10 23:38:13

我还是强烈建议自己搭建php环境。因为在搭建的过程中你会遇到一些问题,通过搜索或是看php手册解决问题后,你会更加深刻的理解它们的工作原理,了解到php配置文件中的一些选项设置。

海妖 发表于 2015-4-14 16:10:58

至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。

爱飞 发表于 2015-4-16 07:10:29

微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox支持的不是很好,所以能少用还是少用的好。

深爱那片海 发表于 2015-4-27 12:47:10

找到的的资料很多都是在论坛里的,需要注册,所以我一般没到一个论坛都注册一个id,所有的id都注册成一样的,这样下次再进来的时候就不用重复注册啦。当然有些论坛的某些资料是需要的付费的。

莫相离 发表于 2015-4-30 14:13:45

如果你已经到这种程度了,那么你已经可以做我的老师了。其实php也分很多的区域,

透明 发表于 2015-6-4 14:11:00

其实没啥难的,多练习,练习写程序,真正的实践比看100遍都有用。不过要熟悉引擎
页: [1]
查看完整版本: PHP网站制作之分享几个十分有效的PHP代码片断