简体   繁体   中英

PHP - Get URL paramters of parent window from iframe

So far, I can place the first URL parameter and its value into variables - from child (iframed) page.

Eg http://google.com/?x=123 I can get x=123

I have used this code for the page (child) that is iframed.

<?php
   //Getting the parent window parameters
    $getURLVar = str_replace("?","",strrchr($_SERVER['HTTP_REFERER'],"?"));
    $getURLVar = str_replace("&","=",$getURLVar);
    $getURLVar = str_getcsv($getURLVar,"=");
    $i=0;
    foreach ($getURLVar as $value)
      {
        if ($i % 4)
            $value1[$i]=$value;

        else
            $value2[$i]=$value;


        $i++;
      } 
   // $getURLVar =array_combine($value2,$value1);


    //print_r($getURLVar);

list($v1, $v2) = $getURLVar;
 echo "$v1";

echo "$v2";


    ?>

Question: What if I had more than 1 parameter? Eg http://google.com/&x=234&results=10

Please help me amend the above to support more than 1 parameter.

Thank you

Use http://www.php.net/manual/function.parse-str.php and http://www.php.net/manual/function.parse-url.php to parse the HTTP_REFERER.

//edit:

$referer = 'http://www.google.de/?q=test&foo=bar&blub=bla';

$url = parse_url($referer);

print_r($url);

if(!empty($url['query'])){
    parse_str($url['query'], $query_params);
    print_r($query_params);
}

Maybe you should read something more about the basics...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM