PHP Url Encoding Not Encoding Properly - php

I am trying to encode the following string: Peôn
Accordiing to: http://meyerweb.com/eric/tools/dencoder/ the string should enocde to: Pe%C3%B4n
When I use urlencode($name) I get Pe%F4n
SOOOO Lost on this.
I am trying to use the encoded string in the following manner:
Fails:
http://us.battle.net/api/wow/character/Kil%27jaeden/Pe%F4n?fields=statistics
Works:
http://us.battle.net/api/wow/character/Kil%27jaeden/Pe%C3%B4n?fields=statistics
mycode:
$name = mysqli_real_escape_string($connection, $_POST['name']);
$name = urlencode($name);
EDIT:
$name = $_POST['name'];
$realm = mysqli_real_escape_string($connection, $_POST['realm']);
$locale = mysqli_real_escape_string($connection, $_POST['locale']);
$toon = query_blizz_toon($name, $realm, $locale);
function query_blizz_toon($name, $realm, $locale){
$realm = urlencode(stripslashes($realm));
$name = urlencode($name);
$q = 'http://'.$locale.'.battle.net/api/wow/character/'.$realm.'/'.$name.'?fields=statistics';
echo $q;
$character = #file_get_contents($q);
$character = json_decode($character);
return $character;
}
echo $q ouputs: http://us.battle.net/api/wow/character/Kil%27jaeden/Pe%F4n?fields=statistics
Still getting the wrong encoding even without the escaping... :/
EDIT:
According to this site: http://www.degraeve.com/reference/urlencoding.php
%F4 is the correct encoding for ô ...

Well i am getting exactly what you are expected to get. Your mysqli_real_escape is the culprit here. Remove it.
Also, make use of Prepared Statements, so that you don't have to focus on escaping and stuff.
<?php
$name = 'Peôn';
echo $name = urlencode($name);
OUTPUT:
Pe%C3%B4n

$name = rawurlencode(utf8_encode($name));
Does the trick. Wish i knew why...

The problem is with the page that has the form.
The web browser is sending the user input encoded in latin-1, but you want it to send UTF-8. One way to fix that is sending the Content-Type header to tell the browser that the page is in UTF-8, if you can't do that you can use the HTML meta tag to do the same, yet another is declaring the accept-charset in the form tag.
<form accept-charset="UTF-8" method="POST" action="...

Related

How do I Set PHP Variables from URL Encoded Parameters?

Ok guys I got a situation here and I could use some help.
I get the parameters sent to me url encoded and they look like this
http://example.com/tyntec.php?sender%3D%2B16155305760%26receiver%3D%2B17874539876%26text%3Dplease+stop+sending+messages
I am trying to set my variables like this but that is not working because I guess the $_REQUEST is skipping the encoded & sign
$to = $_REQUEST['receiver'];
$from = $_REQUEST['sender'];
$text = $_REQUEST['text'];
How do I properly grab the parameters from the URL and set the variables?
Thanks in advance for helping me out.
Try this.
parse_str( urldecode( 'http://example.com/tyntec.php?sender%3D%2B16155305760%26receiver%3D%2B17874539876%26text%3Dplease+stop+sending+messages' ), $output );
var_dump($output);
Use this:
<?php
$url = 'http://example.com/tyntec.php?sender%3D%2B16155305760%26receiver%3D%2B17874539876%26text%3Dplease+stop+sending+messages
';
$url = utf8_decode(urldecode($url));
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['sender'];
echo $query["receiver"];
echo $query["text"];
?>

how to fix variables starting with tags in php

I have the following code:
$name = "<test>";
$msg = "Hi $name Hope your feeling well today";
print_r ($msg);
The problem is, it will print Hi Hope your feeling well today and skip the $name
When I tried it this way:
$name = "<test>";
$msg = "Hi ".$name." Hope your feeling well today";
print_r ($msg);
it also printed the same.
When I tried it as:
$name = "<test>";
$msg = 'Hi $name Hope your feeling well today';
print_r ($msg);
it printed Hi $name Hope your feeling well today
I need a solution so that the variable $name will be printed as it is if starting with < or any other PHP related code.
View the source of your web page. It is there. You don't see it because it is in brackets which the browser interprets as HTML. Since HTML tags are interpreted and not displayed, you don't see that content.
To display those brackets, you need to convert them into HTML entities. You can use the aptly named htmlentities() function to do that.
$name = "<test>";
$msg = "Hi $name Hope your feeling well today";
echo htmlentities($msg, ENT_NOQUOTES, 'UTF-8');
Write instead of < < and instead of > >

Get values from URL using GET from encode UTF-8

