Debashis
Debashis

Reputation: 596

How to read an array from another php page

I have the below mentioned code (actually, configuration) written in a file (config.php) and i want to get the content written in config.php in another file (check.php)

Codes written in config.php:

<?php
$CONFIG = array (
  'id' => 'asd5646asdas',
  'dbtype' => 'mysql',
  'version' => '5.0.12',
);

Code written in check.php to get the content is:

$config_file_path = $_SERVER['DOCUMENT_ROOT'] . 'config.php';
$config = file_get_contents($config_file_path);

Using the above code i am getting the output as a string and i wanted to convert it into an array. To do so, i have tried the below code.

$config = substr($config, 24, -5); // to remove the php starting tag and other un-neccesary things

$config = str_replace("'","",$config);
$config_array = explode("=>", $config);

Using the above code, i am getting an output like:

Array
(
    [0] =>   id 
    [1] =>  asd5646asdas,
  dbtype 
    [2] =>  mysql,
  version 
    [3] =>  5.0.12
)

which is incorrect.

Is there any way to convert it into an array. I have tried serialize() as well as mentioned in accessing array from another page in php , but did not succeed.

Any help on this will be appreciated.

Upvotes: 3

Views: 20608

Answers (6)

Karl Adler
Karl Adler

Reputation: 16836

Use include or require to include and execute the file. The difference is just what happens when the file doesn't exist. inlcude will throw a warning, require an error. To make sure just to load (and execute) the file the first time you can also use include_once, require_one php.net If you are somehow not able to include the file (what reasons ever) take a look at the eval() function to execute php code from string. But this is not recommendet at all because of security reasons!

require_once('config.php');
include_once('config.php');
require('config.php');
include('config.php');

Upvotes: 4

Shuro
Shuro

Reputation: 303

You don't need file_get_contents:

require_once 'config.php'
var_dump($CONFIG);

Upvotes: 8

hek2mgl
hek2mgl

Reputation: 158280

How could you fail to read about the require_once directive? :)

$config = file_get_contents($config_file_path);

Must be:

require_once $config_file_path;
// now you can use variables, classes and function from this file
var_dump($config);

If you follow the link to the require_once manual page please read also about include and include_once. As a PHP developer you MUST know about this. It's elementary

Upvotes: -1

KAsh
KAsh

Reputation: 304

if you are using codeigniter no need to include the config file, you can access using $this, but if you are using plain php, u need to use include() or require() or include_once() or require_once()

Upvotes: 1

Adam
Adam

Reputation: 1985

I'm very confused at your approach, if you just do this:

include(config.php);

Then your $CONFIG variable will be available on other pages.

print_r($CONFIG);

Take a look on the PHP website which documents the include function.

Upvotes: 3

DevZer0
DevZer0

Reputation: 13545

simple

include_file "config.php";

Upvotes: 0

Related Questions