All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP对GB编码动态转UTF-8编码的几种方法评测_[PHP教程]
PHP,has,encountered,an,Access,Violation解决方法总结_php资料_编程技术
PHP实例:生成静态页面的函数_[PHP教程]
WEB中基于XMLHTTP的简单实例分析_[XML教程]
ASPX文件转HTML_[Asp.Net教程]
用好VS2005之扩展membership服务_[Asp.Net教程]
修改后的prototype.js,增加进度条和验证输入框_JavaScript技术_编程技术
Visual C++ 6.0中的应用程序向导
C#,IDataReader造成的资源泄漏_.net资料_编程技术
从各种位置截取字符串的SQL语法_[SQL Server教程]
Reading number is top 10 pictures
网络游戏与脑残
可爱的小动物
治疗多发性骨髓瘤的特效药,一万二一支
A beautiful girl to bud1
8090后结婚的各种XX事
Female model behind the bitterness, often being overcharged3
刘亦菲写真集2
A man's favorite things3--ZhouWeiTong
The real super beauty3
The wise woman of chest1
Download software ranking
网页特效实例大全
电车之狼R
Sora aoi, the maid, students' uniforms
Unix video tutorial11
Tram sex maniac 2 (H) rar bag19
Tram sex maniac 2 (H) rar bag12
Boxer's Top ten classic battle4
Proficient in Eclipse
The Bermuda triangle3
Macromedia Dreamweaver 8
归海一刀 published in(发表于) 2014/2/10 6:50:38 Edit(编辑)
Php高手带路--问题汇总解答_[PHP教程]

Php高手带路--问题汇总解答_[PHP教程]

Php高手带路--问题汇总解答_[PHP教程]

1:为什么我得不到变量

   我在一网页向另一网页POST数据name,为什么输出name时却得不到任何值?

   在PHP4.2以后的版本中reGISter_global默认为off
   若想取得从另一页面提交的变量:

   方法一:在PHP.ini中找到register_global,并把它设置为on.
   方法二:在接收网页最前面放上这个extract(_POST);extract(_GET);(注意extract(_SESSION)前必须要有Session_Start()).
   方法三:一个一个读取变量a=_GET["a"];b=_POST["b"]等,这种方法虽然麻烦,但比较安全.

   2:调试你的程序

   在运行时必须知道某个变量为何值。我是这样做的,建立一文件debug.php,其内容如下:

   PHP代码:


   <?PHP
   Ob_Start();
   Session_Start();
   Echo "<pre>";

   Echo "本页得到的_GET变量有:";
   Print_R(_GET);

   Echo "本页得到的_POST变量有:";
   Print_R(_POST);

   Echo "本页得到的_COOKIE变量有:";
   Print_R(_COOKIE);

   Echo "本页得到的_SESSION变量有:";
   Print_R(_SESSION);

   Echo "</pre>";
   ?>


   然后在php.ini中设置:include_path = "c:/php",并将debug.php放在此文件夹,以后就可以在每个网页里包含此文件,查看得到的变量名和值.

   3:如何使用session

   凡是与session有关的,之前必须调用函数session_start();

   为session付值很简单,如:

   PHP代码:


   <?php
   Session_start();
   Name = "这是一个Session例子";
   Session_Register("Name");//注意,不要写成:Session_Register("Name");
   Echo _SESSION["Name"];
   //之后_SESSION["Name"]为"这是一个Session例子"
   ?>


   在php4.2之后,可以为session直接付值:

   PHP代码:


   <?PHP
   Session_Start();
   _SESSION["name"]="value";
   ?>


   取消session可以这样:
PHP代码:


   <?php
   session_start();
   session_unset();
   session_destroy();
   ?>


   取消某个session变量在php4.2以上还有BUG.

   注意:

   1:在调用Session_Start()之前不能有任何输出.例如下面是错误的.
   ==========================================
   1行
   2行 3行 Session_Start();//之前在第一行已经有输出
   4行 .....
   5行 ?>
   ==========================================

   提示1:

   凡是出现" ........headers already sent.......... ",就是Session_Start()之前向浏览器输出信息.去掉输出就正常,(COOKIE也会出现这种错误,错误原因一样)

   提示2:

   如果你的Session_Start()放在循环语句里,并且很难确定之前哪里向浏览器输出信息,可以用下面这种方法:
   1行 <?PHP Ob_Start(); ?>
   ........这里是你的程序......

   2:这是什么错误

   Warning: session_start(): open(/tmp\sess_7d190aa36b4c5ec13a5c1649cc2da23f, O_RDWR) failed:....
因为你没有指定session文件的存放路径.

   解决方法:
   (1)在c盘建立文件夹tmp
   (2)打开php.ini,找到session.save_path,修改为session.save_path= "c:/tmp"


   4:为什么我向另一网页传送变量时,只得到前半部分,以空格开头的则全部丢失

   PHP代码:


   <?php
   Var="hello php";//修改为Var=" hello php";试试得到什么结果
   post= "receive.php?Name=".Var;
   header("location:post");
   ?>

   receive.php的内容:

   PHP代码:

   <?PHP
   Echo "<pre>";
   Echo _GET["Name"];
   Echo "</pre>";
   ?>


   正确的方法是:

   PHP代码:


   <?php
   Var="hello php";
   post= "receive.php?Name=".urlencode(Var);
   header("location:post");
   ?>


   在接收页面你不需要使用Urldecode(),变量会自动编码.
9:我怎么知道系统默认支持什么函数

   PHP代码:


   <?php
   arr = get_defined_functions();
   Function php() {
   }
   echo "<pre>";
   Echo "这里显示系统所支持的所有函数,和自定以函数php\n";
   print_r(arr);
   echo "</pre>";
   ?>



   10:如何比较两个日期相差几天

   PHP代码:


   <?PHP
   Date_1="2003-7-15";//也可以是:Date_1="2003-7-15 23:29:14";
   Date_2="1982-10-1";
   d1=strtotime(Date_1);
   d2=strtotime(Date_2);
   Days=round((d1-d2)/3600/24);
   Echo "偶已经奋斗了 Days 天^_^";
   ?>



   11:为什么我升级PHP后,原来的程序出现满屏的 Notice: Undefined variable:

   这是警告的意思,由于变量未定义引起的.
   打开php.ini,找到最下面的error_reporting,修改为error_reporting = E_ALL & ~E_NOTICE

   对于Parse error错误
   error_reporting(0)无法关闭.
   如果你想关闭任何错误提示,打开php.ini,找到display_errors,设置为display_errors = Off.以后任何错误都不会提示.

   那什么是error_reporting?


   12:我想在每个文件最前,最后面都加上一文件.但一个一个添加很麻烦

   1:打开php.ini文件
   设置 include_path= "c:"

   2:写两个文件
   auto_prepend_file.php 和 auto_append_file.php 保存在c盘,他们将自动依附在每个php文件的头部和尾部.

   3:在php.ini中找到:
   Automatically add files before or after any PHP document.
   auto_prepend_file = auto_prepend_file.php;依附在头部
   auto_append_file = auto_append_file.php;依附在尾部

   以后你每个php文件就相当于

   PHP代码:


   <?php
   Include "auto_prepend_file.php" ;

   .......//这里是你的程序

   Include "auto_append_file.php";
   ?>


   13:如何利用PHP上传文件

   PHP代码:


   <html><head>
   <title>上载文件表单</title></head>
   <body>
   <form enctype="multipart/form-data" action="" method="post">
   请选择文件: <br>
   <input name="upload_file" type="file"><br>
   <input type="submit" value="上传文件">
   </form>
   </body>
   </html>
