Variable name as a variable?
Variable name as a variable?
am 05.10.2009 16:56:48 von Dotan Cohen
I need to store a variable name as a variable. Note quite a C-style
pointer, but a way to access one variable who's name is stored in
another variable.
As part of a spam-control measure, a certain public-facing form will
have dummy rotating text fields and a hidden field that will describe
which text field should be considered, like this:
input type="text" name="text_1"
input type="text" name="text_2"
input type="text" name="text_3"
input type="hidden" name="real_field" value="text_2"
As this will be a very general-purpose tool, a switch statement on the
hidden field's value would not be appropriate here. Naturally, the
situation will be much more complex and this is a non-obfuscated
generalization of the HTML side of things which should describe the
problem that I need to solve on the server side.
Thanks in advance for any ideas.
--
Dotan Cohen
http://what-is-what.com
http://gibberish.co.il
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Variable name as a variable?
am 05.10.2009 16:59:43 von Ashley Sheridan
--=-1cJ1fFo80WUXAjL9EoQr
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Mon, 2009-10-05 at 16:56 +0200, Dotan Cohen wrote:
> I need to store a variable name as a variable. Note quite a C-style
> pointer, but a way to access one variable who's name is stored in
> another variable.
>
> As part of a spam-control measure, a certain public-facing form will
> have dummy rotating text fields and a hidden field that will describe
> which text field should be considered, like this:
>
> input type="text" name="text_1"
> input type="text" name="text_2"
> input type="text" name="text_3"
> input type="hidden" name="real_field" value="text_2"
>
> As this will be a very general-purpose tool, a switch statement on the
> hidden field's value would not be appropriate here. Naturally, the
> situation will be much more complex and this is a non-obfuscated
> generalization of the HTML side of things which should describe the
> problem that I need to solve on the server side.
>
> Thanks in advance for any ideas.
>
> --
> Dotan Cohen
>
> http://what-is-what.com
> http://gibberish.co.il
>
What's wrong with this:
$user_value = $_REQUEST[$_REQUEST['real_field']];
Obviously this isn't production worthy code, you'd really need to put
the whole thing in a ternary if to check if the values actually exist,
but this would definitely solve your problem.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-1cJ1fFo80WUXAjL9EoQr--
Re: Variable name as a variable?
am 05.10.2009 17:05:27 von Tommy Pham
----- Original Message ----
> From: Dotan Cohen
> To: php-general.
> Sent: Mon, October 5, 2009 7:56:48 AM
> Subject: [PHP] Variable name as a variable?
>
> I need to store a variable name as a variable. Note quite a C-style
> pointer, but a way to access one variable who's name is stored in
> another variable.
>
> As part of a spam-control measure, a certain public-facing form will
> have dummy rotating text fields and a hidden field that will describe
> which text field should be considered, like this:
>
> input type="text" name="text_1"
> input type="text" name="text_2"
> input type="text" name="text_3"
> input type="hidden" name="real_field" value="text_2"
>
> As this will be a very general-purpose tool, a switch statement on the
> hidden field's value would not be appropriate here. Naturally, the
> situation will be much more complex and this is a non-obfuscated
> generalization of the HTML side of things which should describe the
> problem that I need to solve on the server side.
>
> Thanks in advance for any ideas.
>
> --
> Dotan Cohen
>
> http://what-is-what.com
> http://gibberish.co.il
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
You mean something like this?
$var_name = "text_2";
echo $$var_name; // equivalent to echo $text_2;
Regards,
Tommy
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Variable name as a variable?
am 05.10.2009 17:30:34 von Lars Torben Wilson
On Mon, 5 Oct 2009 16:56:48 +0200
Dotan Cohen wrote:
> I need to store a variable name as a variable. Note quite a C-style
> pointer, but a way to access one variable who's name is stored in
> another variable.
>
> As part of a spam-control measure, a certain public-facing form will
> have dummy rotating text fields and a hidden field that will describe
> which text field should be considered, like this:
>
> input type="text" name="text_1"
> input type="text" name="text_2"
> input type="text" name="text_3"
> input type="hidden" name="real_field" value="text_2"
>
> As this will be a very general-purpose tool, a switch statement on the
> hidden field's value would not be appropriate here. Naturally, the
> situation will be much more complex and this is a non-obfuscated
> generalization of the HTML side of things which should describe the
> problem that I need to solve on the server side.
>
> Thanks in advance for any ideas.
>
Some reading on this if you're interested:
http://us2.php.net/manual/en/language.variables.variable.php
You can also access array properties using variables if you like:
$foo->some_prop = 'Hi there!';
$bar = 'some_prop';
echo $foo->$bar;
Regards,
Torben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Variable name as a variable?
am 05.10.2009 18:02:22 von Dotan Cohen
Thanks, all! I will experiment with the three different solutions
presented and see what best fits this application. As to the security
aspect, yes, I am aware that this is a simplification and that the
values must be sanitized.
Have a great week.
--
Dotan Cohen
http://what-is-what.com
http://gibberish.co.il
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php