简体   繁体   中英

PHP: Getting variables from a php file?

I'm creating a php application that allows the user to install different themes. Each theme directory must be placed in the "themes" directory. Each theme must have a file called theme_profile.php which have theme specifications such as the name of theme and etc...

So this theme_profile.php has a bunch of variables wrapped inside an array called $theme, ($theme["title"]="My theme" etc...)

Now I want my application index file to get these variables without simply 'including' the file (which is risky if malicious code is placed in this file).

Is there anything such as get_variables_from_file("file.php") ?

it is not necessary to use php file for this purpose. Simply create an XML or any other of data files types and parse them in your PHP code. This is much safer.

在另一个问题上查看此答案可能会向您发送正确的方向: https//stackoverflow.com/a/1858294/303657

As mentioned in other answers, using PHP is not necessary in such cases. Actually it is better to use data files. (Like XML, JSON, etc.)

This is a sample JSON encoded file:

// first create the $theme array
$theme['title'] = '...';
$theme['...'] = '...';

// then json encode it
$themeJson = json_encode($theme);

// put it in a file
file_put_contents('theme_profile.json', $themeJson);

For loading purpose, simply use json_decode function .

You can also use PHP serialize the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM