dongpan1416 2013-03-26 18:58
浏览 33

在多个域之间存在PHP URL重定向问题

My PHP skills are lacking a bit and I've tried troubleshooting this for a while and searching for an answer with no luck... and I'm sure it's something simple.

I have a page on one domain that is checking to see if a cookie exists, and if it does not, it sends the user to a completely separate domain to enter in their Date of Birth. Once they enter it in and are over 21, they are to be redirected back to the original domain, but for some reason my script is only capturing the sub-directory of the referring domain, rather than the whole thing.

So, user visits the following page at a hypothetical URL, abc.com:

<?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>

So far so good, they are now sent to xyz.com to enter in their date of birth:

<?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>

The script is working great if I keep the age verification piece and the referring page all on the same domain. But how do I modify this so that the age verification page is capturing the full URL of the referring domain, not just the sub-domain?

  • 写回答

1条回答 默认 最新

  • dongwei1895 2013-03-26 19:16
    关注

    So, ignore the same origin remark I think, Just seeing the session start code made me think you were trying to share a session across domains.

    You are simply asking about $_SERVER variables I see.

    'REQUEST_URI' - The URI which was given in order to access this page; for instance, '/index.html'.

    Try something like HTTP_HOST instead.

    Similar: PHP get domain name

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看