<?
   upload_file=_FILES['upload_file']['tmp_name'];
   upload_file_name=_FILES['upload_file']['name'];

   if(upload_file){
   file_size_max = 1000*1000;// 1M限制文件上传最大容量(bytes)
   store_dir = "d:/";// 上传文件的储存位置
   accept_overwrite = 1;//是否允许覆盖相同文件
   // 检查文件大小
   if (upload_file_size > file_size_max) {
   echo "对不起,你的文件容量大于规定";
   exit;
   }

   // 检查读写文件
   if (file_exists(store_dir . upload_file_name) && !accept_overwrite) {
   Echo "存在相同文件名的文件";
   exit;
   }

   //复制文件到指定目录
   if (!move_uploaded_file(upload_file,store_dir.upload_file_name)) {
   echo "复制文件失败";
   exit;
   }

   }

   Echo "<p>你上传了文件:";
   echo _FILES['upload_file']['name'];
   echo "<br>";
   //客户端机器文件的原名称。

   Echo "文件的 MIME 类型为:";
   echo _FILES['upload_file']['type'];
   //文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。
   echo "<br>";

   Echo "上传文件大小:";
   echo _FILES['upload_file']['size'];
   //已上传文件的大小,单位为字节。
   echo "<br>";

   Echo "文件上传后被临时储存为:";
   echo _FILES['upload_file']['tmp_name'];
   //文件被上传后在服务端储存的临时文件名。
   echo "<br>";


   Erroe=_FILES['upload_file']['error'];
   switch(Erroe){
   case 0:
  Echo "上传成功"; break;
   case 1:
  Echo "上传的文件超过了 PHP.ini 中 upload_max_filesize 选项限制的值."; break;
   case 2:
  Echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。"; break;
   case 3:
  Echo "文件只有部分被上传";break;
   case 4:
  Echo "没有文件被上传";break;
   }
   ?>



   14:如何配置GD库

   下面是我的配置过程
   1:用dos命令(也可以手动操作,拷贝dlls文件夹里所有dll文件到system32目录下) copy c:\php\dlls\*.dll c:\Windows\system32\
   2:打开php.ini
   设置extension_dir = "c:/php/extensions/";
   3:
   extension=php_gd2.dll;把extension前面的逗号去掉,如果没有php_gd2.dll,php_gd.dll也一样,保证确实存在这一文件c:/php/extensions/php_gd2.dll
   4:运行下面程序进行测试

   PHP代码:


   <?php
   Ob_end_flush();
   //注意,在此之前不能向浏览器输出任何信息,要注意是否设置了 auto_prepend_file.
   header ("Content-type: image/png");
   im = @imagecreate (200, 100)
  or die ("无法创建图像");
   background_color = imagecolorallocate (im, 0,0, 0);
   text_color = imagecolorallocate (im, 230, 140, 150);
   imagestring (im, 3, 30, 50, "A Simple Text String", text_color);
   imagepng (im);
   ?>


   15:什么是UBB代码

   UBB代码是HTML的一个变种,是Ultimate Bulletin Board (国外一个BBS程序,国内也有不少地方使用这个程序)采用的一种特殊的TAG.
   即使禁止使用 HTML,你也可以用 UBBCode? 来实现.也许你更希望使用 UBBCode? 而不是 HTML, 即使论坛允许使用 HTML, 因为使用起来代码较少也更安全.
5:如何截取指定长度汉字而不会出现以"?>"结尾,超出部分以"..."代替

   一般来说,要截取的变量来自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)
   {
   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;
   }



   6:规范你的SQL语句

   在表格,字段前面加上"`",这样就不会因为误用关键字而出现错误,当然我并不推荐你使用关键字.

   例如
   Sql="INSERT INTO `xltxlm` (`author`, `title`, `id`, `content`, `date`) VALUES ('xltxlm', 'use`', 1, 'criterion your sql string ', '2003-07-11 00:00:00')"

   "`"怎么输入? 在TAB键上面.


   7:如何使Html/PHP格式的字符串不被解释,而是照原样显示

   PHP代码:


   <?PHP
   str="<h1>PHP</h1>";
   Echo "被解释过的: ".str."<br>经过处理的:";
   Echo htmlentities(nl2br(str));
   ?>



   8:怎么在函数里取得函数外的变量值

   PHP代码:


   <?PHP
   a="PHP";
   foo();
   Function foo()
   {
   global a;//删除这里看看是什么结果
   Echo "a";
   }
   ?>


来源: php联盟





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.