在看本文之前,请确保你已掌握了PHP的一些知识以及MYSQL的查询操作基础

转载至:Submitted by 上火 on 2007, January 30, 1:12 PM. php学习

 

===> register_globals[全局变量]的相关说明
很多入门的朋友被全局变量搞的很糊涂,可以参考以下的说明:

 

 

CODE
  1. 决定是否将 EGPCS(Environment,GET,POST,Cookie,Server)变量注册为全局变量。例如,如果 register_globals = on,那么 URL http://www.example.com/test.php?id=3 将产生 $id。或者从 $_SERVER['DOCUMENT_ROOT'] 得来 $DOCUMENT_ROOT。如果不想用户数据把全局域弄乱的话可以将此选项关闭。自 PHP 4.2.0 开始,本指令默认为 off。推荐使用 PHP 的预定义变量来替代,例如超全局变量:$_ENV,$_GET,$_POST,$_COOKIE 和 $_SERVER。



===> 一个写文本数据的函数,可以有效防止清零
自己用在计数器中,在linux下日ip8万没有发现清零的现象

CODE
  1. <?php
  2. // Write Something to a Text
  3. function writetofile($filename,$data,$method="rb+"){
  4. touch($filename);
  5. $handle=fopen($filename,$method);
  6. flock($handle,LOCK_EX);
  7. $fettle=fwrite($handle,$data);
  8. if($method=="rb+"){ftruncate($handle,strlen($data));}
  9. fclose($handle);
  10. return $fettle;
  11. }
  12. //test
  13. writetofile("test.txt","Some data!");
  14. ?>

===> Client does not support authentication protocol
好多朋友升级了mysql为4.10以上会发生此错误,php4.x下phpmyadmin之类的程序连接不上数据库,是因为mysql4.10以上改变了用户密码验证协议,php的连接模块也需要更换新的,php5默认就是这种新的连接模块...

不想更换可以这样:

CODE
  1. # SET PASSWORD FOR 'some_user'@'some_host' = OLD_PASSWORD('newpwd');
  2. # FLUSH PRIVILEGES;



官方说明文档:

CODE
  1. Client does not support authentication protocol
  2. MySQL 4.1 and up uses an authentication protocol based on a password hashing algorithm that is incompatible with that used by older clients. If you upgrade the server to 4.1, attempts to connect to it with an older client may fail with the following message:
  3. shell> mysql
  4. Client does not support authentication protocol requested
  5. by server; consider upgrading MySQL client
  6. To solve this problem, you should use one of the following approaches:
  7. Upgrade all client programs to use a 4.1.1 or newer client library.
  8. When connecting to the server with a pre-4.1 client program, use an account that still has a pre-4.1-style password.
  9. Reset the password to pre-4.1 style for each user that needs to use a pre-4.1 client program. This can be done using the SET PASSWORD statement and the OLD_PASSWORD() function:
  10. mysql> SET PASSWORD FOR
  11. -> 'some_user'@'some_host' = OLD_PASSWORD('newpwd');
  12. Alternatively, use UPDATE and FLUSH PRIVILEGES:
  13. mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('newpwd')
  14. -> WHERE Host = 'some_host' AND User = 'some_user';
  15. mysql> FLUSH PRIVILEGES;
  16. Substitute the password you want to use for ``newpwd'' in the preceding examples. MySQL cannot tell you what the original password was, so you'll need to pick a new one.
  17. Tell the server to use the older password hashing algorithm:
  18. Start mysqld with the --old-passwords option.
  19. Assign an old-format password to each account that has had its password updated to the longer 4.1 format. You can identify these accounts with the following query:
  20. mysql> SELECT Host, User, Password FROM mysql.user
  21. -> WHERE LENGTH(Password) > 16;
  22. For each account record displayed by the query, use the Host and User values and assign a password using the OLD_PASSWORD() function and either SET PASSWORD or UPDATE, as described earlier.

===> 如果在数字前面强制加0
格式化数字的位数,未满位的加前导0

CODE
  1. <?php
  2. $num = 12;
  3. $var = sprintf("%09d", $num);
  4. echo $var;
  5. ?>



===> 取得当前脚本的路径
请不要用 $_SERVER['PHP_SELF']取路径,$_SERVER['PHP_SELF']取出来的在某些情况下是错误的.也不要用$_SERVER['SERVER_NAME'],$_SERVER['SERVER_NAME']取得的主机名不包含端口号,而且有时候取得的是服务器名,非你的虚拟主机名.
脚本路径:

CODE
  1. <?php
  2. $url = 'http://'.dirname($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']).'/';
  3. ?>


地址栏信息:

CODE
  1. <?php
  2. $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  3. ?>



===> url传递参数需要这样编码
与 HTML 实体相匹配的变量。像 &amp、&copy 和 &pound 都将被浏览器解析,并使用实际实体替代所期待的变量名。解决办法是使用 &amp; 代替 & 作为分隔符。使用 htmlentities(urlencode($data)) 对你的 URL 进行编码。

CODE
  1. <?php
  2. echo "<a href='filename.php?par=" . htmlentities(urlencode($parameter)) . "'>";
  3. ?>


接受页面直接使用,不需要解码.

===> 数据存入MySQL需要注意
入库:

CODE
  1. <?php
  2. $str = addslashes($str);
  3. ?>


出库:

CODE
  1. <?php
  2. $str = stripslashes($str);
  3. ?>



===> 返回MySQL数据库上一步 INSERT 操作产生的 ID
mysql_insert_id() 返回给定的 link_identifier 中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号。如果没有指定 link_identifier,则使用上一个打开的连接。

CODE
  1. <?php
  2. $last_id = mysql_insert_id();
  3. ?>



===> 如何解决表单出错返回从填的时候以前填写的东西全部消失
因为使用了session,可以用session_cache_limiter强制数据流从新生效.

CODE
  1. <?php
  2. // session start
  3. session_cache_limiter("private, must-revalidate");
  4. session_start();
  5. ...
  6. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值