<?php
/*
* 根据预定义的定界符分解字符串:explode()函数
* 次函数将字符串str分成子串数组,其形式为:
* array explode(string separator,string str [, int limit])
* 原字符串被根据separator指定的字符串分割为不同的元素。元素的数量可以通过可选的参数limit来限制。可以结合explode()/sizeof()和strip_tags()来确定给定文本块中单词的总数
* 如下所示:
*/
$summary="
In the latest installment of the ongoing Developer.com PHP series.
I discuss the many improvements and addtions to
<a href=\"http:www.php.com\">PHP</a> object-oriented architecture.
";
echo "<br/>";
$words=explode(' ', strip_tags($summary));
echo "This sentence's lenght is:".sizeof($words);
/*
* explode()函数始终比preg_split、spilt()和spliti()快得多。因此,在不需要使用正则表达式时,一定要使用这个函数。
*/
?>
<?php
/*
* 将数组转换成字符串
* explode()函数可以根据界定字符将字符串转换为相应的数组,但是可以通过implode()函数将数组转换为规定的界定字符为界限的字符串
* 其形式为:
* string implode(string delimiter,array pieces)
* 如下所示:
*/
$citys=array("Chengdu","Chongqing","Beijing","Shanghai","Guangzhou");
$citystring=implode("|", $citys);
echo $citystring;
?>
用mysql_real_escape_string()过滤用户输入字串
$user = mysql_real_escape_string($user)
;
$pwd = mysql_real_escape_string($pwd)
;
$sql = "SELECT * FROM users WHERE
user='" . $user . "' AND password='" . $pwd . "'";
用header()返回浏览器一个文件,在发送http头部给浏览器前不能有任何内容发往浏览器,否则header()调用不成功。
<php?
header("Content-Type: image/jpeg\n");
header("Content-Disposition: inline; filename=\"$name\"\n");//如果让浏览器下载,inline改为attachment
header("Content-Length: $filesize\n");
?>
<?php
在使用session的过程中,下面方法可以一定程度上防止会话劫持:
1、在登入的验证界面(比如login.php)设置一个session变量
if(/*用户密码验证通过*/)
{ $_SESSION['agent']=md5($_SERVER['HTTP_USER_AGENT']);
}
2、然后登录成功后转跳到的页面(比如logined.php)校验此变量。
if(!isset($_SESSION['agent']) OR ($_SESSION['agent']!=md5($_SERVER['HTTP_USER_AGENT'])))
{ $url="http://hostname/index.php";
header("Location: $url");//没有登录成功,转跳回首页
exit();
}
?>