Reputation: 555
I'm having a problem that's probably (hopefully) very easy to solve but I'm just about stuck.
When I use the urlencode (or rawurlencode) function it will not find he script I'm sending it too.
So this works fine:
$data["text"] .= "<a href='index.php?load=results®=".$row["region"]."'>".$row["region"]."</a><br/>";
And it will take me to the correct page:
index.php?load=results®=CRO
But the person I'm doing the job is having her "&" symbol replaced with a (R) registered symbol, and she is getting a page not found. Unfortunately I'm unable to reproduce this on any of the other computers in this office
So when i change the code to read
$data["text"] .= "<a href='".urlencode("index.php?load=results®=".$row["region"])."'>".$row["region"]."</a><br/>";
It tries to take me to this page
index.php%3Fload%3Dresults%26reg%3DCRO
But it gives me the following page not found error:
The requested URL index.php?load=results®=CRO was not found on this server.
Which is really weird because the requested URL is exactly the same as the URL which resolves no bother up the top. I understand I'll need to run urldecode (or rawurldecode) at some stage but if it isn't even finding a script to load I can't run that function anyway...
EDIT: Further details - I am coding on a Mac using TextWranger. The file is encoded using utf8 with Unix (LF) Line Endings. The local server is using MAMP and the live server is a Windows based machine but I'm not sure of any configuration settings that could help as I've just started here. The output HTML is encoded with UTF-8.
Upvotes: 1
Views: 2869
Reputation: 11942
According to the HTML 4.01 specification:
Although URIs do not contain non-ASCII values (see [URI], section 2.1) authors sometimes specify them in attribute values expecting URIs (i.e., defined with %URI; in the DTD). For instance, the following href value is illegal:
So essentially you should not be using non-ASCII characters in your URI, since different user agents will handle them differently.
For example, the user agent may decide to use the document's character-set encoding or it may assume an html-entity encoding. Which is probably what your user mentioned they experienced. So the best thing to do is make sure you always supply ASCII characters in the href
attribute of an a
tag by urlencoding the entire URI part of your URL.
If you're assembling this URL on the fly, each time, one function in PHP that can help you take care of the whole ordeal is http_build_query, which puts together the query string for you and properly url-encodes it.
$row["region"] = "CRO";
$query = array(
"load" => "results",
"reg" => $row["region"],
);
$query = http_build_query($query);
echo $URL = "index.php?" . $query; // index.php?load=results®=CRO
Now you can use
echo "<a href=\"$URL\">{$row['region']}</a>";
and you get...
<a href="index.php?load=results®=CRO">CRO</a>
Upvotes: 1
Reputation: 334
The question mark is used as a separator and is not part of the query string.
You can't enecode character "?" Character "?" is not part of query string.
Of course you are free to encode any part of query string
Try:
<?php
echo "<a href='get.php?".urlencode("load=results®=".urlencode($row["region"])."'>".$row["region"]."</a><br/>";
var_dump($_GET);
?>
Upvotes: 0
Reputation: 43850
Here is how you use it:
$data["text"] .= "<a href='index.php?load=results®=".urlencode($row["region"])."'>".$row["region"]."</a><br/>";
What you are doing is urlencoding the values of you parameters, not the whole query.
So basically:
$data = "index.php?key=".urlencode("hello world value");
// will result in:
"index.php?key=hello%20world%20value"
Upvotes: 2