How to turn string into readable by php server way? (ActionScript) - php

So I know that my server on real form submit turns %CE%EB%E5%E3+%DF%EA%F3%F8%EA%E8%ED into Олег Якушкин . How to peform string transfer from Олег Якушкин into %CE%EB%E5%E3+%DF%EA%F3%F8%EA%E8%ED using ActionScript? (Its ok if a space character as %20, not + , PHP should handle that fine.)

you can encode it using ActionScript's escape() method.

Related

php file_get_contents encoding issue

I am trying to Integrate an SMS service into my website. I need to make a HTTP call with a param named "msg" (urlencoded).
Currently, I am constructing the entire URL with msg param being urlencoded (i.e $msg = urlencode($msg)), and I am sending the SMS.
$msg = urlencode("Hello World");
Although what I receive on my phone is "Hello+World" and NOT "Hello World".
So is there an issue with file_get_contents ?
Also, is there anyway, I can see the string "file_get_contents" finally sends out ?
Try using rawurlencode() instead.
The principal difference (although there are others) is that it encodes spaces as %20 instead of +. This is quite likely the source of your problem.
I figured what was the issue:
I was using http_build_query to create the query string, and I was passing a urlencode'd param to it. Hence it was being encoded twice.
So is there an issue with file_get_contents ?
No.
Also, is there any way, I can see the string file_get_contents finally sends out ?
Yes, with a network sniffer.
You need to use urldecode() after retrieving data and then display to user

how can I javascript decodeURI in PHP?

I have a javascript which sends some specific information to a PHP api . Before to send it performs encodeURI . How can I "decode" it in PHP ? I understand that urldecode/urlencode is different that javascript encode/decodeURI so what can I use ?
Use encodeURIComponent in Javascript: http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp and urldecode in PHP: http://php.net/manual/en/function.urldecode.php
Unless you've encoded it multiple times (e.g. by explicitly calling the encode method AND inserting the value into a form field which is then submitted) you don't need to do anything - it is transparently converted back to its original form when the request is parsed.
You can use rawurldecode function in php, but this function is not UTF-8, then your have to convert to UTF-8 with utf8_decode like this
echo utf8_decode(rawurldecode('Educa%C3%A7%C3%A3o%20Multim%C3%ADdia'));

mysql_real_escape_string() -> stripslashes() -> jquery.append()

im letting my users type in texts, then take them to server side php and process them, and if everything goes as it should, i just append the text with jquery without the page having to load all over again.
This is the procedure:
$post_text = htmlspecialchars(mysql_real_escape_string($_POST['post_text']));
some logic...
everything ok!
stripslashes(str_replace("\\n", "", $post_text))
and then i send all the nessesary data witj json
echo json_encode($return);
on the client side i append the html chunk saved in a variable from the server side.
this seems to work on localhost, it removes all the slashes and so on, but online it just doenst remove the slashes, and they keep coming up, when i hit refresh they dissapear becouse then its a
stripslashes($comment['statusmsg_text'])
written out with php straight from the database. Is it the json that adds some extra stuff? i dont get it becouse it works perfectly on localhost.
best of regards,
alexander
The additional slashes might be magic quotes. You shouldn’t rely on them and disable them.
Additionally, mysql_real_escape_string should only be used to prepare strings to be put into a string context in an MySQL statement. Similar applies to htmlspecialchars that should only be used for sanitizing data to be put into an HTML context.
It may be, that on your server and your localhost the magic_quotes_gpc directive is set differently, so your string is double encoded on server side.
Try it without stripslashes, json_encode should handle that. All you need to do is use mysql_real_escape once, before your string touches your database.

PHP's rawurlencode is not equal to JavaScripts escape! Why?

i realized when i used urlencode or rawurlencode in PHP encoding the simple character § (paragraph) i get the following result: "%C2%A7".
But when i use escape in Javascript to encode that character, i get only "%A7".
In this case i have encoding problems when sending/receiving data between the server running PHP and the javascript client trying to fetch the data via ajax/jquery.
I want to be able to write any type of text i want. For this i encode the text and send it to the backend php script, escaping the data and sending. When i retrieve it, on php side i take the data from mysql and do rawurlencode and send it back.
Both sides, work in UTF-8 mode. jquery ajax function is called with "contentType: application/x-www-form-urlencoded:charset=UTF-8", mysql server is set for UTF-8 both for client and server, and the php script starts echoing with header( "application/x-www-form-urlencoded:charset=UTF-8");
Why is PHP producing that %C2 thing, which generates the character  on javascript side.
Coult somebody help?
I had the same problem a while ago and found the solution :
function rawurlencode (str) {
str = (str+'').toString();
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
replace(/\)/g, '%29').replace(/\*/g, '%2A');
}
The code is taken from here - http://phpjs.org/functions/rawurlencode:501
Hope it helps.
It's clearly a charset íssue:
[adrian#cheops3:~]> php -r 'echo rawurlencode(utf8_encode("§"));'
%C2%A7
[adrian#cheops3:~]> php -r 'echo rawurlencode("§");'
%A7
(the terminal is obviously not running in utf8 mode)
If you have a literal § in your PHP code ensure that the php file is saved as UTF8.

How do I escape '+' in data I’m sending via AJAX in Mootools?

I was building a simple web based calculator which takes equations from a HTML form, evaluates it on the server using PHP and sends the result back.
I am using Mootools to send the data via the req.send AJAX operation.
But, each time I have a '+' in an equation, it is not seen on the POST data the server gets.
Any ideas why this is happening and how I can work around it?
eg:
10 + 12 in HTML form is seen as 10 12 in the $_POST data.
The Mootools send command I am using is something like this with
<textarea name="equationTextArea">10+12</textarea>
req.send("eqn="+$('equationTextArea').value);
Upon submit, I see $_REQUEST['eqn'] as 10 12.
Try using the function encodeURIComponent over your text value. It, well.. uri encodes your text.
Set your form's encoding to multipart/form-data - this is an alternative to the default application/x-www-form-urlencoded and doesn't encode a space into a plus sign +.
Example from the w3.org reference:
<form action="http://example.com/cgi/handle"
enctype="multipart/form-data"
method="post">
Your text most likely either need to be URLEncoded.

Categories