Search code examples
phpjsonapiurlencodeurldecode

Premium url shortner issue with urlencode replacing & sign with ampersand


As a novice and beginner php learner, I'm using the Code-Canyon Premium URL Shortner script and done 2 days of research. Unfortunately I am unable to resolve my issue.

The url shorten script is urlencoding the API url that it sends to the script, In doing this it is replacing the & symbols with & causing the url to not work correctly on the final destination page.

I have tried to use preg_replace, str_replace and also tried to use urldecode on the destination page but none of these seem to work. Here is my current script:

$makeshort = "http://mywebsite.com/email/quote.php?quoteid=$visitor&customertype=fhbs";
$mkshrt = str_replace("/&/","%26",$makeshort);
$short = "http://shorturl.com/api?&api=REMOVED&format=text&url=".urlencode($mkshrt);

// Using Plain Text Response
$api_url = $short;
$res= @file_get_contents($api_url);
if($res)
$shorturl = $res;
$shorty = json_decode($shorturl);
$shorturl = $shorty->{'short'};
echo $shorturl;

Note: Where you see &format=text in the api url, I have tried to use it with and without the &format=text however this makes no difference what so ever.

I am hoping that there could be a simple and quick way to resolve this issue as I am only passing over 2 variables and its the second variable that is being displayed like this:

mywebsite.com/email/quote.php?quoteid=01234567890&customertype=fhbs

So the customertype variable is the one being messed up due to the amp; symbol.

I sincerely hope someone with the expertise could advise me on the best approach or even a simple way to resolve this issues as I really am at my whits end! MY knowledge is not good enough to research the exact key phrases in order to point myself in the right direction.

Thanks for your time in reading this and I hope someone would be kind enough to help me out here.


Solution

  • I know the feeling as i myself am just becoming to terms with coding and developing.

    I personally would solve this by one of two ways, If you have tried to already use htmlspecialchars or htmlentities along with urldecode then the most simple and quickest way to achieve this would be to read the URL string then replace the &symbol with the & using str_replace and do either a meta refresh of the page or `header location redirect

    Here is what i mean with a breif example however one must stress that some extra security maybe needed and this is ONLY a quick fix not a secure stable and permanent fix, Though one could play with this and maybe work something out for your own circumstances.

    $url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    if(strstr($url, "&")){
        $url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        $url = str_replace('&', '&', $url);
        echo "<meta http-equiv='refresh' content='0;URL=$url'>";
        exit;
    }
    

    Alternative way with header location:

    $url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    if(strstr($url, "&amp;")){
        $url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        $url = str_replace('&amp;', '&', $url);
        header("Location: $url");
        exit();
    }
    

    This will totally remove any &amp; symbols from the url and replace them with &. You can also play around with this to remove even more from the url string and replace things like / or forbidden words.

    An example of the output will look like this:

    Original url causing the problems:

    http://mywebsite.com/email/quote.php?quoteid=1234567890&amp;customertype=fhbs

    New url after the script has executed and refreshed the page:

    http://mywebsite.com/email/quote.php?quoteid=1234567890&customertype=fhbs

    As you can see from the hyperlinked text above, The ampersand breaks the string and everything after that is not read correctly but when this script executes and refreshes the page the url will be just like the second hyperlink thus making the url work for what you require.

    NOTE: THIS IS NOT A SECURE WAY OF DOING THINGS AND MAY NO BE IDEAL FOR YOUR CIRCUMSTANCES, THIS IS JUST AN IDEA AND HOPE THIS HELPS!

    Thanks.