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.
You don't need file_get_contents:
require_once 'config.php'
var_dump($CONFIG);
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');
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.
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()
simple
include_file "config.php";
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
Related
First, I do not hope this question is too stupid for stackoverflow, but I am quite new to php and do not have much experience.
I have the file page.php and the file sendTestMail.php and I would like to run the file sendTestMail.php with specific parameters.
I am calling sendTestMail.php like that:
require_once WPGAMAIL_PATH.'sendTestMail.php';
And I need to set two array parameters $wp_set and $ga_set.
Any suggestions what a best-practice solution is?
I appreciate your replies!
You have tons of possiblities, however without knowing the structure within your sendTestMail.php I can only give you hints.
You should basically create a function within your sendTestMail.php - e.g.
<?php
function sendTestMail($wp_set, $ga_set) {
/* your actual code ...*/
}
If you now require the script you can simply call the function an pass the parameters
<?php
require_once WPGAMAIL_PATH.'sendTestMail.php';
sendTestMail('john', 'doe'); // or whatever your parameters are
Other examples of how to pass variables to included / required files can be found here
PHP pass variable to include
I'm trying to figure out how to add to a PHP data array externally using PHP.
Say if this array, below. Was in a file called Index.php
$data=array("user1"=>array("url"=>"user1.pdf","password"=>"pass1"),
"user2"=>array("url"=>"user2.php","password"=>"pass2"));
and I wanted to add third user using a different Php file taking inputs from somewhere else to insert into the url, password and user namespace.
Thanks.
$data['user3']=array("url"=>"user3.pdf","password"=>"pass3")
I believe include_once() or require_once() should do the trick.
include_once('index.php');
array_push($data,"user3"=>array("url"=>"user3.pdf","pass"=>"123"));
include_once or require_once functions are similar to executing that file once and continuing further. http://in1.php.net/include_once
Alternatively, php now allows for object-oriented programming, so if you are familiar with it you can take a shot at that
If you need it from different file, then the simplest way is to include the other file, which adds users to $data.
index.php
$data = array(
"user1"=>array("url"=>"user1.pdf","password"=>"pass1"),
"user2"=>array("url"=>"user2.php","password"=>"pass2")
);
other_file.php
include "other_file.php";
$data["user3"] = array("url"=>"user3.php","password"=>"pass3");
// or
array_push($data, array("user3" => array("url"=>"user3.php","password"=>"pass3"));
Are you sure you need that other file?
I don't know how to search for it, so I'm asking you here.
Situation is the following:
Got a vServer with root. Apache, PHP5, MySQL installed and running.
Now I have an index.php where I want to include 'config.php';. Simple thing!
In the config.php I have a variable like $url = 'http://xxxxxxx';, but I cannot access it in the index.php. There's just an empty Array, when I print_r(parse_url($url)); it.
The curious thing is, when I'm connected with ssh to the server and run php index.php, the output is the whole array as expected.
Do you have any idea?!
Have you tried to include your config.php with absolute path, eg. include('/var/www/config.php');
The best strategy for a config file would be having it written in the following format:
<?php
return array(
'url' => 'http://xxxxxxx',
...
);
Then in your index.php you can do $config = require('wherever/it/is/config.php') and access your parameters like $config['url']. Otherwise you would probably have to use globals which is almost never a good practice.
Damn, I'm so stupid.
I had something like:
config.php
foreach {
if(true){
something;
return false;
}
}
$url = ...;
I just wanted to stop the foreach if it was successful. Maybe worked too good..
But I don't understand, why it hadn't any errors when phping it in the terminal..
Thank you guys!
I am using less for creating css file. I am using this plugin
<?php
require "lessc.inc.php";
$less = new lessc;
$less->setVariables(array(
"base" => "968px"
));
$less->checkedCompile("webform.less", "output.css");
?>
When I first compile this code... the base variable in webform.less is compiled and exact output is created. But when I edit base value and compile, it is not updating. Am I missing anything here in less compilation using php?
The checkedCompile() method compiles only if the input file is a different one or the output file does not already exist. You have to make use of the function compileFile() to compile it again and again..
The following code may do the trick..
<?php
require "lessc.inc.php";
$less = new lessc;
$less->setVariables(array(
"base" => "968px"
));
$less->compileFile("webform.less", "output.css");
?>
I'm calling a file named ajax.php (from my browser for testing)
ajax.php require_once delete.php
delete.php require_once no_direct.php
delete.php starts like this:
$allowed = array('group'=>'admin');
require_once(ASSETS.'/no_direct.php'); //Yes, ASSETS is defined and no_direct is being included.
In no_direct.php I'm trying to:
var_dump($allowed)
and I just keep coming up NULL.
Does this happen because we are running inside ajax.php's require_once function and the scope of $allowed pushes back to the GLOBAL scope not allowing me to access it from no_delete.php?
I was looking here: PHP variable defined in 'parent' file not recognized in 'required' file , just to be diligent.
I'm sure I could solve this with the GLOBAL keyword, but I was hoping for a little clarification. The PHP scope doc didn't seem to answer the question for me.
It wasn't wrapped in another function as thought to be the case.
Is there any chance that you already have called require_once(ASSETS.'/no_direct.php'); before you assigned value to $allowed?
require_once(ASSETS.'/no_direct.php');
...
$allowed = array('group'=>'admin');
require_once(ASSETS.'/no_direct.php');
Script no_direct.php should not output $allowed in this case.
Output will be:
Notice: Undefined variable: allowed in D:\wampserver\www\gim\no_direct.php on line 2
NULL
p.s. there's my path on localhost in wamp for my test file
Does this happen because we are running inside ajax.php's require_once function and the scope of $allowed pushes back to the GLOBAL scope not allowing me to access it from no_delete.php?
Definitely not.
There are NO scope issues regarding includes.
The only scope-dependent issue is user-defined functions.
So, if there are no functions involved, the only cause can be some mistake/mistype - you're editing/including wrong file, or including HTML code it via http or something of the kind. Just double-check.
So my ajax.php file also require_once build.php and config.php
My build.php file require_once no_direct.php as well.
As the function suggests, it will only require no_direct.php ONCE!
So the NULL that I was seeing was coming from build.php's include and not the delete.php's include of no_delete.