Decode user input with special characters - php

I have a search script where the user input some querywords that are sent with GET. If the user input contains special characters e.g. äpple the url becomes search.php?q=äpple and everything works fine. But if the user reruns this url search.php?q=äpple the browser address field shows search.php?q=%E4ppleand the query fails. How should I decode that string. urldecodedid not work. I thought this was exactly what it was for so is something else wrong? Or which other functions can I try?
UPDATE
if I use utf8_encode on the search.php?q=%E4pple it produces the right ächar but if the string in the url is sent by GET and thus correctly (and working) äalready, the utf8_encode messes it up.
I guess I can make a condition were I check the encoding before doing something but isnt it an easier way??

Solved it by checking the encoding of $_GET['q'] and handle it appropriately for the result of the encode check. See http://php.net/manual/en/function.utf8-encode.php for functions that check encode.

Related

php filter a string so that json_encode does not error out

I'm grabbing a bunch of data from a database and putting it into a PHP array. I'm then looking to json_encode that array using $output = json_encode($out).
My issue is that from time to time, something in the array is not able to be read by json_encode and the whole thing fails. If I use print_r($out) to have a look, I can clearly see where it's failing, because the character that is screwing things up always appears as a question mark inside of a black diamond �.
First - what are these characters?
Second - Is there a function I can pass the elements through prior to adding them to the array that would strip these out, or replace 'them' with blanks?
I found the answer to this. Since the data coming FROM the database was stored with the "black diamond" character, I needed to get this out POST grabbing it from the database.
$x[4] = utf8_encode(odbc_result($query, 'B'));
By passing the result through utf8_encode, the string is encoded into UTF-8 and the illegal character is removed.
Say echo json_encode($out);
This will solve your issue
Black diamonds are browser issue. Database uses plain question marks.
It seems you are getting already wrong data from databalse. But that's quite tricky to have incorrect utf with your settings. You need to check everything
if your table marked with utf8 charset
if your data indeed encoded in utf (not marked but indeed encoded)
if your server sending correct charset in Content-type header.
it is also useful to see the page choosing different charsets from your browser menu.
But first of all you have to wipe any trace of all random actions you tried, all these various encode, decode and stuff. Just plain and direct output from database. Otherwise you will never get to the problem

How do I sent an encrypted hash over URL in CakePHP 3?

I'm working on a reset password function that matches hashes to allow for the user to reset. The only trouble is the hash that was created cannot be passed over URL without breaking the page. For example, my hash is this:
http://localhost/users/changeResetPassword/e0b4ab1d2cdc5742c7b5f72ef6c2935dadfe458dc275b7419d9f1ac66461aa20%0F5%3A%C6%5C%26%2A%E4%D5%ACA%94%ADV%BF%EB%CAz%97O%1F%7D%F0h~%E3-.%FF%B4z%5E%1AQ%B8%8Ca%BC500%2A%EC%7B%FA%AF8%E3%2A%7F%BA%A4y%03%AE%29%94%09%26%9E%29e%E5%DEn%1At%C1%EC%F7%D4x%EAvlA%BE%5B%0D%CF
All of these % seem to break the page, because I get this error:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If I take away everything up to the last % and try it again, the page loads fine. It's just some characters in the URL seem to bug everything out:
http://localhost/users/changeResetPassword/e0b4ab1d2cdc5742c7b5f72ef6c2935dadfe458dc275b7419d9f1ac66461aa20
This loads fine, so I know it's just a URL problem. Any ideas?
Base64 encode the hash and pass it along, then decode when received. Cake URI parsing likely breaks because it'll try to interpret those %xx as html encoded values but it does not appear that is what they are since you have stuff like %03 and from the link: "The ASCII device control characters %00-%1f were originally designed to control hardware devices. Control characters have nothing to do inside a URL"
With regards to ndm's comment indicating you may additionally need to URL encode the base64 string since base64 can contain the characters + = / I'd recommend you also look at url encoded forward slash is breaking url to see why simply encoding the problematic characters might also be problematic. If the current solution of passing unencoded base64 strings is not causing any issues with your rewrite rules I would recommend you keep it as it.

Fetch contents from a unicode URL

I would like to fetch the contents (HTML) from this URL: http://www.tvsporedi.si/spored.php?id=Vaš kanal. I've tried with File_Get_Contents and cURL.
No matter how I construct the URL in my code the page always returns a blank page (a page with header&menu but no content). I tried url encoding the id parameter, leaving the id as it is, without any luck. The only change I can make to the url (it seems) is encoding the space (with %20. Encoding the š does not work.
So I guess what I'm asking is why does PHP "eat" the š? The PHP file is saved with UTF-8 encoding ...
Try fetching:
http://www.tvsporedi.si/spored.php?id=Va%C5%A1%20kanal
This works for me in a browser, and I would guess with whatever method you use.
I used Firebug to see how the browser was encoding the request...
works for me with urlencode:
readfile('http://www.tvsporedi.si/spored.php?id=' . urlencode('Vaš kanal'));
demo

Pass special characters in PHP

I have a text field where where the user can pass wild cards - more specific to the question they can use '%' character.
I am using ajax to get the value and send it to a PHP file. If I enter '%BA' in the text file and retrieve the value using
document.getElementById('textfield').value
This actually gets '%BA'. I am using POST method to send it to a PHP file. But the variable displays as "�" in the web browser and inserts " ° - degree small o" in the database.
I am sure there are other cases that I am not aware of as well. Is there a function in PHP to escape the special characters or any other way to get the exact string?
Edit: This may be a guess but doing escape(document.getElementById('textfield').value) to send the value and using urldecode($values[3]) to retrieve the value doesn't work. Maybe it's a js to PHP problem.
Update: urldecode will not work. Read the first comment in urldecode. Used the function there. Solved.
while passing the value using ajax , you just encode the value with encodeURIComponent() function and use urldecode() function to decode it in the php file. This might solve the issue.
You could encode the characters with urlencode (and maybe htmlspecialchars too) before storing it in the database, and use urldecode ( and maybe htmlspecialchars_decode) to decode them before displaying to the user.
You can use escape in javascript i.e.
escape(document.getElementById('val'))

PHP get url with special characters without urlencode:ing them!

I would like file_get_contents to get a url that looks like this: http://wapedia.mobi/sv/Gröt
The problem is that it requests (can't post entire link, sorry): ...wapedia.mobi/sv/Gr%C3%B6t which you can see has been urlencoded, that page does not give me any results.
How can I do this?
According to the PHP manual, you must specifically encode a URL if it contains special characters. This means the function itself should do no special encoding. Most likely your URL is being encoded before being passed to the function, so pass it through urldecode first and see what happens.
Edit: You're saying the encoding is being messed up. Again the PHP manual specifically states that you need to encode urls prior to passing them to file_get_contents. Try encoding the URL, then passing it to the function.
$url = urlencode($url);
file_get_contents($url);

Categories