Jump to content

Recommended Posts

I have a page (forum.php) and on this page I have the following code:

 

<?php
$iframe = $_GET['iframe'];
echo $iframe;
  
?>

 

So when I go to forum.php?iframe=http://google.com, the page displays "http://google.com" as expected. The issue is, if I go to forum.php?iframe=http://somesite.com?a=1&b=2, then the forum.php page only displays "http://somesite.com?a=1".

 

I've also tried forum.php?iframe="http://somesite.com?a=1&b=2", but even with quotes, the $_GET['iframe'] variable gets truncated at the first "&" sign.

 

How can I get around this?

Link to comment
https://forums.phpfreaks.com/topic/110247-php-get-variable-truncated-at/
Share on other sites

Naturally, it gets 'truncated' at the ampersand, 'cause that's the query string delimiter. If you really need URLs with a query string AS a query string, you'll have to urlencode() the URL (i.e. you can't just type it in manually) and then urldecode() it again in your script:

 

<?php
$iframe = urldecode($_GET['iframe']);
echo $iframe;
  
?>

 

The URL to the script should look something like "forum.php?iframe=http%3A%2F%2Fsomesite.com%3Fa%3D1%26b%3D2" if you're going for "forum.php?iframe=http://somesite.com?a=1&b=2" in your script.

Actually, I've just thought out a neat workaround:

 

<?php
//save part of URL up until the first ampersand, and remove that entry from the GET array
$url = array_shift($_GET);
//loop through what's left of the GET array, and append the name and value pairs to the $url
foreach ($_GET as $key => $value) {
$url .= '&' . $key . (($value == '') ? '' : '=') . $value;
}
echo $url;
?>

 

It's important that the first name in the script's query string (you called it 'iframe') is unique - if it's found in the entered URL, the script will break. It will also break if you use additional name and value pairs in the script's query string (but this can also be worked around very simple :)).

 

I'm still recommending the method I initially posted, if that's possible in your scenario (e.g. if the URL is entered through a form).

Thanks for the reply. Unfortunately the referring page can't run PHP. However, your response got me going in the right direction. In the referring page, i used this JavaScript code:

var str1, str2;
str1 = location.href;
str2 = str1.replace ("&", "andandand");
top.location.href = "http://asurfc.com/forum.php?iframe=" + str2;

 

That just replaces "&" with "andandand". I used "andandand" to make sure I wouldn't have any problems if the URL already included a single "and".

 

Then on my forum.php code, I returned the URL with this:

<?php
$iframe = str_replace("andandand" , "&" , $_GET['iframe']);
if ($iframe == "")
{
$iframe = "./forum/";
}
  
?>

 

which just replaces the "andandand" with "&".

 

Thanks for the help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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