您正在查看 "Php" 分类下的文章
2008-12-31 14:20
以下应该是我04年前后刚学动态网站建设时写的一篇文章,现在看来还是很有参考价值,甚至是很前沿,所以从百度上搜了出来。
用PHP编写留言本应注意的问题
一、防止表格变形
在相应表格标签中加入
style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all"
二、屏蔽html代码
在提交留言时把留言内容中的"<" 和 ">" 替换成 &lt; 和 &gt;即可。
三、替换硬回车
即保留签写留言者写留言时的样式,这也需要用到替换
在ASP中替换chr(13)为<br>
在PHP中替换" "为<br>
注意:如果要结合屏蔽html代码,必须先替换第二点所说的再替换第三点
欢迎提供编写留言本中其它应该注意的问题!
为什么这么说呢?在工作的近三年中,我的所有同事中,没有一个能忠于用户的真实意愿,当用户输入“&”字符,甚至是单引号,双引号,+号,script,html代码,会自动把这些给过滤掉,自认为这样做是为了保证网站安全。其实不然,如果我们后台做好相应的转义工作,这样的担心简直就是杞人忧天 。就拿一个留言本,或者一个心情来说(就是当下SNS最流行的用户更改心情,状态之类的东西,类似于QQ的签名),通常这类是只出现纯文本之类的东西。可能会有一些用户要输入一些死循环之类的script,但也有可能有用户在留言本中交流html代码之类的东西,如果一概而论过滤掉,那用户的真实意愿呢?谈何用户体验?
在我眼中,用户体验就是维护用户真实意愿,做到像博客这样的标准才行,输什么显示什么(当然像留言本或状态这类是不支持html代码的,所以应该把用户的html代码显示出来,而不是屏蔽)。
另外还有一个在现实工作中,经常发现各类网站出现的一个bug,那就是比如输入 a=b&c=d&z=x这类字符串,或者是一个动态的网址,第一个&符号之后的东西全部会截掉而不显示,通常这类网站都是通过ajax提交的,所以在js端中会认为&后面是提交的另一参数,导致被截取,这是很多sns网站没有注意的地方,最简单的解决方法就是把 & 替换成%26 或者用encodeURIComponent进行转义。
|
2008-10-06 22:27
相信很多初学者很需这个方法,帖出来,呵呵
//{{{ utf-8截取前n个字
function Ctruncate($str = '', $len = 0, $etc = ' ...')
{
if(0 == $len) return "";
$str_len = preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy);
if($len >= $str_len)
{
return $str;
}
else
{
$newstr = mb_substr($str,0,$len,'utf-8');
return $newstr.$etc;
}
}
//}}} |
2008-09-26 21:23
What is the difference between echo and print?
Which is faster, echo or print?
1. Speed. There is a difference between the two, but speed-wise it
should be irrelevant which one you use. echo is marginally faster
since
it doesn't set a return value if you really want to get down to the
nitty gritty.
2. Expression. print() behaves like a function in that you can do:
$ret = print "Hello World"; And $ret will be 1. That means that print
can be used as part of a more complex expression where echo cannot. An
example from the PHP Manual:
$b ? print "true" : print "false";
print is also part of the precedence table which it needs to be if it
is
to be used within a complex expression. It is just about at the bottom
of the precedence list though. Only "," AND, OR and XOR are lower.
3. Parameter(s). The grammar is: echo expression [, expression[,
expression] ... ] But echo ( expression, expression ) is not valid.
This would be valid: echo ("howdy"),("partner"); the same as: echo
"howdy","partner"; (Putting the brackets in that simple example
serves
no purpose since there is no operator precedence issue with a single
term like that.)
So, echo without parentheses can take multiple parameters, which get
concatenated:
echo "and a ", 1, 2, 3; // comma-separated without parentheses
echo ("and a 123"); // just one parameter with parentheses
print() can only take one parameter:
print ("and a 123");
print "and a 123";
source: http://www.forumnettr.com
http://www.forumnettr.com/forum
转自 http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40 |
2008-09-08 15:28
<?php
class a{
function a(){
if(version_compare(phpversion(),'5')<0)
{
register_shutdown_function(array(&$this,'__destruct'));
}
}
function __destruct(){
echo "执行了";
}
}
$a = new a();
sleep(5);
echo "test";
?> |
|
|