当前位置:编程学习 > php >>

php 如何记录临时参数

在一个php分页,判断用户$_GET页码是否合法时,必须记录合法操作的参数,如果用户打开了一个不合法的参数,返回上一个合法的参数,怎么记录临时参数,除了cookie和session还有什么方法?

小例子:
<?php

if(isset($_GET['var']) && !empty($_GET['var'])){

echo('Value:'.$_GET['var']."<br>");
//此时记录$_GET['var']的值,
}else{
echo("NULL value or empty");
//此时返回刚才的$_GET['var']的值

}

?>

有什么函数可以存储临时变量?不能使用cookie和session。

追问:php缓存技术,但是必须要有插件支持,php手册里有apc
答案:
可以用隐藏的表单
我没发现哪儿储存了变量的值,你的方法是输出..面向对象可以保存在成员里

 <?php
/*****************函数部分**************************/
/*获取指定网页的内容
$url为网页地址
*/
function getcontent($url){
if($open=file($url)){
   $count=count($open);
    for($i=0;$i<$count;$i++)
    {
     $theget.=$open[$i];
     }
  }else{
  die('请求过多,超时,请刷新');
  }
return $theget;
}
?>
2,偷取程序部分,也分两部分,
1),PHP与XML不同之处是需要特殊的调用才能支持COOKIE.或者记录SessionID(后面有说明程序)

php代码如下
<?PHP
//登陆并保存COOKIE
$f = fsockopen("www.url.net",80);
$cmd = <<<EOT
GET /test/login.php?name=test&password=test HTTP/1.0


EOT;
fputs($f,$cmd);
$result = '';
$cookie = '';
$location = '';
while($line = fgets($f))
{
$result .= $line;
//取得location跟setCookie头HTTP头信息
$tmp = explode(":",$line);
if($tmp[0]=="Set-Cookie")
$cookie .= $tmp[1];
if($tmp[0]=="Location")
$location = $tmp[1];
}
fclose($f);
2),获取页面
//下面访问你要访问的页面(这部分也可以参考下面的核心例程)
$f = fsockopen("www.url.net",80);l
//下面的cookie就是发送前页保存下的的cookie
$cmd = <<<EOT
GET /test/test.php HTTP/1.0
cookie:$cookie


EOT;
fputs($f,$cmd);
while($line = fgets($f))
{
echo $line;
}
fclose($f);
?>

核心例程就是fsockopen();
不妨再给段代码你瞧瞧:
--------------------------------------------------------------------------------

function posttohost($url, $data)
{
$url = parse_url($url);
if (!$url) return "couldn't parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url['query'])) { $url['query'] = ""; }
$encoded = "";
while (list($k,$v) = each($data))
{
$encoded .= ($encoded ? "&" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($v);
}
$fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
if (!$fp) return "Failed to open socket to $url[host]";
fputs($fp, sprintf("POST %s%s%s HTTP/1.0", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]");
fputs($fp, "Content-type: application/x-www-form-urlencoded");
fputs($fp, "Content-length: " . strlen($encoded) . "");
fputs($fp, "Connection: close");
fputs($fp, "$encoded");
$line = fgets($fp,1024);
if (!eregi("^HTTP/1\\.. 200", $line)) return $line ;
$results = ""; $inheader = 1;
while(!feof($fp))
{
$line = fgets($fp,1024);
if ($inheader && ($line == "" || $line == "\r")) {
$inheader = 0;
}
elseif (!$inheader) {
$results .= $line;
}
}
fclose($fp);
return $results;
}
$data=array();
$data["msg"]="HELLO THIS IS TEST MSG";
$data["Type"]="TEXT";
echo posttohost("Http://url/xxx", $data);

应该说明白了吧?
另外登陆部分还有一种简单方法是把SessionID保存下来

  源代码:

<?php
/* 
* 得到网页内容 
* 参数:$host [in] string
* 主机名称(例如: www.url.com.cn)
* 参数:$method [in] string
*      提交方法:POST, GET, HEAD ... 并加上相应的参数( 具体语法参见 RFC1945,RFC2068 )
* 参数:$str [in] string
*      提交的内容
* 参数:$sessid [in] string
*      PHP的SESSIONID
*
* @返回 网页内容 string
*/
function GetWebContent($host, $method, $str, $sessid = '')
{
    $ip = gethostbyname($host);
    $fp = fsockopen($ip, 80);
    if (!$fp) return;
    fputs($fp, "$method\r\n");
    fputs($fp, "Host: $host\r\n");
    if (!empty($sessid))
    {
        fputs($fp, "Cookie: PHPSESSID=$sessid; path=/;\r\n");
    }
    if ( substr(trim($method),0, 4) == "POST")
    {
        fputs($fp, "Content-Length: ". strlen($str) . "\r\n"); //  别忘了指定长度
    }
    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n\r\n");
    if ( substr(trim($method),0, 4) == "POST")
    {
        fputs($fp, $str."\r\n");
    }
    while(!feof($fp))
    {
        $response .= fgets($fp, 1024);
    }
    $hlen = strpos($response,"\r\n\r\n"); // LINUX下是 "\n\n"
    $header = substr($response, 0, $hlen);
    $entity = substr($response, $hlen + 4);
    if ( preg_match('/PHPSESSID=([0-9a-z]+);/i', $header, $matches))
    {
        $a['sessid'] = $matches[1];
    }
    if ( preg_match('/Location: ([0-9a-z\_\?\=\&\#\.]+)/i', $header, $matches))
    {
        $a['location'] = $matches[1];
    }
    $a['content'] = $entity;    
    fclose($fp);
    return $a;
}

/* 构造用户名,密码字符串 */
$str = ("username=test&password=test");
$response = GetWebContent("localhost","POST /login.php HTTP/1.0", $str);
echo $response['location'].$response['content']."<br>";
echo $response['sessid']."<br>";
if ( preg_match('/error\.php/i',$response['location']))
{
    echo "登陆失败<br>";
} else {
    echo "登陆成功<br>";
    // 不可以访问user.php,因为不带sessid参数
    $response = GetWebContent("localhost","GET /user.php HTTP/1.0", '', '');
    echo $response['location']."<br>"; // 结果:error.php?errcode=2

    // 可以访问user.php
    $response = GetWebContent("localhost","GET /user.php HTTP/1.0", '', $response['sessid']);
    echo $response['location']."<br>"; // 结果:user.php
}
?>

上一个:php批量处理求助
下一个:php怎样读取文件

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,