当前位置: 编程技术>php
本页文章导读:
▪php5.2.0内存管理改进
php5.2.0的内存管理做了较大的改进,某些情况下内存不能释放的问题不存在了。测试php脚本(mem.php),我用echo N>>和sleep来控制脚本在某个阶段暂停以便检测状态。 代码如下:<?php echo '1.........
▪php中截取字符串支持utf-8
截取字符串 <?php $string="2006年4月我又长大了一岁!"; echo substr($string,1)."..."; //截取字符串 function SubstrGB($in,$num) { $pos=0; $out=""; while($pos<strlen($in)) { $c=substr($in,$pos,1); if($c=.........
▪php中的登陆login
login <?php require "../include/DBClass.php"; $username=$_POST['UserName']; $password=$_POST['Password']; if(empty($username) || empty($password)){ Go_Msg("请填写用户名,密码!","../default.html"); exit; } DBConn(); $passwo.........
[1]php5.2.0内存管理改进
来源: 互联网 发布时间: 2013-11-30
php5.2.0的内存管理做了较大的改进,某些情况下内存不能释放的问题不存在了。
测试php脚本(mem.php),我用echo N>>和sleep来控制脚本在某个阶段暂停以便检测状态。
<?php
echo '1>>';
sleep(5);
$o = array();
for ($i=0;$i<=100000;$i++) {
$o[]='aaaaaaaaaaaaaaaaaaaaa';
}
echo '2>>';
sleep(5);
unset($o);
echo '3>>';
while (true) {
echo '..';
sleep(10);
}
?>
监测内存使用情况的bash script(注意:里面的”mem”是取自上面的php脚本名):
while true;do clear;ps au|grep -v "\(vi\|grep\)"|grep "\(mem\|RSS\)";sleep 2;done;
以下是$/usr/local/bin/php mem.php这个进程在三种状态(数组创建前,数组创建后,数组销毁后)下,用5.1.6和5.2.0的PHP(我使用了一样的configure参数)测试的RSS(内存使用值,单位KB)结果。
php5.1.6:
3164
18076
17572
PHP5.2.0:
4088
14400
4424
大家可以看到5.1.6这个版本,unset数组之后,内存并没有从进程里释放出来,虽然它是可以继续被这个php进程再利用,但是却无法被系统的其他进程所使用。而5.2.0则真正释放出了内存。
大家可能还注意到,在最开始,5.2.0的内存使用比5.1.6多了几个kb,这是由于5.2.0增加了一些新的东西,这个是很正常的。
另外php5.2.0的内存分配也进行了大的改进,官方的说法是对memory_limit的检测由每次呼叫emalloc()的时候进行改为直接检测从系统请求的内存数据块(blocks)。需要更进一步了解的朋友可以自己去研究代码。而由于内存分配的实现的更改,内存控制可以更精确地被控制在memory_limit之下,也就是说以前php代码里,如果有超过了memory_limit的内存使用而没有出错的,在php5.2.0里可能会报错。为了平衡这次改进,PHP5.2.0的默认的memory_limit由以前的8MB改为了16MB.搜索源代码可以看到这个修改(find . -name \*c -type f |xargs cat |grep memory_limit)。
测试php脚本(mem.php),我用echo N>>和sleep来控制脚本在某个阶段暂停以便检测状态。
代码如下:
<?php
echo '1>>';
sleep(5);
$o = array();
for ($i=0;$i<=100000;$i++) {
$o[]='aaaaaaaaaaaaaaaaaaaaa';
}
echo '2>>';
sleep(5);
unset($o);
echo '3>>';
while (true) {
echo '..';
sleep(10);
}
?>
while true;do clear;ps au|grep -v "\(vi\|grep\)"|grep "\(mem\|RSS\)";sleep 2;done;
以下是$/usr/local/bin/php mem.php这个进程在三种状态(数组创建前,数组创建后,数组销毁后)下,用5.1.6和5.2.0的PHP(我使用了一样的configure参数)测试的RSS(内存使用值,单位KB)结果。
php5.1.6:
3164
18076
17572
PHP5.2.0:
4088
14400
4424
大家可以看到5.1.6这个版本,unset数组之后,内存并没有从进程里释放出来,虽然它是可以继续被这个php进程再利用,但是却无法被系统的其他进程所使用。而5.2.0则真正释放出了内存。
大家可能还注意到,在最开始,5.2.0的内存使用比5.1.6多了几个kb,这是由于5.2.0增加了一些新的东西,这个是很正常的。
另外php5.2.0的内存分配也进行了大的改进,官方的说法是对memory_limit的检测由每次呼叫emalloc()的时候进行改为直接检测从系统请求的内存数据块(blocks)。需要更进一步了解的朋友可以自己去研究代码。而由于内存分配的实现的更改,内存控制可以更精确地被控制在memory_limit之下,也就是说以前php代码里,如果有超过了memory_limit的内存使用而没有出错的,在php5.2.0里可能会报错。为了平衡这次改进,PHP5.2.0的默认的memory_limit由以前的8MB改为了16MB.搜索源代码可以看到这个修改(find . -name \*c -type f |xargs cat |grep memory_limit)。
[2]php中截取字符串支持utf-8
来源: 互联网 发布时间: 2013-11-30
截取字符串
<?php
$string="2006年4月我又长大了一岁!";
echo substr($string,1)."...";
//截取字符串
function SubstrGB($in,$num)
{
$pos=0;
$out="";
while($pos<strlen($in))
{
$c=substr($in,$pos,1);
if($c=="\n") break;
if(ord($c)>128)
{
$out.=$c;
$pos++;
$c=substr($in,$pos,1);
$out.=$c;
}
else
{
$out.=$c;
}
$pos++;
if($pos>=$num) break;
}
return $out;
}
echo SubstrGB($string,8) ;
?>
<?php
/***************************************************************************
* cut_string.php
* ------------------------------
* Date : Jul 16, 2005
* Copyright : none
* Mail :
*
* 作用:截取中文字符.
*
*
***************************************************************************/
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
if($code == 'UTF-8')
{
$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i<$strlen; $i++)
{
if($i>=$start && $i<($start+$sublen))
{
if(ord(substr($string, $i, 1))>129) $tmpstr.= substr($string, $i, 2);
else $tmpstr.= substr($string, $i, 1);
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
echo "<br>".cut_str($string,8,$start=0,$code='sdf') ;
?>
<?php
$string="2006年4月我又长大了一岁!";
echo substr($string,1)."...";
//截取字符串
function SubstrGB($in,$num)
{
$pos=0;
$out="";
while($pos<strlen($in))
{
$c=substr($in,$pos,1);
if($c=="\n") break;
if(ord($c)>128)
{
$out.=$c;
$pos++;
$c=substr($in,$pos,1);
$out.=$c;
}
else
{
$out.=$c;
}
$pos++;
if($pos>=$num) break;
}
return $out;
}
echo SubstrGB($string,8) ;
?>
<?php
/***************************************************************************
* cut_string.php
* ------------------------------
* Date : Jul 16, 2005
* Copyright : none
* Mail :
*
* 作用:截取中文字符.
*
*
***************************************************************************/
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
if($code == 'UTF-8')
{
$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i<$strlen; $i++)
{
if($i>=$start && $i<($start+$sublen))
{
if(ord(substr($string, $i, 1))>129) $tmpstr.= substr($string, $i, 2);
else $tmpstr.= substr($string, $i, 1);
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
echo "<br>".cut_str($string,8,$start=0,$code='sdf') ;
?>
[3]php中的登陆login
来源: 互联网 发布时间: 2013-11-30
login
<?php
require "../include/DBClass.php";
$username=$_POST['UserName'];
$password=$_POST['Password'];
if(empty($username) || empty($password)){
Go_Msg("请填写用户名,密码!","../default.html");
exit;
}
DBConn();
$password=md5($password);
$SQL = "Select UserName,UserType,UserPass From siteuser where UserName='" . $username ."' and UserPass='" . $password . "' and isopen=1";
//echo "$SQL";
$result = $db -> exec_SQL($SQL);
$total=mysql_num_rows($result);
if($total==0){
DBClose();
Go_Msg("注册用户还没有通过管理员审核或用户名及密码不符!请重新输入!","../default.html");
exit;
}else{
$Rs = $db -> fetch_array($result);
//附值,并登录
session_register("username");
session_register("usertype");
$_SESSION["usertype"]=$Rs['UserType'];
$_SESSION["username"]=$username;
//echo $_SESSION["username"];
if ($_SESSION["usertype"]==1){
Go_Msg("登陆成功!","default.php");
//header('Location:http://163.com');
}
if ($_SESSION["usertype"]==2){
Go_Msg("登陆成功!","../user2/default_1.php");
}
if ($_SESSION["usertype"]==3){
Go_Msg("登陆成功!","../user3/default_2.php");
}
// header('Location:default.php');
}
DBClose();
?>
<?php
require "../include/DBClass.php";
$username=$_POST['UserName'];
$password=$_POST['Password'];
if(empty($username) || empty($password)){
Go_Msg("请填写用户名,密码!","../default.html");
exit;
}
DBConn();
$password=md5($password);
$SQL = "Select UserName,UserType,UserPass From siteuser where UserName='" . $username ."' and UserPass='" . $password . "' and isopen=1";
//echo "$SQL";
$result = $db -> exec_SQL($SQL);
$total=mysql_num_rows($result);
if($total==0){
DBClose();
Go_Msg("注册用户还没有通过管理员审核或用户名及密码不符!请重新输入!","../default.html");
exit;
}else{
$Rs = $db -> fetch_array($result);
//附值,并登录
session_register("username");
session_register("usertype");
$_SESSION["usertype"]=$Rs['UserType'];
$_SESSION["username"]=$username;
//echo $_SESSION["username"];
if ($_SESSION["usertype"]==1){
Go_Msg("登陆成功!","default.php");
//header('Location:http://163.com');
}
if ($_SESSION["usertype"]==2){
Go_Msg("登陆成功!","../user2/default_1.php");
}
if ($_SESSION["usertype"]==3){
Go_Msg("登陆成功!","../user3/default_2.php");
}
// header('Location:default.php');
}
DBClose();
?>
最新技术文章: