PHP模拟用户登陆代码

<?php

/*

 $url 访问地址

 $method请求post或get

 $post_data如果是post则需要写上发送的内容

 $sesskey模拟登陆的session_id名字

 */

function LoginWebContent($url, $method, $post_data, $sessid = '', $sesskey='EMP_GANJI_COM')

{ 

        $matches = parse_url($url);

    $host = $matches['host'];

    $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';

    $port = !empty($matches['port']) ? $matches['port'] : 80;



        $method = strtoupper($method);

        $fp=@fsockopen($host,$port); 

        if (!$fp) return;

        fputs($fp, "$method $path HTTP/1.0\r\n"); 

        fputs($fp, "Host: $host\r\n"); 

        if (!empty($sessid)) 

        { 

                fputs($fp, "Cookie: $sesskey=$sessid; path=/;\r\n"); 

        } 

        if ( $method == "POST") 

        { 

                fputs($fp, "Content-Length: ". strlen($post_data) . "\r\n"); //别忘了指定长度 

        }

        fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); 

        fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)\r\n)");

        fputs($fp, "Connection: Keep-Alive\r\n\r\n");

        if ( $method == "POST") 

        { 

                fputs($fp, $post_data."\r\n"); 

        } 

        while(!feof($fp)) 

        { 

                $response .= fgets($fp); 

        } 

        fclose($fp); 

        $hlen = strpos($response,"\r\n\r\n"); 

        $header = substr($response, 0, $hlen); 

        $entity = substr($response, $hlen + 4); 

        $return = array();

        if ( preg_match("/$sesskey=([0-9a-z]+);/i", $header, $matches)) 

        { 

                $return['sessid'] = $matches[1]; 

        } 

        $return['content'] = $entity;     

        return $return; 

}

上一篇: vc tab control 属性页   下一篇: linux多线程下载

提交疑问

回顶部