1

I realise similar questions have come up a few times, but I can't seem to find one that will help with what I am trying to do.

We have a config.php file which is basically a list of variables that a user sets, along with their values, eg:

$OUR_EMAIL_ADDRESS = '[email protected]';
$OUR_WEBSITE       = 'www.test.com';

Just like all the other users we want to transfer a PHP value via Javascript.

We have a way of doing this already, via ajax - but it is not as flexible as we would like as each variable needs a custom script / php page:

Part 1 - javascript (jquery)

$.get("/getConfig_email.php", function(email){
    alert(email);
});

part 2: getConfig_email.php

<?php
echo $OUR_EMAIL_ADDRESS_VALUE;
?>

The question:

Is there a way that PHP can use a string as a variable name? ie. What I want to do is this:

Part 1 - javascript (jquery)

$.get("/getConfig.php?name=OUR_EMAIL_ADDRESS_VALUE", function(email){
    alert(email);
});

part 2: getConfig.php

<?php
$var_name = $_GET['name'];

//some magic here to turn the name of the variable 
//in to the actual variable - a bit like a reverse extract

echo $var_name.value;
?>

What do you think? Is this possible somehow - I'm hoping there is just a php command that can do this for me!

Many thanks in advance.

3 Answers 3

1

Maybe the better option will be to set config variables inside an array:

$CFG = array(
    'OUR_EMAIL_ADDRESS' => '[email protected]',
    'OUR_WEBSITE' => 'www.test.com'
);

And pick up them with:

$var_name = $_GET['name'];
echo isset($CFG[$var_name]) ? $CFG[$var_name] : "";

Otherwise, it might be a real security problem of your script.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep... that was what I thought when I first wanted to do this... In hindsight - it is definitely what we would have done. As it is, there is just a page where users can set direct variables. This script will only be usable with logged in users - (I didnt paste the whole thing above) and there are other security checks.
Well, then the solution of @mellamokb will work in your case. So use $var_name = $_GET['name']; echo $$var_name;. But anyway try to avoid such "dirty hacks" in your future projects :)
0

Yes, it is possible via variable variables:

echo $$var_name;

However, this is unwise since you are taking user input and possibly the user can get values of internal variables that are supposed to be hidden.

2 Comments

Ah - this could be exactly what we are after. I had read the php page on this but didnt really get how to use them. So in the example I gave - I would just: $name = $_GET['name']; echo $$name; Does that look right?
Obviously will do what we can to deal with security issues, such as naming and checking the variables names that are allowed to be collected or something.
0

create a dictionary with all your config variables and use it to look up a variable by name

1 Comment

Alas, we are way too far down the road to do this. New projects all work in this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.