rtadams89 Posted June 14, 2008 Share Posted June 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/110247-php-get-variable-truncated-at/ Share on other sites More sharing options...
thebadbad Posted June 15, 2008 Share Posted June 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/110247-php-get-variable-truncated-at/#findComment-565713 Share on other sites More sharing options...
thebadbad Posted June 15, 2008 Share Posted June 15, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/110247-php-get-variable-truncated-at/#findComment-565728 Share on other sites More sharing options...
rtadams89 Posted June 15, 2008 Author Share Posted June 15, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/110247-php-get-variable-truncated-at/#findComment-565756 Share on other sites More sharing options...
Daniel0 Posted June 15, 2008 Share Posted June 15, 2008 You're better off doing: var str1, str2; str1 = location.href; str2 = str1.replace ("&", "%26"); top.location.href = "http://asurfc.com/forum.php?iframe=" + str2; And then no replacing in the PHP script. %26 resembles the ampersand. Quote Link to comment https://forums.phpfreaks.com/topic/110247-php-get-variable-truncated-at/#findComment-565864 Share on other sites More sharing options...
rtadams89 Posted June 15, 2008 Author Share Posted June 15, 2008 I tried that at first, but that wasn't working for me either. Not sure why, as I know it has before. Quote Link to comment https://forums.phpfreaks.com/topic/110247-php-get-variable-truncated-at/#findComment-566026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.