php解决session跨域,验证码无效问题。

isset($_GET['getcode'])

最近在项目中遇到.net域名下通过iframe加载.cn域名网站。注册的时候验证码失效了。其中也找到了办法,不过不兼容ie8及以下浏览器。一直反复测试。今天终于成功了。

对于上面的一句话的前半句解释下,估计有的同学搞不懂逻辑关系:访问 www.abc.net (A站) 可以看到www.abc.cn(B站) 的网站内容,从而进行注册。有的同学会问,干嘛这么费劲,域名解析到同一服务器不就完了。可是两个域名不是同一个人 买的,总之是不愿解析。

好,闲话不扯了。

任务:通过A站ifarme加载B站然后注册。

问题:保存在session中验证码丢失,一直提示验证码错误。

解题思路:

1.session,cookie都不用。那就在头部信息传递参数。把验证码信息写入header中。

2.注册页面获取验证码做特殊处理。img的src丢空,onclick也不要,加入id。写一个函数获取验证码然后把图片赋值给img

解决步骤:

1.生成验证码页:header('Code:abcdefg') ; 在图片输出前写

.注册页:

2.验证码input 和img 标签 

3.js代码:

autocode();//页面载入完成 
$("#img").bind("click",function(){ autocode() }) ; //每次点击
function autocode()
{//获取验证码
$.get('',{"action":"getcode"},function(data){
$("#img").attr('src',data)
});

}

4.当页PHP代码:


if( isset($_GET['action']) && $_GET['action']=='getcode' )
{
$url = 'http://www.abc.cn/a.jpg';
$img = file_get_contents($url);
$info = $http_response_header;

}








1.获取头部信息

$http_response_header — HTTP 响应头

说明$http_response_header 数组与 get_headers() 函数类似。当使用HTTP 包装器时,$http_response_header 将会被 HTTP 响应头信息填充。$http_response_header 将被创建于局部作用域中。


/*
//打印信息
array(9) {
  [0]=>
  string(15) "HTTP/1.1 200 OK"
  [1]=>
  string(35) "Date: Sat, 12 Apr 2008 17:30:38 GMT"
  [2]=>
  string(29) "Server: Apache/2.2.3 (CentOS)"
  [3]=>
  string(44) "Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT"
  [4]=>
  string(27) "ETag: "280100-1b6-80bfd280""
  [5]=>
  string(20) "Accept-Ranges: bytes"
  [6]=>
  string(19) "Content-Length: 438"
  [7]=>
  string(17) "Connection: close"
  [8]=>
  string(38) "Content-Type: text/html; charset=UTF-8"
}
NULL
*/
//头部信息解析函数
function parseHeaders( $headers )
{
    $head = array();
    foreach( $headers as $k=>$v )
    {
        $t = explode( ':', $v, 2 );
        if( isset( $t[1] ) )
            $head[ trim($t[0]) ] = trim( $t[1] );
        else
        {
            $head[] = $v;
            if( preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#",$v, $out ) )
                $head['reponse_code'] = intval($out[1]);
        }
    }
    return $head;
}

print_r(parseHeaders($http_response_header));

/*
Array
(
    [0] => HTTP/1.1 200 OK
    [reponse_code] => 200
    [Date] => Fri, 01 May 2015 12:56:09 GMT
    [Server] => Apache
    [X-Powered-By] => PHP/5.3.3-7+squeeze18
    [Set-Cookie] => PHPSESSID=ng25jekmlipl1smfscq7copdl3; path=/
    [Expires] => Thu, 19 Nov 1981 08:52:00 GMT
    [Cache-Control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    [Pragma] => no-cache
    [Vary] => Accept-Encoding
    [Content-Length] => 872
    [Connection] => close
    [Content-Type] => text/html
)
*/

你可能感兴趣的:(php,weather)