How to send string(say: public_Key) having special characters in URL? - php

When I said simple string It works fine, but when I send my public_key string in the same way, It shows error.
I have tried the urlencode() method.
<iframe src="http://local.abc.com/formulaList?id=<?php echo $public_key; ?>" >

base64() doesn't work because its output can contain slashes (and anyway, most public key strings are already base64 encoded)
htmlspecialchars() escapes HTML special chars as its name implies which has nothing to do with urls (for example, é will be converted to é)
urlencode() is the right function to use but keep in mind an URL shouldn't be too long as explained in this SO answer
What problem did you encounter when using urlencode() ?

I write something this might work.
$url = htmlspecialchars("wdsd#ccddsd*");
header("Location: https://www.google.com?key={$url}");
this will work for you!

Related

PHP file_get_contents() Chinese character ERROR CODE

I use file_get_contents() to download a JSON. There're some Chinese characters in the URL, I tried to print the URL out, it's OK. But when I ran the program, the URL I put in the function became error code. How do I know that is this URL links to a JSON that links to a MySQL request, and in the console of MySQL, I saw the URL became error code. I tried lots of ways to change URL string to UTF-8 or GB2312, etc, but none of that works. I Wish I could get help here, thanks.
Its very difficult to understand your question. I think i understood the first part of your question:
I use file_get_contents() to download a JSON. There're some Chinese
characters in the URL, I tried to print the URL out, it's OK. But when
I ran the program, the URL I put in the function became error code.
You try to access a URL containing chinese characters using file_get_contents():
The answer to this is:
You need to encode the part of the url containing chinese characters using urlencode() or rawurlencode().
The main difference between urlencode()and rawurlencode() is, that urlencode() converts spaces to +. rawurlencode() converts spaces to %20.
urlencode is used for Query Parameters as example ?q=my+search+key, in every other case you use rawurlencode.
Example:
$test = 'http://www.example.com/'.rawurlencode('以怎么下载').'.html';
print_r($test);
// $html = file_get_contents($test);
// output:
http://www.example.com/%E4%BB%A5%E6%80%8E%E4%B9%88%E4%B8%8B%E8%BD%BD.html
I hope it solves your problem.

php convert foreign language chars to "string"

UPDATE: Please ignore this question, it appears that md5 is not
returning result because I pass the URL through filter_var($url,
FILTER_SANITIZE_URL) and looks like FILTER_SANITIZE URL doesn't work
for foreign characters.
I have a problem where I want to get a hash from URLs e.g
https://ko.wikipedia.org/wiki/추간판_탈출증
The URL is provided by user in a form with so I assume it's already UTF-8 since my website is UTF-8.
However the above cannot be used with md5() as it returns empty result. May I know what php function do I use to convert it to something like below where md5() can be used?
https://ko.wikipedia.org/wiki/%EC%B6%94%EA%B0%84%ED%8C%90_%ED%83%88%EC%B6%9C%EC%A6%9D
I tried iconv, htmlspecialchar, htmlentities and I cannot seems to be able to find the right function to convert the strings.
You can use directly md5 to Encode whole URL as Below :
echo md5('https://ko.wikipedia.org/wiki/추간판_탈출증');
Which gives output as :
26eb333445f4e154f8ecb76e7c2ac858
UPDATED :
As Per w3schools FILTER_SANITIZE_URL
The FILTER_SANITIZE_URL filter removes all illegal URL characters from
a string.
This filter allows all letters, digits and
$-_.+!*'(),{}|\^~[]`"><#%;/?:#&=
The function you are looking for is rawurlencode. However you will have to extract the part of the url you want to encode or the whole url will be encoded.
$encoded = rawurlencode('추간판_탈출증');
// value of $encoded is now '%EC%B6%94%EA%B0%84%ED%8C%90_%ED%83%88%EC%B6%9C%EC%A6%9D'
use urlencode(url) function for conversion
once check this url url encode functions are found here

using htmlentities with superglobal variables

I'm working on php with a book now. The book said I should be careful using superglobal variables, so it's better to use htmlentities like this.
$came_from = htmlentities($_SERVER['HTTP_REFERER']);
So, I wrote a code like this;
<?php
$came_from=htmlentities($_SERVER['HTTP_REFERER']);
echo $came_from;
?>
However, the display of the code above was the same without htmlentities(); It didn't change anything at all. I thought that it would change \ into something else. Did I use it wrong?
So, by default, htmlentities() encodes characters using ENT_COMPAT (converts double-quotes and leave single-quotes alone) and ENT_HTML401. Seeing as the backslash isn't part of the HTML 4.01 entity spec (as far as I can see anyway), it won't be converted.
If you specify the ENT_HTML5 flag, you get a different result
php > echo htmlentities('abc\123');
abc\123
php > echo htmlentities('abc\123', ENT_HTML5);
abc&bsol;123
This is because backslash is part of the HTML5 spec. See http://dev.w3.org/html5/html-author/charref
Sorry. My previous answer was absolutely wrong. I was confused with something else. My apologise. Let me refrain my answer:
htmlentities will convert special characters into their HTML entity. "<" for example will be converted to "<". Your browser will automaticly recognise this HTML entity and decode it back to "<". So you won't notice any difference.
The reason for this is to prevent problems when saving your document in something different then UTF-8 encoding. Any characters not encoded might become screwed up for this reason.

Escaped symbol on get parameter

I have a string has encrypted but with some symbol qwfKOEK==dwk&f
What if I need pass this string to a parameter:
www.example.php?string=qwfKOEK==dwk&f
$_GET["string"]
But I can’t get the string cause the symbol interrupt it.
Anyway to escape the symbol?
I had try html_entity_decode but seems not working, any possible way to escape the symbol and $_GET the original string?
A URL value needs to be URL encoded using urlencode or rawurlencode.
The difference between the two is two slightly different standards for encoding, whereby the rawurlencode variant is generally preferred.
If you try to put this sting in a GET parameter, definitely it'll not be accepted as it explodes at "=" sign. You can try passing the same though a POST parameter or try changing the encryption technique. I hope this may solve the problem. plus, html_entity_decode() doesnt apply to "=" sign.

Pass text with special characters as get parameter in php

I want to pass any text as a get parameter to a php script. For know I just append the text this way:
action.php?text=Hello+my+name+is+bob
This url is composed by javascript and I do a ajax request with this url.
In action.php I do
$encoded = array_map('rawurlencode', $_GET);
But this does not work for special chars like ÖÄüä.
Any idea how to solve this?
url_encode(string) will return the given string with special characters converted into %XX format.
http://us3.php.net/manual/en/function.urlencode.php
I know you can send special characters fine without encoding through $_POST, which is another alternative
try with url_encode().........

Categories