user1581579
user1581579

Reputation: 341

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.

E.g 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? E.g http://google.com/&x=234&results=10

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

Thank you

Upvotes: 1

Views: 3326

Answers (1)

pmayer
pmayer

Reputation: 341

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

Upvotes: 4

Related Questions