Get values from URL using GET from encode UTF-8 - php

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;

Related

how to pass variable in get without special char and retrieve back

how to pass variable in get without %20 %27 and retrieve back exactly what is passed i tried urlencode and decode but not works need some help
passing in url http://localhost/civilhelp/enquiry.php?cat=%27PLUMBING%20FITTINGS%20&%20SANITARY%20WARE%27
i need to remove %27 %20 used urlencode but no use
retrive only PLUMBING FITTINGS and remaining pls anybody help with a solution
i am passing it as
$cate = 'PLUMBING FITTINGS & SANITARY WARE'; it is from db
<a href="enquiry.php?cat='<?php echo urlencode($cate) ;?>'">
try to retrieve as
$category = urldecode($_GET['cat']);
$category = str_replace("'"," ",$category);
print_r($category);
$category = trim($category);
print_r($category); it just prints PLUMBING FITTINGS
Am not getting exactly aht i passed
Note: I have used urlencode and urldecode also trim
But still i can't Hope what is my problem!!
that cannot be right i tried your code and it show the correct data try this
$url = 'http://localhost/civilhelp/enquiry.php?cat=%27PLUMBING%20FITTINGS%20&%20SANITARY%20WARE%27';
$category = urldecode($url);
$category = str_replace("'","",$category);
echo $category; // result: http://localhost/civilhelp/enquiry.php?cat=PLUMBING FITTINGS & SANITARY WARE
EDIT:
i got your problem just in the cat='' change & to %26
<?php if(!empty($r['category'])){ $cat = $r['category']; $cate=substr($cat,0,25) . '';
//$cat = str_replace(' ', '', $cate);
$cat = str_replace('&', '%26', $cat); after changing it works fine // print_r($cat);?>
<a href="enquiry.php?cat='<?php echo $cat ;?>'">
enter code here

PHP Url Encoding Not Encoding Properly

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="...

PHP explode string, concat with search url and echo it

I have names of actors in variable $actor
Example:
Actor One, Actor Two,Someone
I need to split the string per person. I figured I'd use explode to get something like this:
[0] => Actor One
[1] => Actor Two
[2] => Someone
Then I need to build search links in following format:
http://site.com/?s=Actor+One
http://site.com/?s=Actor+Two
http://site.com/?s=Someone
And finally echo it like this:
<a href='http://site.com/?s=Actor+One'>Actor One</a>, <a href='http://site.com/?s=Actor+Two'>Actor Two</a>, <a href='http://site.com/?s=Someone'>Someone</a>
I'm just totally lost in PHP syntax. Help is appreciated.
(this is homework, isn't it?)
Anyway:
// $actors is an array with the names
$actors = explode(',', $actor);
foreach ($actors as $name) {
$e_name = urlencode($name);
print "" . htmlentities($name) . "";
}
<?php
$arr = array('Actor One','Actor Two','Someone');
foreach($arr as $value){
echo ''.htmlentities($value).'';
}
?>
Oops url encoding.. my bad!
Just to post a safe version that works independently of whether the array content has been maliciously prepared:
$cs = "UTF-8";
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", $cs);
/* ... */
foreach($actor as $name)
{
print('<a href="http://site.com/?s=' . htmlentities(urlencode($name), ENT_QUOTES, $cs)
. '">' . htmlentities($name, ENT_QUOTES, $cs) . '</a>');
Probably best to include the encoding as well, after all your array could contain all sorts of characters.
There are lots of steps to what you need to do:
explode the string into an array (as you did)
Create and echo the links with those urls
When the actor name goes into a url, remember to rawurlencode it
When it goes into HTML, remember to htmlspecialchars it
So the above can be translated into this code:
$actors = explode(',', $actor);
foreach($actors as $actor) {
printf('<a href=\'http://site.com/?s=%s\'>%s</a>',
rawurlencode($actor),
htmlspecialchars($actor));
}

Attempting to send array through GET method, but unable to unserialize

$subprompt = serialize($errors);
header('Location: landingpage.php?success=false&prompt=' . $prompt . '&subprompt=' . $subprompt);
then
if(isset($_GET['subprompt'])){
$subprompt = $_GET['subprompt'];
$subprompt = unserialize($subprompt);
print_r($subprompt);
}
I get all the data when I echo just the $_GET variable, however when I try to unserialize it I get nothing; a blank variable.
$subprompt = urlencode(serialize($errors));
urlencode is intended for this purpose.
$url_string = urlencode(serialize($errors));
Try this:
Besides urlencode, you can try base64 encoding the data. A bit "prettier" url :)
$subprompt = base64_encode(serialize($errors));
header('Location: landingpage.php?success=false&prompt=' . $prompt . '&subprompt=' . $subprompt);
And
if(isset($_GET['subprompt'])){
$subprompt = $_GET['subprompt'];
$subprompt = unserialize(base64_decode($subprompt));
print_r($subprompt);
}
To avoid encoding issues, you should send the value as base64_encode(serialize($subprompt)); instead. The result will be about a 33% longer string, so keep in mind to not exceed the maximum url length.

removing items from url strings in PHP

I would appreciate some help here:
What I want to do:
remove &itemsperpage=10 from:
http://localhost/thi/search/filter.html?type=featured&page=2&itemsperpage=10
and create a link from it:
http://localhost/thi/search/filter.html?type=featured&page=2&itemsperpage=15
here's what I have come up with so far:
<a href="<?php echo url::site(url::current()) . http_build_query($_GET) // don't know what follows ?>"
the framework functions I'm using are:
url::current() = returns current url in controller/action format
url::site() = returns absolute url i.e http://localhost/site/controller/action
so I have to remove '&itemsperpage' from the resulting string in the http_build_query function
but I am having trouble with character encodings and such! please help!
so here's the problem with character encoding:
$needle = '&itemsperpage';
$querystring = http_build_query($_GET) . '<br/>';
// echo $querystring . '<br/>';
$pos = strpos($querystring, $needle);
$remove = substr($querystring, ((int)$pos));
echo substr(str_replace($remove, '', $querystring), 1); // returns ';'
I can't remove the string '&itemsperpage' from the result of http_build_query which is:
'type=featured&page=2&itemsperpage=10' and functions like strstr outputs nothing
I would just do this:
$array = $_GET;
$array['itemsperpage'] = 15;
Then just use your code, but with the new variable (and the ?):
<a href="<?php echo url::site(url::current()) . '?' . http_build_query($array)">
The HttpQueryString class has several methods for getting, setting, modifying query strings and 'translating' their charsets.
You can achieve the effect you're looking for by removing the itemsperpage element from the $_GET array before building the query string.
unset($_GET['itemsperpage']);
And then just use the code you already wrote:
<a href="<?php echo url::site(url::current()) . http_build_query($_GET); ?>">
EDIT: I misread your post. I thought you only wanted to remove the field / value pair from the GET request. All you have to do is overwrite the value with the value you want:
$_GET['itemsperpage'] = 15;
And then use the code you already wrote.

Categories