let me start by saying that I have no idea how to formulate this question, I have spend the last two days looking for some ways to to the following.
I send some information encoded using base64 as follow....
Values are:
Lóms Gruñes
this values came from an input box
beacuse of that I do this
$name = htmlentities(( $_POST ['name'] ) , ENT_NOQUOTES, 'UTF-8');
$midn = htmlentities(( $_POST ['midn'] ) , ENT_NOQUOTES, 'UTF-8');
the output for this should be
Lóms Gruñes
And that is what gets encode, until that everything is fine, and I can do whatever I need with that, in this case I'm going to encode it using base64
$datas ='&name='. $name .'&midn='. $midn;
$bd = base64_encode($datas);
// Now lets send that info to another file..
header( 'Location: other.php?d=$db' ) ;
So the url will be something like
domain.com/other.php?d=TCZvYWN1dGU7bXMgR3J1Jm50aWxkZTtlcw==
So now lets decode it so that it can be saved...
$ds = base64_decode($_GET['d']);
parse_str($ds, $params);
$name = htmlentities($params['name'], ENT_NOQUOTES);
$midn = htmlentities($params['midn'], ENT_NOQUOTES);
It looks pretty straight forward isn't it... but here is the problem because when I try to use the values nothing happen... lets say I just want to echo it...
echo $name . '<br>';
echo $midn;
What I get is
L
Gru
so where is the ó and the ñ?
ok, let say I don't encode anything so the URL will look like this...
domain.com/other.php?name=Lóms&midn=Gruñes
// and the I use echo like this:
echo $_GET['name'] . '<br>';
echo $_GET['midn'];
// the output is
L
Gru
Even if I put : header ('Content-type: text/html; charset=utf-8'); after the <?php ... nothing happen... so, the question... how can I get the ó as a value or better yet, how can I send the í,ó,ñ,á...etc as is in the url domain.com/file.php?data=íÄÑó and retrive it as is and save it as is and display it as is...
I;m not sure if this has some relevant information, the data is going to be saved in a DB, the DB is InnoDB, utf8_general_ci
Thank you for taking the time...
Always escape/encode for the technology you're using.
To get the UTF-8 encoded values from the form submission:
$name = $_POST['name'];
$midn = $_POST['midn'];
To output those into HTML:
<p><?php echo htmlspecialchars($name, ENT_COMPAT, 'UTF-8'); ?></p>
To embed them in a URL:
$url = sprintf('foo.php?name=%s&midn=%s', rawurlencode($name), rawurlencode($midn));
// or
$url = 'foo.php?' . http_build_query(array('name' => $name, 'midn' => $midn));
In foo.php, to get the values back:
$name = $_GET['name'];
$midn = $_GET['midn'];
And again, to output those into HTML:
<p><?php echo htmlspecialchars($name, ENT_COMPAT, 'UTF-8'); ?></p>
And that's basically all you need to do. Always escape values using the right function for the medium, don't escape more or earlier than you need to.
First of all, if you want to decode htmlentities try html_entity_decode().
For encoding the url try urlencode().
Example from php.net:
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
Hope it helps!
May be this will work for you . Instead of using htmlentites I used rawurlencode and rawurldecode function .
Below is same code
First get the values from the
$name = rawurlencode('Lóms Gruñes'); use can use //( $_POST ['name']
$mid = rawurlencode('Gruñes'); and here ($_POST ['midn'] )
then
echo $datas ='name='. $name .'midn='. $midn;
$en = base64_encode($datas);
Append this on next url . Then get the value and decode it using base64_decode function
$dd = base64_decode($en);
parse_str($dd);
echo $name;
echo $midn;

php simplexml_load_file with htmlspecialchars

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'PASS‌​WORD'%20remoteIp%3D'IP'%2F%3E%3CCountry%3EUK%3C%2FCountry%3E%3C%2FPickUpCityListR‌​Q%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];

XML character encoding issue with PHP

I have code which is creating an XML, my only problem is with the encoding of words like á, olá and ção.
These characters dont appear correctly and when I try reading the XML I get an error displayed relating to that character.
$dom_doc = new DOMDocument("1.0", "utf-8");
$dom_doc->preserveWhiteSpace = false;
$dom_doc->formatOutput = true;
$element = $dom->createElement("hotels");
while ($row = mysql_fetch_assoc($result)) {
$contact = $dom_doc->createElement( "m" . $row['id'] );
$nome = $dom_doc->createElement("nome", $row['nome'] );
$data1 = $dom_doc->createElement("data1", $row['data'] );
$data2 = $dom_doc->createElement("data2", $row['data2'] );
$contact->appendChild($nome);
$contact->appendChild($data1);
$contact->appendChild($data2);
$element->appendChild($contact);
$dom_doc->appendChild($element);
What can I change to fix my problem, I am using utf-8???
Please try to put directly 'á', 'olá' or 'ção' in your script.
$data1 = $dom_doc->createElement("data1", 'ção');
If you don't have problem, this is probably the data you get from mysql that are wrongly encoded.
Are you sure your mysql outputs correct UTF-8?
To know that, make your PHP dump your data in an HTML document with meta tag set to UTF-8 and see if the characters display correctly.
You can also call :
$data1 = $dom_doc->createElement("data1", mb_detect_encoding($row['data']));
and see what encoding is detected by PHP for your data.
If you can't convert the data from your database, or change its settings, you can use mb_convert to do it on-the-fly : http://www.php.net/manual/en/function.mb-convert-encoding.php
You are using utf-8, the 8-bit unicode encoding format. Even though it properly supports all 1,112,064 code points in Unicode its possible that there is an issue here.
Try UTF-16 as the standard, just an idea. See below:
$dom_doc = new DOMDocument("1.0", "utf-16");
OR
$dom_doc = new DOMDocument("1.0", "ISO-10646");

Categories