简体   繁体   中英

Accessing Parent global variable in iFrame url

I have following code Placed in my domain as thedomain.com/myapp/index.php

<?php
ini_set('display_errors',1); 
error_reporting(E_ERROR);

$storeId = 123;
?>

<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
</head>
<body>

<div class="wrapper">
<iframe src="thedomain.com/myapp/thurber/index.php" width="850" target="_parent" height="850" scrolling="no" style="overflow:hidden; border:none;" ></iframe>
</div>

</body>
</html>

In my thurber/index.php i want to access the parent variable $storeId

Is this possible? if so How can i achieve it?

You can pass the variable via $_GET:

<iframe src="thedomain.com/myapp/thurber/index.php?sid=<?php echo $storeId; ?>" width="850" target="_parent" height="850" scrolling="no" style="overflow:hidden; border:none;" ></iframe>

In thurber/index.php you can access it via $_GET['sid'] . Note that this can be manipulated and you pass only the value which is set when the echo is executed. If you change the value later, thurber/index.php still works with the old value.

EDIT (for multiple):

<iframe src="thedomain.com/myapp/thurber/index.php?sid=<?php echo $storeId; ?>&var_two=<?php echo $var_two; ?>" width="850" target="_parent" height="850" scrolling="no" style="overflow:hidden; border:none;" ></iframe>

Access via $_GET['var_two']

Sharing php variables trough an iframe is inpossible. However, using cURL you can pass variables and fetch the output from the other file to print it on your page.

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