I have a url that gets encoded when serialized with jquery and comes out like this:
index.php?city%5B%5D=METROPLOIS&city%5B%5D=GOTHAM
Supposed to be index.php?city[]=METROPLOIS&city[]=GOTHAM
On the other end, I'm using joomla to pull down a bunch of variables out of the url
$city = JRequest::getVar('city');
Now, another user was nice enough to point me to this code in PHP which would get the city variables and then implode them into
$cities = $_GET['city'];
$str = "CITY='".implode("' OR CITY='",$city)."'";
Where $cities = $_GET['city']; is equivalent to $city = JRequest::getVar('city');
So, I'm running into a challenge that I don't know how to address. How to decode the URL in Joomla so that it will recognize city%5B%5D as city[] or city.
I've seen this: http://php.net/manual/en/function.urldecode.php, but that pulls down the URL and puts it into a string. How would I then tell Joomla to pull variables form that string instead of the URL ?
Use urldecode to decode the url
$decoded = urldecode('index.php?city%5B%5D=METROPLOIS&city%5B%5D=GOTHAM');
$query = parse_url($decoded, PHP_URL_QUERY);
parse_str($query, $params);
$city = $params['city'];
echo "CITY='".implode("' OR CITY='",$city)."'";
DEMO
http://php.net/manual/en/function.parse-url.php
http://php.net/manual/en/function.parse-str.php
Related
I've looked at every post on SO that remotely pertains to this and I just can't figure this out. This code is taken directly from another SO post and was marked as the correct working answer:
$query = $_GET;
// replace parameter(s)
$query['d'] = 'new_value';
// rebuild url
$query_result = http_build_query($query);
// new link
Link
Again, taken straight from another post. When I try this code, i change the $_GET to the actual URL that i want to alter. When the code gets to the $query['d'] part, it tells me I get an illegal string offset and the error is the index that's specified. So then I parse the URL, and then do parse_str($query, $output) which in turn allows me to do $output['d'] and THEN I can set a new value to that variable. If I echo it out, it's fine.
But then I get to the http_build_query line, and it tells me that it's expecting an array or object and I can't build the new URL. Here is my code:
$link = parse_url('https://www.google.com/search?source=hp&ei=85GhW6CNHoSqsgXnzoD4Ag&q=coding+tutorial&btnK=Google+Search&oq=coding+tutorial', PHP_URL_QUERY);
parse_str($link, $output);
$output['oq'] = 'new value';
$query_result = http_build_query($link);
echo $query_result;
This code yields that the http_build_query function wants an array or object...i guess i'm not giving it that in some way? What do I need to do to get this to work?
If you want to rebuild the full URL after modifying the query parameters, you could do this:
$url = 'https://www.google.com/search?source=hp&ei=85GhW6CNHoSqsgXnzoD4Ag&q=coding+tutorial&btnK=Google+Search&oq=coding+tutorial';
$link = parse_url($url, PHP_URL_QUERY);
parse_str($link, $output);
$output['oq'] = 'new value';
echo substr($url, 0, strpos($url, '?') + 1) . http_build_query($output);
Output:
https://www.google.com/search?source=hp&ei=85GhW6CNHoSqsgXnzoD4Ag&q=coding+tutorial&btnK=Google+Search&oq=new+value
I am trying to parse url and extract value from it .My url value is www.mysite.com/register/?referredby=admin. I want to get value admin from this url. For this, I have written following code. Its giving me value referredby=admin, but I want only admin as value. How Can I achieve this? Below is my code:
<?php
$url = $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
setcookie('ref_by', parse_url($url, PHP_URL_QUERY));
echo $_COOKIE['ref_by'];
?>
You can use parse_str() function.
$url = "www.mysite.com/register/?email=admin";
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];
Try this code,
$url = "www.mysite.com/register/?referredby=admin";
$parse = parse_url($url, PHP_URL_QUERY);
parse_str($parse, $output);
echo $output['referredby'];
$referred = $_GET['referredby'];
$referred = "referredby=admin";
$pieces = explode("=", $referred);
echo $pieces[1]; // admin
I don't know if it's still relevant for you, but maybe it is for others: I've recently released a composer package for parsing urls (https://www.crwlr.software/packages/url). Using that library you can do it like this:
$url = 'https://www.example.com/register/?referredby=admin';
$query = Crwlr\Url\Url::parse($url)->queryArray();
echo $query['referredby'];
The parse method parses the url and returns an object, the queryArray method returns the url query as array.
Is not a really clean solution, but you can try something like:
$url = "MYURL";
$parse = parse_url($url);
parse_str($parse['query']);
echo $referredby; // same name of the get param (see parse_str doc)
PHP.net: Warning
Using this function without the result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.
Dynamically setting variables in function's scope suffers from exactly same problems as register_globals.
Read section on security of Using Register Globals explaining why it is dangerous.
I have url like this
index.php?name=aya&age=29
index.php?name:aya&age:29
I want get name and age from this url.
I think if i can get query string, perhaps i can render it.
How can do it?
first use $_SERVER['QUERY_STRING'] to get querystring from url
and then get data from string via parse_str function.
code:
$str = $_SERVER['QUERY_STRING'];
$str = str_replace(":","=",$str);
parse_str($str, $get);
echo $get['name'];
echo $get['age'];
i want to fetch youtube videos from the above script but the above code is getting keyword from GET parameter example.com/s=keyword and i want it to get from a example.com/HERE
i mean you can see there is a $_GET['s']
So this function works like this
example.com/s=keyword
and i want it to work like this
example/page/keyword
sorry for my bad english
$keyword = $_GET['s'];
file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=$keyword&type=video&key=abcdefg&maxResults=5");
Have a look at $_SERVER[REQUEST_URI]
This will return you the current url. Then process it using simple string or array functions to get the params, like
$current_url = $_SERVER['REQUEST_URI'];
$url_arr = explode("/", $current_url);
Then access the parameters using the array indexes
like $page = $url_arr[0];
I am getting content of XML using this code:
$xml = simplexml_load_file("X.xml");
echo $xml->CountryList->Country[1];
Here is the X.xml:
<PickUpCityListRQ>
<CountryList>
<Country>Albania</Country>
<Country>Andorra</Country>
</CountryList>
</PickUpCityListRQ>
Everything works fine, it returns Andorra for me, but, when I try to use url with special characters, like this one:
http://somelink/ServiceRequest.do?xml=<PickUpCityListRQ><Credentials username='USERNAME' password='PASSWORD' remoteIp='IP'/><Country>UK</Country></PickUpCityListRQ>
This link won't work for you as it just an example, but believe, real link returns the same content as X.xml. I know that the reason of that are special characters in the link, but I can't get it work. I tried something like this:
$username = "USERNAME";
$password = "PASSWORD";
$accessurl = htmlspecialchars("Credentials username='$username' password='$password' remoteIp='123.123.123.123'/");
$required = htmlspecialchars("<PickUpCityListRQ><$accessurl><Country>UK</Country></PickUpCityListRQ>");
$url = 'somelink/service/ServiceRequest.do?xml='.$required;
echo $url;
It returns (with echo) the required link, in case if I use it manualy (in browser) I'll get to the required content. But if I try to get XML content using this code:
$xml = simplexml_load_file($url);
echo $xml->CountryList->Country[1];
I won't work.
Any ideas?
Thank you in advance.
htmlspecialchars is used to protect special char inside an HTML content page (especially on user input, to avoid some sort of XSS or other attack..).
When you are manipulating URLs, you should use instead urlencode to send your content as parameter of the URL.
So your URL will be:
http://someserver/somethink/services/ServiceRequest.do?xml=%3CPickUpCityListRQ%3E%3CCredentials%20username%3D'USERNAME'%20password%3D'PASSWORD'%20remoteIp%3D'IP'%2F%3E%3CCountry%3EUK%3C%2FCountry%3E%3C%2FPickUpCityListRQ%3E
As the documentation says, urldecode is not requiered because the superglobals $_GET and $_REQUEST are already urldecoded. So, in your script which do the job you can directly use the value in your $_GET entry.
$xml = simplexml_load_string($_GET['xml']);
documentation : urlencode
Answer stolen from PHP simplexml_load_file with special chars in URL
Use this
$username = "USERNAME";
$password = "PASSWORD";
$accessurl = "Credentials username='$username' password='$password' remoteIp='123.123.123.123'/";
$required = "<PickUpCityListRQ><$accessurl><Country>UK</Country></PickUpCityListRQ>";
$url= rawurlencode("somelink/service/ServiceRequest.do?xml={$required}");
$xml = simplexml_load_file($url);
echo $xml->CountryList->Country[1];