`
#天琪#
  • 浏览: 155835 次
  • 性别: Icon_minigender_2
  • 来自: 长沙
社区版块
存档分类
最新评论

php 问题汇总解答

    博客分类:
  • php
阅读更多
1.如何截取指定长度汉字而不会出现以"?>"结尾,超出部分以"..."代替

  一般来说,要截取的变量来自Mysql,首先要保证那个字段长度要足够长,一般为char(200),可以保持100个汉字,包括标点.

  PHP代码:

  <?PHP
  $str="这个字符好长呀,^_^";
  $Short_Str=showShort($str,4);//截取前面4个汉字,结果为:这个字符...
  Echo   "$Short_Str";
  Function csubstr($str,$start,$len)
  {
  $strlen=strlen($str);
  $clen=0;
  for($i=0;$i<$strlen;$i++,$clen++)
  {
  if ($clen>=$start+$len)
  break;
  if(ord(substr($str,$i,1))>0xa0)
   //ord--Return ASCII value of character
   //substr -- Return part of a string
   //FE:$rest = substr("abcdef", 1, 3); // returns "bcd"
   //在ASCII中,0xa0表示汉字的开始 
  {
  if ($clen>=$start)
  $tmpstr.=substr($str,$i,2);
  $i++;
  }
  else
  {
  if ($clen>=$start)
  $tmpstr.=substr($str,$i,1);
  }
  }

  return $tmpstr;
  }
  Function showShort($str,$len)
  {
  $tempstr = csubstr($str,0,$len);
  if ($str<>$tempstr)
  $tempstr .= "..."; //要以什么结尾,修改这里就可以.

  return $tempstr;
  }

2.数据放入数据库和取出来显示在页面需要注意什么
  入库时

  $str=addslashes($str);

  $sql="insert into `tab` (`content`) values('$str')";

  出库时

  $str=stripslashes($str);

  显示时

  $str=htmlspecialchars(nl2br($str)) ;
  <?php

  //$content来自数据库

  $content=nl2br(htmlspecialchars($content));

  $content=str_replace(" ","&nbsp;",$content);

  $content=str_replace("\n","<br>\n",$content);

  ?>

3.我点击后退按钮,为什么之前填写的东西不见
  这是因为你使用了session.

  解决办法:
  PHP代码:

  <?php session_cache_limiter('private, must-revalidate');session_start();
  .....................?>



4.如何取得用户的真实IP

  PHP代码:

  <? function iptype1 () {

   if (getenv("HTTP_CLIENT_IP"))

   {
  return getenv("HTTP_CLIENT_IP");

  }

  else

  {

  return "none";
  }

  }

  function iptype2 () {

  if (getenv("HTTP_X_FORWARDED_FOR"))

  {

   return
  getenv("HTTP_X_FORWARDED_FOR");

  }

  else {

  return "none";
  }

  }

function iptype3 () {

  if (getenv("REMOTE_ADDR"))

  {

   return getenv("REMOTE_ADDR");
  }

   else {

  return "none";

   }

   }

  function ip() {

   $ip1 = iptype1();

   $ip2 = iptype2();

  $ip3 = iptype3();

  if (isset($ip1) && $ip1 != "none" && $ip1 != "unknown")

  {

  return $ip1;

  }

   elseif (isset($ip2) && $ip2 != "none" && $ip2 != "unknown")
  {

  return $ip2;

  }

  elseif (isset($ip3) && $ip3 != "none" && $ip3 != "unknown")

   {

  return $ip3;

  }

   else

  { return "none"; }

  }

   Echo ip();

  ?>

5.如何从数据库读取三天内的所有记录
  首先表格里要有一个DATETIME字段记录时间,

  格式为'2003-7-15 16:50:00'
  SELECT * FROM `xltxlm` WHERE TO_DAYS(NOW()) - TO_DAYS(`date`) <= 3;


6.怎么在图片里显示IP地址

  PHP代码:

  <? Header("Content-type: image/png");

  $img = ImageCreate(180,50);
  $ip = $_SERVER['REMOTE_ADDR'];

   ImageColorTransparent($img,$bgcolor);

  $bgColor = ImageColorAllocate($img, 0x2c,0x6D,0xAF); // 背景颜色

  $shadow = ImageColorAllocate($img, 250,0,0); // 阴影颜色

  $textColor = ImageColorAllocate($img, oxff,oxff,oxff); // 字体颜色

  ImageTTFText($img,10,0,78,30,$shadow,"d:/windows/fonts/Tahoma.ttf",$ip);
//显示背景

  ImageTTFText($img,10,0,25,28,$textColor,"d:/windows/fonts/Tahoma.ttf","your ip is".$ip);

// 显示IP    

  ImagePng($img);    

  imagecreatefrompng($img);
  ImageDestroy($img);      

  ?>

7.如何读取当前地址栏信息
  PHP代码:

  <?php

  $s="http://{$_SERVER['HTTP_HOST']}:{$_SERVER["SERVER_PORT"]}{$_SERVER['SCRIPT_NAME']}";

  $se='';
  foreach ($_GET as $key => $value) {
  $se.=$key."=".$value."&";
  }
  $se=Preg_Replace("/(.*)&$/","$1",$se);
  $se?$se="?".$se:"";
  echo $s."?$se";
  ?>

8.计算当前在线人数
  例子一:用文本实现
  PHP代码:

  <?php

  //首先你要有读写文件的权限

  //本程序可以直接运行,第一次报错,以后就可以

   $online_log = "count.dat"; //保存人数的文件,

   $timeout = 30;//30秒内没动作者,认为掉线

   $entries = file($online_log);
   $temp = array();


for ($i=0;$i<count($entries);$i++) {

   $entry = explode(",",trim($entries[$i]));

   if (($entry[0] != getenv('REMOTE_ADDR')) && ($entry[1] > time()))
{

   array_push($temp,$entry[0].",".$entry[1]."\n"); //取出其他浏览者的信息,并去掉超时者,保存进$temp

   }

   }
   array_push($temp,getenv('REMOTE_ADDR').",".(time() + ($timeout))."\n");
//更新浏览者的时间

   $users_online = count($temp); //计算在线人数
   $entries = implode("",$temp);

   //写入文件

   $fp = fopen($online_log,"w");

   flock($fp,LOCK_EX); //flock() 不能在NFS以及其他的一些网络文件系统中正常工作

   fputs($fp,$entries);

   flock($fp,LOCK_UN);

   fclose($fp);
   echo "当前有".$users_online."人在线";
  ?>



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics