用ie访问的时候,你的浏览器会纪录下和查找到你的sessionId,通过这个sessionId,再定位服务器上的sessionId文件.fopen(url)无法纪录这个东西的,所以不行.

解决方案 »

  1.   

    fopen是文件操作函数,当然不会发送session的id你的需求需要用sock函数完成,包括读取对方表单
    参考代码
    <?php 
    /*  
    * 得到网页内容  
    * 参数:$host [in] string 
    *      主机名称(例如: www.imsorry.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)) 
        { 
            echo "PHPSESSID=$sessid<br>"; 
            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; 
    } $localhost = www.imsorry.com.cn';
    $page = 'test.php';
    /* 构造用户名,密码字符串 */ 
    $str = ("username=test&password=test"); 
    $response = GetWebContent($localhost,"GET $page HTTP/1.0",''); 
    echo $response['location'].$response['content']."<br>"; 
    echo $response['sessid']."<br>"; 
    print_r($response);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 $page HTTP/1.0", '', $response['sessid']); 
        echo $response['location']."<br>"; // 结果:user.php 
    print_r($response);
        $response = GetWebContent($localhost,"GET $page HTTP/1.0", '', $response['sessid']); 
        echo $response['location']."<br>"; // 结果:user.php 
    print_r($response);
    } ?> 
      

  2.   

    随便打开一页。
    1 从COOKIE中提取 SESSIONID
    2 从URL链接中提取SESSIONID