Page 1 of 1
Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 6:00 am
by tkj
Hi.
I have made a quick test code with the $_GET command, but it doesn't work 100%
This is the code:
Code: Select all
<?php
$string = $_REQUEST['text'];
print $string
?>
When i then enter the adress, with the property ?text=#ABC Hey - then it doesn't write anything!
If i remove the # character from the property, then it works fine, and it writes the string..
Also, i've tested the string length of $text when the property was ?text=#ABC Hey, and the string length was 0!!!
Do you know what the problem is? It's something to do with the # character, but i need to have it in the property!
Best Regards
Thomas Jespersen
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 6:16 am
by onion2k
# is the 'named anchor' reference character in HTML, it's what you use to jump to a specific point in a page. If you want to pass it as a string you need to URL encode it ... eg use urlencode() on the string before it's echo'd out.
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 10:07 am
by tkj
So you mean that this should work?
Code: Select all
<?php
$string = urlencode($_GET['text']);
print $string;
?>
Then i'm sorry, it doesn't work!
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 10:39 am
by Stryks
You'll need to run it through urlencode() *before* it is embedded in a URL.
So if you want to pass #ABC in your URL, you'd have to do
Code: Select all
<a href="page.php?text=<?php echo urlencode('#ABC'); ?>">link</a>
Or, if you're wanting for users to be able to enter their own values, consider just leaving the # out and adding it later.
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 10:41 am
by tkj
The problem is, that it's is coming from another server, which i can't change!
The server outputs automatically the # character, as it's in the text it needs to output to my server/script...

So it can't be fixed, and used with PHP?
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 10:51 am
by onion2k
If you don't have control over the script that is producing the link with the # in it so you can url encode it then there's nothing you can do. I'm pretty certain the bit after the # isn't passed by the browser when you click a link ... as it shouldn't be, because it's only an internal page link.
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 11:08 am
by tkj
onion2k wrote:If you don't have control over the script that is producing the link with the # in it so you can url encode it then there's nothing you can do. I'm pretty certain the bit after the # isn't passed by the browser when you click a link ... as it shouldn't be, because it's only an internal page link.
It's not a link, like you use to make a link jump to a specific position on a site!
It's just the # in the beginning of the URL PHP Properties (GET)...
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 11:15 am
by Stryks
Yeah ... *we* get what it is .. .but the browser doesn't understand that you are using this reserved character to so something other than what it knows to do, which is to assume it is for an internal page link. So, it just clips that bit and sends the rest to PHP.
PHP never gets it and therefore, you get an empty string.
You cannot pass an unencoded # as a part of a URL.
Sorry.
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 11:16 am
by Stryks
Well .. as far as I'm aware.
Feel free to prove me wrong anyone.

Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 11:18 am
by onion2k
tkj wrote:onion2k wrote:If you don't have control over the script that is producing the link with the # in it so you can url encode it then there's nothing you can do. I'm pretty certain the bit after the # isn't passed by the browser when you click a link ... as it shouldn't be, because it's only an internal page link.
It's not a link, like you use to make a link jump to a specific position on a site!
It's just the # in the beginning of the URL PHP Properties (GET)...
No, the problem is that it
IS a link like you use to make a link jump to a specific position on a site. That's why you don't get it in your PHP script. And that's why you'd need to URL encode the character ... to stop the browser thinking that it's an internal page anchor. Unless you urlencode() it the browser won't send it.
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 11:29 am
by tkj
onion2k wrote:tkj wrote:onion2k wrote:If you don't have control over the script that is producing the link with the # in it so you can url encode it then there's nothing you can do. I'm pretty certain the bit after the # isn't passed by the browser when you click a link ... as it shouldn't be, because it's only an internal page link.
It's not a link, like you use to make a link jump to a specific position on a site!
It's just the # in the beginning of the URL PHP Properties (GET)...
No, the problem is that it
IS a link like you use to make a link jump to a specific position on a site. That's why you don't get it in your PHP script. And that's why you'd need to URL encode the character ... to stop the browser thinking that it's an internal page anchor. Unless you urlencode() it the browser won't send it.
Hmm, also when it's in a GET Variable.. I think so!
So there aren't any way doing this without changing in the other servers script?
This is the variable ?text=#PSP Hi, my name is Thomas
I only need to get the things (string) after #PSP, but i can't pass it, as it has the anchor.
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 11:33 am
by onion2k
You can't. As far as the browser is concerned the # means "Ok, the address and variables stop here, the rest is just so I can find a location".
Re: Problem with $_GET - # Character
Posted: Sun Sep 28, 2008 12:11 pm
by tkj
onion2k wrote:You can't. As far as the browser is concerned the # means "Ok, the address and variables stop here, the rest is just so I can find a location".
The strange is, that this code works, both from the other server, and if i write the URL myself:
Code: Select all
<?php
$text = $_GET['text'];
mail("my@email.com", "Mobil Test", $text, "From: noreply@tkjweb.dk\nX-Mailer: http://$HTTP_HOST$REQUEST_URI");
?>
But this only works if i write the URL and make the anchor as %23:
Code: Select all
<?php
$string = urlencode($_GET['text']);
$string = substr(urldecode($string), 1);
list($init, $cmd, $rest) = split(" ", $string);
if ($init == "PSP" && $cmd == "number") {
$numbertoget = $rest;
error_reporting(E_ALL);
$request = 'http://www.tkjweb.dk/get_phoneowner.php?number='.urlencode($numbertoget);
// Make the request
$return = file_get_contents($request);
// Retrieve HTTP status code
list($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3);
// Check the HTTP Status code
switch($status_code) {
case 200:
// Success
break;
case 503:
die('Error 503. That means: Service unavailable. An internal problem prevented us from returning data to you.');
break;
case 403:
die('Error 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
break;
case 400:
// You may want to fall through here and read the specific XML error
die('Error 400. That means: Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
break;
default:
die('The call returned an unexpected HTTP status of:' . $status_code);
}
list($name, $adress, $city) = split("<br>", $return);
$message = "The Number: ".$numbertoget."\n\nName: ".$name."\nAdress: ".$adress."\nCity: ".$city;
mail("my@email.com", "Phone Number Lookup", $message, "From: noreply@tkjweb.dk\nX-Mailer: http://tkjweb.dk");
}
?>
I've also changed the http://$HTTP_HOST$REQUEST_URI in the other script to
http://tkjweb.dk, as it said that the $REQUEST_URI was an undefined variable!
Please check my code and see if you can find some mistakes, and figure out why the first one is working, while the other isn't!