Jump to content

IFrames and PHP


Cless

Recommended Posts

I need to create an iFrame, however, I need to make the PHP executed before it be global. For example:

 

<?php

$hi="blah";

?>

<iframe src='frame.php' width='100' height='350'>

 

Then, in "frame.php", the iFrame I am calling, I would want to be able to use the variable I defined before it ($hi). Not really a very practical example, but yeah.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/155121-iframes-and-php/
Share on other sites

You cannot do that with PHP. The variable would have to be passed to the frame.php via Javascript or GET.

 

This would allow you to use $_GET['hi'] to get that variable:

 

<?php

$hi = url_encode("blah");
$another = url_encode("hello bob");

?>

<iframe src='frame.php?hi=<?php echo $hi; ?>&another=<?php echo $another; ?>' width='100' height='350'>

 

url_encode ensures that the string is passed correctly. Then in the frame.php script:

 

<?php
$hi = isset($_GET['hi'])?$_GET['hi']:null;
$another = isset($_GET['another'])?$_GET['another']:null;

if (!is_null($hi)) 
    echo "You passed in {$hi} for the hi.";

if (!is_null($another))
    echo "You passed in {$another} for the another.";
?>

 

A very simple example, but should get the point across.

Link to comment
https://forums.phpfreaks.com/topic/155121-iframes-and-php/#findComment-815993
Share on other sites

Would it be possible to use the $_SESSION var or would it not be guaranteed to be set by the time the iframe was called??

 

I think it would be set. As long as they had session_start() at the top of each page. There is no reason for it not to as PHP is done on the server not the client. The guarantee would actually be 100% certain due to that fact.

Link to comment
https://forums.phpfreaks.com/topic/155121-iframes-and-php/#findComment-816039
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.