今天我们来实现一个提交话费的WEB程序,主要应用在:代理电信公司话费交纳。
第一步:获取登录页面的SESSION会话ID值.
为什么要获取SESSION值?
我们知道,用户访问一个网站时往往需要浏览许多网页。对于一个通过PHP构筑的网站来说,用户在访问的过程中需要执行许多的动态页面(如:jsp、PHP、APS.NET等)。然而由于HTTP协议自身的特点,用户每执行一个动态页面,都需要和Web服务器重新建立连接。
又由于WEB程序无状态记忆的特点,此次连接无法得到上次连接的状态。这样,用户在一个动态页面中对一个变量进行了赋值操作,而在另外一个动态页面中却无法得到这个变量的值。例如,用户在负责登录的动态页面中设置了string username = "wind",却无法在另一个动态页面中通过调用 username 来获得“wind”这个值。也就是说,在程序中无法设置全局变量。也就是说: 每个动态脚本中所定义的变量都是只在这个脚本内有效的局部变量。
Session解决方案,就是要提供在动态脚本中定义全局变量的方法,使得这个全局变量在同一个Session中对于所有的动态页面都有效。上面我们提到了,Session不是一个简单的时间概念,一个Session中还包括了特定的用户和服务器。因此更详细地讲,在一个Session定义的全局变量的作用范围,是指这个Session所对应的用户所访问的所有动态脚本。
又因为HTTP协议是一种无状态链接方式,也就是说服务端与客户端不是链接在一起的。HTTP是一个客户端和服务器端请求和应答的标准(TCP)。客户端是终端用户,服务器端是网站。通过使用Web浏览器、网络爬虫或者其它的工具,客户端发起一个到服务器上指定端口(默认端口为80)的HTTP请求。(我们称这个客户端)叫用户代理(user agent)。应答的服务器上存储着(一些)资源,比如HTML文件和图像。(我们称)这个应答服务器为源服务器(origin server)。
在用户代理和源服务器中间可能存在多个中间层,比如代理,网关,或者隧道(tunnels)。尽管TCP/IP协议是互联网上最流行的应用,HTTP协议并没有规定必须使用它和(基于)它支持的层。 事实上,HTTP可以在任何其他互联网协议上,或者在其他网络上实现。HTTP只假定(其下层协议提供)可靠的传输,任何能够提供这种保证的协议都可以被其使用。
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> /** * Post 方式请求网页数据 * * @param string $url 网页地址 * @prarm string $host 主机 * @param string $session 会话值 * @prarm string $type 类型(POST、GET) * @prarm string $port 端口 * @prarm string $data 数据 */ function getPageConent( $url, $host, $session = "", $type = "POST", $port = "", $data = "") { if( empty($port) ) $port = 80; /* 请求数据 */ $post_data = $data; $lenght = strlen($post_data); $headers = "{$type} {$url} HTTP/1.1\r\n"; $headers .= "Accept: * /*\r\n"; $headers .= "Content-Type: application/x-www-form-urlencoded\r\n"; $headers .= "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6; CIBA; .NET CLR 4.0.20506)\r\n"; if($session != "" ) $headers .= "Cookie:JSESSIONID={$session}\r\n"; $headers .= "Host: {$host}:{$port}\r\n"; $headers .= "Content-Length: {$lenght}\r\n"; $headers .= "Connection: Close\r\n\r\n"; $headers .= $post_data; if( $fp = fsockopen( $host, $port, $errno, $errstr, 100) ) { fwrite($fp, $headers); $header = fread($fp, 1024); $content = fread($fp, 1024); $content .= fread($fp, 1024); $content .= fread($fp, 1024); $content .= fread($fp, 1024); fclose($fp); } if( $data != "" ) { echo $headers; echo "<hr />"; echo $header; echo "<hr />"; echo $content; echo "<hr />"; exit; } else { return $content; } }
下面是获取网页代码的程序
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> /** * 获取URL地址内容 * * @param string $url 地址 * * @return mixed */ function getUrlContent($url) { $url_parsed = parse_url($url); $host = $url_parsed['host']; $port = $url_parsed['port']; /* Port */ if ( $port == 0 ) { $port = 80; } /* Path */ $path = $url_parsed['path']; if (empty($path)) { $path = "/"; } /* query */ if ( $url_parsed['query'] != "" ) { $path .= "?".$url_parsed['query']; } /* Open Page Content */ $out = "GET {$path} HTTP/1.0\r\nHost: {$host}\r\n\r\n"; if ($fp = @fsockopen( $host, $port, $errno, $errstr, 30 )) { fwrite($fp,$out); $header = fread($fp,1024); fclose($fp); return $header; } else { return false; } }
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> /** * HTTP 编码定义 */ if (!function_exists('http-chunked-decode')) { function http_chunked_decode($chunk) { $pos = 0; $len = strlen($chunk); $dechunk = null; while(($pos < $len) && ($chunkLenHex = substr($chunk,$pos, ($newlineAt = strpos($chunk,"\n",$pos+1))-$pos))) { if (!is_hex($chunkLenHex)) { trigger_error('Value is not properly chunk encoded', E_USER_WARNING); return $chunk; } $pos = $newlineAt + 1; $chunkLen = hexdec(rtrim($chunkLenHex,"\r\n")); $dechunk .= substr($chunk, $pos, $chunkLen); $pos = strpos($chunk, "\n", $pos + $chunkLen) + 1; } return $dechunk; } } /** * 判断一个字符串可以代表十六进制的一个电话号码 * * @param string $hex * @return boolean true if the string is a hex, otherwise false */ function is_hex($hex) { /* 正则表达式是思想 */ $hex = strtolower(trim(ltrim($hex,"0"))); if (empty($hex)) { $hex = 0; }; $dec = hexdec($hex); return ($hex == dechex($dec)); }
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> /** * 存储图片 */ function SaveImgage($content, $dir="img.png") { $content = http_chunked_decode($content); $con=fopen($dir, "w"); fwrite($con, $content); fclose($con); return $dir; }
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> /* 操作PHP代码 */ if( $_POST['Action'] == 'ok' ) { $txtpPhone = $_POST['txtpPhone']; $txtMoney = $_POST['txtMoney']; $txtType = $_POST['txtType']; /* post data */ $AGENTS_LOGIN_TYPE = "AG2"; $AG2_agentNumber = "*************"; $AG2_agentPassword = "*********"; $logon_valid = $_POST['txtcode']; $SessionId = $_POST['SessionId']; $LOGIN = ""; $postData = "LOGINS=LOGIN_IN&AGENTS_LOGIN_TYPE={$AGENTS_LOGIN_TYPE}&AG2_agentNumber={$AG2_agentNumber}&AG2_agentPassword={$AG2_agentPassword}&logon_valid={$logon_valid}&JSESSIONID={$SessionId}"; $img = getPageConent("/agent/login.jsp", "www.gz.ct10000.com", $SessionId, "POST", "80", $postData); echo $img;exit; } else { $str = getUrlContent("http://www.gz.ct10000.com/agent/index.jsp"); $preg = '/JSESSIONID=(.*);/isU'; preg_match_all($preg, $str, $match); $SessionId = $match[1][0]; $coding = getPageConent("/public/image.jsp", "www.gz.ct10000.com", $SessionId); $img = SaveImgage($coding); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>用户交话费</title> </head> <body> <form aaction="index.php" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1"> <input type="hidden" name="SessionId" id="SessionId" value="<?=$SessionId?>" /> <input type="hidden" name="Action" id="Action" value="ok" /> <span id="Label1">输入验证</span><input name="txtcode" type='text' id="txtcode" /><img src="img.png" alt="aa" /><br> <span id="Label1">电话号码</span><input name="txtpPhone" type="text" id="txtpPhone" /><br /> <span id="Label3">充值金额</span><input name="txtMoney" type="text" id="txtMoney" /><br /> <span id="Label2">电话类型</span><input name="txtType" type="text" id="txtType" /><br /> <input type="submit" name="Button1" value="提交" id="Button1" /> </form> </body> </html>