skb5525 Posted March 28, 2019 Share Posted March 28, 2019 I've inherited some PHP code that is trying to save a querystring to a variable: $from_url = ''; if ((isset($_GET['from'])) && ($_GET['from'] != '')) { $from_url = $_GET['from']; } The sample URL is: https://www.website.com/default.html?from=http://app.website.com/default.asp?utm_source=WelcomeEmail&utm_campaign=welcomeemailWhat the code saves to $from_url is:from=http://app.website.com/default.asp?utm_source=WelcomeEmailIt seems to stop at the "&" after the first querystring item and I understand why (because it parses the URL and runs into the next apostrophe "&" and assumes that it is not part of the "from" parameter).How do I need to tweak the PHP code to have it save the entire querystring?In other words, how do I take everything after ..."from=" (i.e. http://app.website.com/default.asp?utm_source=WelcomeEmail&utm_campaign=welcomeemail) and put it into a variable? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 28, 2019 Share Posted March 28, 2019 Execute the following. Isn't utm_source and utm_campaign defined? echo('<pre>'.print_r($_GET,1).'</pre>'); Also, try the following, and you might be able to use one of the values. echo('<pre>'.print_r($_SERVER,1).'</pre>'); PS. A shortcut for you if you are using PHP7. $from_url=$_GET['from']??null; Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 28, 2019 Share Posted March 28, 2019 (edited) The url in your posted example has two ? args. That is most likely the problem. Your script can't tell one url from the containing url so you will have to perhaps break apart the entire string by using an explode on the ? with a limit of 2 results. Then you can pull the 2nd one apart on the ? in the same way again and then on the &. The final result will have your "from" arg in the first element of this result. I think. Edited March 28, 2019 by ginerjm Quote Link to comment Share on other sites More sharing options...
maxxd Posted April 1, 2019 Share Posted April 1, 2019 Do you have control over the building of the link? Because there should be an ampersand before the 'from' variable (not the question mark), and the value of from should be url-encoded. I'm pretty sure this would take care of your issue. If you don't have control then honestly I'm not sure exactly how you'd fix the issue - maybe parse_url()? Not entirely sure what it does with slightly malformed URLs such as yours, but there's a possibility you can use this and ginerjm's explode() suggestion to figure it out, I guess. Quote Link to comment 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.