来自 编程 2019-11-22 22:02 的文章
当前位置: 网上澳门金莎娱乐 > 编程 > 正文

php4的session功能评述_php基础_脚本之家【网上澳门金莎娱乐】

比较模糊的是session_decode和session_encode:bool session_decode;session_decode() decodes the session data in data, setting variablesstored in the session.bool session_encode;session_encode() returns a string with the contents of the currentsessionencoded within.php manual里面好象没有相关的例子。从字面意思来看decode是把用户的串解析出来以后set到session里面,encode则是把session的数据“打包”返回给用户。这样看来encode的作用可能更大一些,用户register数据到session里面以后应该是用encode把数据取出来的,但是encode返回的东西仍需要处理,比如:session_register;$val1 = "abcde"session_register;$val1 = 1234session_register;$val1 = 123.45那么session_encode返回的东西是:val1:s:5:"abcde";val2:i:1234;val3:d:123.45;很明显,'s'是string,'i'和'd'是数字,'s'类型的变量需要长度。如果你的session id是aaeebbcfd4455ec2c0d5cb590f8fab74,那么这串东西实际上是存在/tmp/aaeebbcfd4455ec2c0d5cb590f8fab74文件当中。现在需要你自己把这串东西做处理,析出你想要的session data。很奇怪php4没提供一个方便的接口实现已register变量的解析... 或者有我没找着。先自己写一个了...session_data_init取出所有“打包”的session_datasession_data_get根据变量名取得数据用:$data = session_data_init;$result = session_data_get($data, "val1");就可以把val1的数据取出来。/**网上澳门金莎娱乐, get all date registered in the session*/function session_data_init {$sessionData = session_encode();return $sessionData;}?>/** extract one variable from enconded session data*/function session_data_get {$matchStr = $name."|";$matchStart = strpos;if {if < strlen return "";$tmpStr = substr($data, 0, strlen;if (strcmp != 0) return "";}$typeStart = $matchStart + strlen;$dataType = substr;if (strcmp($dataType, "s") == 0) {/* string */$lenStart = $typeStart + 2;$lenEnd = strpos($data, ":", $lenStart) - 1;$lenLen = $lenEnd - $lenStart + 1;$strLen = substr($data, $lenStart, $lenLen);$strStart = $lenEnd + 3;$strResult = substr($data, $strStart, $strLen);return $strResult;} else if (strcmp($dataType, "i") == 0 ||strcmp($dataType, "d") == 0) { /* number */$numStart = $typeStart + 2;$numEnd = strpos($data, ";", $numStart) - 1;$numLen = numEnd - numStart + 1;$numResult = substr($data, $numStart, $numLen); return $numResult; } else { return ""; } } ?>

例子1

   // replace anything not alpha numeric
   $key = preg_replace('/[^a-z]/i', '', $key);

<?php

代码
// 类似 XPATH 的数组选择器
function xml_array_select( $arr, $arrpath )
{
    $arrpath = trim( $arrpath, '/' );
    if(!$arrpath) return $arr;
    $self = 'xml_array_select';
   
    $pos = strpos( $arrpath, '/' );
    $pos = $pos ? $pos : strlen($arrpath);
    $curpath = substr($arrpath, 0, $pos);
    $next = substr($arrpath, $pos);
   
    if(preg_match("/\[(\d+)\]$/",$curpath,$predicate))
    {
        $curpath = substr($curpath, 0, strpos($curpath,"[{$predicate[1]}]"));
        $result = $arr[$curpath][$predicate[1]];
    }else $result = $arr[$curpath];
   
    if( is_array($arr) && !array_key_exists($curpath, $arr) )
    {
        die( 'key is not exists:' . $curpath );
    }
   
    return $self($result, $next);
}

测试代码:test.php

<?php
include './ArrayToXML.php';
header('Content-Type:   text/xml');
$data=array("name"=>"zhangsan","sex"=>"0","address"=>array("sheng"=>"chongqing","shi"=>"nanchuan","zhen"=>"daguan"));
echo ArrayToXML::toXml($data);

   // if there is another array found recrusively call this function
   if (is_array($value))
   {
    $node = $xml->addChild($key);
    // recrusive call.
    ArrayToXML::toXml($value, $rootNodeName, $node);
   }
   else
   {
    // add single node.
    $value = htmlentities($value);
    $xml->addChild($key,$value);
   }

/**
 * 最简单的XML转数组
 * @param string $xmlstring XML字符串
 * @return array XML数组
 */
function simplest_xml_to_array($xmlstring) {
    return json_decode(json_encode((array) simplexml_load_string($xmlstring)), true);
}

// Xml 转 数组, 不包括根键
function xmltoarray( $xml )
{
    $arr = xml_to_array($xml);
    $key = array_keys($arr);
    return $arr[$key[0]];
}

本文由网上澳门金莎娱乐发布于编程,转载请注明出处:php4的session功能评述_php基础_脚本之家【网上澳门金莎娱乐】

关键词: