...

View Full Version : problem with the $_GET thingy



maingroupe
02-19-2008, 11:15 PM
hello people a newbie here with a simple question:


index.php?code=abcd
$code = $_GET['code'];
echo "$code"; returns abcd


index.php?code=abcd&efgh
$code = $_GET['code'];
echo "$code"; also returns abcd

how can i fix this problem. i know "&" is the problem so i want it to be ignored and treated as just another character.

thanks in advance

by the way, what other characters could potentially create the same problem?

deamonlizard
02-19-2008, 11:28 PM
Write the & sign as & #38;

Just remove the space between the & and the # in the thing above (in cannot write it as it should be otherwise the browser actually converts it to a read & sign).



That 38 thing is the "html entity" for the & sign

php can do this automatically for you with the function urlencode

see http://www.php.net/manual/en/function.urlencode.php

So your link should be

index.php?code=abcd& #38;efgh

(once again just remove the space between & and # and it will work)

jaap
02-19-2008, 11:39 PM
And another small explanation: the & is a separator between the arguments in the $_GET request. Therefore, you should always use urlencoding (http://www.php.net/manual/en/function.urlencode.php) (like deamonlizard talks about).

_Aerospace_Eng_
02-19-2008, 11:47 PM
Examples of the above suggestions. This is how it will look when using
then you can decode it using urldecode.

$code = urlencode('abcd&efgh');
echo $code; // returns abcd%26efgh

<?php
$code = '';
if(isset($_GET['code']))
{
$code = urldecode($_GET['code']);
echo $code; // returns abcd&efgh
}
?>

oesxyl
02-19-2008, 11:48 PM
the & don't create problem, the problem is that you don't know that the query string must be one or more pair name=value separated by & or ;

in your second example if you use:



ndex.php?code=abcd&efgh=myvalue
$code = $_GET['code'];
echo "$code"; also returns abcd // you don't need to quote $code, is a variable
$efgh = $_GET['efgh'];
echo $efgh; will return 'myvalue'


@jaap: urlencode is used to encode specials chars in url, not &


sorry, I missunderstand what you want. After _Aerospace_Eng_ I understand that you are talking about a single value which contain &.

@jaap: you are right in this case, & is special char inside a value, and urlencode work, :)


best regards

_Aerospace_Eng_
02-19-2008, 11:57 PM
If its a string that gets passed to the url then yes you can encode and yes it does encode &.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum