2013-03-26 107 views
2

我的PHP技能缺乏一点,我尝试了一段时间来解决这个问题并寻找一个没有运气的答案......并且我确信它很简单。对多个域名之间的PHP URL重定向问题

我在一个域上有一个页面,用于检查cookie是否存在,如果不存在,它会将用户发送到一个完全独立的域以输入其出生日期。一旦他们进入并超过21岁,他们将被重定向回原始域,但由于某种原因,我的脚本只捕获引用域的子目录,而不是整个事情。

<?php 
function over21(){ 
    session_start(); 
    $redirect_url='http://xyz.com'; 
    $validated=false; 
    if(!empty($_COOKIE["over21"])) { $validated=true; } 
    if(!$validated && isset($_SESSION['over21'])) { $validated=true; } 
    if($validated) { return; } 
    else { 
     $redirect_url=$redirect_url."?return=".$_SERVER['REQUEST_URI']; 
     header('location: '.$redirect_url); 
     exit(0); 
    } 
} 
over21(); 
?> 

<html> 
    <head> 
    <title>abc.com page</title> 
    </head> 
    <body> 
    Before you are able to read this content, you will be redirected to xyz.com to validate your age 
    </body> 
</html> 

到目前为止好,他们现在发送到xyz.com在他们的出生日期输入:

所以,用户在一个假设的URL,abc.com访问以下网页

<?php 
session_start(); 
if(isset($_POST['submit'])) 
{ 
    $month = $_POST['month']; 
    $day = $_POST['day']; 
    $year = $_POST['year']; 

    $birthday = mktime(0,0,0,$month,$day,$year); 
    $difference = time() - $birthday; 
    $age = floor($difference - 662256000); 
    if($age >= 21) 
    { 
     setcookie("over21",$value, time()+3600*24); 
     $_SESSION['over21'] = 1; 
     $redirect=isset($_GET['return'])?urldecode($_GET['return']):'./'; 
     header("location: ".$redirect); 
     exit; 

    } 
    else 
    { 
     $_SESSION['under21'] = 0; 
     header("location: http://google.com"); 
     exit; 
    } 
} 
?> 
<html> 
    <head> 
    <title>this is xyz.com</title> 
    </head> 
    <body> 
    <form> 
     There is a form in here with input's and such to gather day, month and year of birth. 
    </form> 
    </body> 
</html> 

如果我将年龄验证部分和引用页面保留在同一个域中,该脚本工作得非常好。但是,我该如何修改它,以便年龄验证页面捕获引用域的完整URL,而不仅仅是子域?

+0

做两个网站共用一个域名?您是否熟悉[同源策略](http://en.wikipedia.org/wiki/Same_origin_policy)? – ficuscr 2013-03-26 19:10:22

回答

0

因此,我认为忽略相同的来源评论,只是看到会话开始代码让我觉得你试图跨域分享会话。

你只是问我看到$_SERVER variables

'REQUEST_URI' - 为访问此页面而提供的URI;比如'/index.html'。

改为尝试类似HTTP_HOST

类似: PHP get domain name

+0

谢谢@ficuscr,这有帮助。我修改了重定向引用页面,以便else语句是:$ redirect_url = $ redirect_url。“?return =”。$ _ SERVER ['HTTP_HOST'];但现在看来,引荐页面的完整网址正被追加到引荐页面的网址中,而不是重新引导回来。要看到这个动作,请访问http://rwd-development.com,它将重定向到rwd-development2.com(而不是重定向的URL字符串)。一旦你提交你的年龄,它应该把你送回rwd-development.com,但它只是附加现有的URL。有什么想法吗?再次感谢 – 2013-03-27 16:34:11

+0

起初我以为你在发布年龄验证表单时失去了GET值,但是我发现表单动作URL带有?return变量。认为你只需要完整地表示位置标题的URI,添加'http://'。例如。 'header(“Location:http://www.example.com/”);' – ficuscr 2013-03-27 17:02:13

+0

This works:http://rwd-development2.com/?return=http://yahoo.com检查rwd-development。 com网页代码,认为你正在做一个额外的重定向。使用像Fiddler的HTTPLiveHeaders这样的工具可以让你看到标题发送和任何302重定向 - 使用宝贵的工具。 – ficuscr 2013-03-27 17:08:51