请教一个关于php的问题.
由于公司接了一个业务. 给一个网站作注册推广.由于对方不能提供任何东西.我只能自己写一个统计,以服务器作一个转发.
现遇到一个关于验证码的问题..
验证码地址: http://www.sss988.com/captcha/index
注册页面地址: http://www.sss988.com/zh-cn/home/register
我这边现在是直接打开难码页面没任何输出.而在注册页面可以正常显示验证码. 从注册页面再打开的单独难码页面也可以正常显示.但一旦新开一个窗口或选项卡. 就不输出任何内容..
之前是正常的.我用php后台给作了个转接. 用fsockopen 打开这个地址获取内容及cookie,发给浏览器.之前都好好的. 最近我发送请求一直返回302..很郁闷..
我这边的验证码页面: v.php
<?php
include_once('common.inc.php');
if(function_exists('date_default_timezone_set')) date_default_timezone_set('Etc/GMT-8');
$_cookies = array();
$_cookies['affcode'] = '220143';
$header = get_HeaderInfo('www.sss988.com','GET','/captcha/index',array(),$_cookies);
$data = get_SendData('www.sss988.com',80,$header);
header("Content-type: image/gif");
echo $data['data'];
common.inc.php 内容
<?php
if(!function_exists('fsockopen')){
function _fsockopen($host, $port, &$errno, &$errstr, $timeout){
$ip = @gethostbyname($host);
$s = @socket_create(AF_INET, SOCK_STREAM, 0);
if(socket_set_nonblock($s)){
$r = @socket_connect($s, $ip, $port);
if ($r || socket_last_error() == EINPROGRESS) {
$errno = EINPROGRESS;
return $s;
}
}
$errno = socket_last_error($s);
$errstr = socket_strerror($errno);
socket_close($s);
return false;
}
}
function get_ip(){
isset($_SERVER['HTTP_X_FORWARDED_FOR']) or $_SERVER['HTTP_X_FORWARDED_FOR'] = '';
isset($_SERVER['REMOTE_ADDR']) or $_SERVER['REMOTE_ADDR'] = '';
isset($_SERVER['HTTP_CLIENT_IP']) or $_SERVER['HTTP_CLIENT_IP'] = '';
if($_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['REMOTE_ADDR']) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if(strpos($ip, ',') !== false) {
$tmp = explode(',', $ip);
$ip = trim(end($tmp));
}
if(is_ip($ip)) return $ip;
}
if(is_ip($_SERVER['HTTP_CLIENT_IP'])) return $_SERVER['HTTP_CLIENT_IP'];
if(is_ip($_SERVER['REMOTE_ADDR'])) return $_SERVER['REMOTE_ADDR'];
return 'unknown';
}
function is_ip($ip) {
return preg_match("/^([0-9]{1,3}\.){3}[0-9]{1,3}$/", $ip);
}
function get_HeaderInfo($host,$type='GET',$file='/',$params=array(),$cookies=array()){
$_params = $_cookies = '';
foreach($params as $key=>$value){
$_params .= "&".$key."=".urlencode($value);
}
$_params = (strlen($_params) > 1) ? substr($_params,1) : '';
foreach($cookies as $key=>$value){
$_cookies .= "; ".$key."=".urlencode($value);
}
$_cookies = (strlen($_cookies) > 2) ? substr($_cookies,2) : '';
$file .= ($type == 'GET') ? (($_params == '') ? '' : '?'.$_params) : '';
$header = $type." ".$file." HTTP/1.1\r\n";
$header .= "Host: ".$host."\r\n";
$header .= "Referer: ".get_ip()."\r\n";
//$header .= "Referer: http://www.sss988.com/zh-cn/home/register\r\n";
$header .= "X-Forwarded-For: ".get_ip()."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($_params)."\r\n";
$header .= "Cookie: ".$_cookies."\r\n";
$heak_ary = array('HTTP_HOST','HTTP_REFERER','HTTP_X_REWRITE_URL','HTTP_ORIGIN','HTTP_CONTENT_LENGTH');
foreach($_SERVER as $name => $value){
if(substr($name, 0, 5) == 'HTTP_' && !in_array($name, $heak_ary) && $value != ''){
$header .= str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))).":".$value."\r\n";
}
}
$header .= "Connection: Close\r\n\r\n";
$header .= $_params."\r\n";
return $header;
}
function _setCookie($cookie){
$_cookies = explode("; ",$cookie);
$_tmp = explode("=",$_cookies[0]);
setcookie($_tmp[0], $_tmp[1]);
return $_cookies;
}
function _getDataHeader(&$fp,&$reHeader,&$cookies){
$maxlen = 0;
while(!feof($fp)){
$line = fgets($fp,1024);
if(substr($line, 0, 12) == 'Set-Cookie: '){ $cookies[] = _setCookie(substr($line, 12)); }
$reHeader .= $line;
if(substr($line, 0, 16) == 'Content-Length: '){
$maxlen = intval(substr($line, 16, -2));
}
if($line == "\r\n" || $line == "") break;
}
return $maxlen;
}
function _getDataBody(&$fp,$maxlen){
$reData = "";
while(!feof($fp)){
$line = fgets($fp,$maxlen+1);
$reData .= $line;
if(strlen($line) < $maxlen) $maxlen = $maxlen - strlen($line);
else break;
}
return $reData;
}
function debug_obj($debug_starttime=null){
$mtime = explode(' ', microtime());
if($debug_starttime == null){
return $mtime[1] + $mtime[0];
}else{
return number_format(($mtime[1] + $mtime[0] - $debug_starttime), 3);
}
}
function get_SendData($host,$port=80,$header='',$_time=10){
$debug_starttime = debug_obj();
if(function_exists('fsockopen')){
$fp = fsockopen($host,$port,$errno,$errstr,$_time);
}else if(function_exists('pfsockopen')){
$fp = pfsockopen($host,$port,$errno,$errstr,$_time);
}else if(function_exists('stream_socket_client')){
$fp = stream_socket_client($host.':'.$port,$errno,$errstr,_time);
}else{
$fp = _fsockopen($host,$port,$errno,$errstr,_time);
}
//$fp = fsockopen($host,$port,$errno,$errstr,_time);
if(!$fp) return array('header'=>'','data'=>'error...->'.$errstr.'--->'.$errno,'cookie'=>'','time'=>debug_obj($debug_starttime));
$reHeader = $reData = "";
$cookies = array();
fputs($fp,$header);
$maxlen = _getDataHeader($fp,$reHeader,$cookies);
$reData = _getDataBody($fp,$maxlen);
fclose($fp);
return array('header'=>$reHeader,'data'=>$reData,'cookie'=>$cookies,'time'=>debug_obj($debug_starttime));
}
function _log($msg,$logfile='',$exit=false){
$logfile = ($logfile == '') ? './log/'.date('Y-m-d').'.log' : $logfile;
dir_create(dirname($logfile));
$data = file_exists($logfile) ? file_get_contents($logfile)."\r\n\r\n-----".date('Y-m-d H:i:s')."-----\r\n".$msg : "-----".date('Y-m-d H:i:s')."-----\r\n".$msg;
if(@$fp = fopen($logfile, 'wb')) {
flock($fp, LOCK_EX);
$len = fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
}
if($exit) exit($msg);
}
function dir_create($path) {
if(is_dir($path)) return true;
if(!file_exists($path)){
dir_create(dirname($path));
mkdir($path);
}
return is_dir($path);
}
function initSQLITE($dbfile='i.db'){
$database = new SQLiteDatabase($dbfile, 0666, $error);
if (!$database) {
_log('-500'.$error);
}
$check_tbc = $database->query("SELECT * FROM sqlite_master WHERE type='table' AND name='i'", SQLITE_ASSOC, $check_tbc_error);
if($check_tbc->numRows() == 0){
$create_Table_sql = "CREATE TABLE i ('id' INTEGER PRIMARY KEY NOT NULL,'ip' TEXT,'data' TEXT,'time' TEXT,'other' TEXT)";
$database->query($create_Table_sql, SQLITE_ASSOC, $insert_error);
if ($insert_error){
_log('-501'.$insert_error);
}
}
return $database;
}
function saveDataInSQLITE($data=array()){
$_post = array();
$_post['data'] = $data;
$_post['post'] = $_POST;
$_post['other'] = $_SERVER;
$_other = isset($data['data']) ? $data['data'] : '';
$insert_newInfo_sql = "INSERT INTO i(id,ip,data,time,other) VALUES (NULL,'".get_ip()."','".serialize($_post)."','".date('Y-m-d H:i:s')."','".$_other."')";
execSqlInSQLITE($insert_newInfo_sql);
}
function selectDataInSQLITE($where='',$pid=1,$pc=10){
$where = $where == '' ? '' : ' WHERE '.$where;
$sql = "SELECT * FROM i ".$where." LIMIT ".(($pc*$pid)-$pc).",".$pc;
return execSqlInSQLITE($sql);
}
function execSqlInSQLITE($sql=''){
if($sql == ''){
_log('SQL Error. SQL In Null.');
return null;
}else{
$database = initSQLITE();
if(!$database){
_log('database Failure!');
return null;
}else{
$query = $database->query($sql, SQLITE_ASSOC, $query_error);
if ($query_error){
_log("Exec SQL Failure! Error: [".$sql."] $query_error");
return null;
} else if (!$query){
_log("Exec SQL Failure! [".$sql."]");
return null;
}else{
$reData = array();
while($row = $query->fetch()){
$reData[] = $row;
}
return $reData;
}
}
}
}