I created the following script and put it to cron job on server. Unfortunately it does not work.
include 'includes/defines.php';
include 'includes/framework.php';
$conf = JFactory::getConfig();
$options = array(
'defaultgroup' => '',
'storage' => $conf->get('cache_handler', ''),
'caching' => true,
'cachebase' => $conf->get('cache_path', JPATH_SITE . '/cache')
);
$cache = JCache::getInstance('', $options);
$cache->clean('cachegroup');
Do you have an idea what is wrong?
New to doctrine php and I followed each instruction to the point. Downloaded doctrine using composer as instructed on http://www.doctrine-project.org/projects/dbal.html and then on my index.php I added this http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#getting-a-connection only changing the test database credentials.
include_once 'vendor/autoload.php';
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'teastdb',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
and this does not work (shows blank page, I even inserted both right and wrong credential for testing purpose). What is wrong?
Here is a screenshot as well
I've got the file 'init.php'. This one is being used to start the session, set some settings and that sort of stuff. I call to this file using this line:
require_once 'core/init.php';
This works perfectly in my opinion. Now, I've written the following script so that it becomes very easy to call a setting in the init.php file.
class Config {
public static function get($path = null) {
if ($path){
$config = $GLOBALS['config'];
$path = explode('/', $path);
foreach($path as $bit) {
if(isset($config[$bit])) {
$config = $config[$bit];
}
}
return $config;
}
return false;
}
}
So now I can use this line of code in my other pages to use the setting:
Config::get('settings/main_color')
This is very easy of course. But now I'd like to edit a setting without having to change the file myself. It should all be done by scripts in the browser. The rest of my init.php settings global looks like this:
$GLOBALS['config'] = array(
'mysql' => array(
'host' => 'localhost:3307',
'username' => 'root',
'password' => 'usbw',
'db' => 'webshop'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'sessions' => array(
'session_name' => 'user',
'token_name' => 'token'
),
'settings' => array(
'main_color' => '#069CDE',
'front_page_cat' => 'Best Verkocht,Populaire Producten',
'title_block_first' => 'GRATIS verzending van €50,-',
'title_block_second' => 'Vandaag besteld morgen in huis!',
),
'statics' => array(
'header' => 'enabled',
'title_block' => 'enabled',
'menu' => 'enabled',
'slideshow' => 'enabled',
'left_box' => 'enabled',
'email_block' => 'enabled',
'footer' => 'enabled',
'keurmerken' => 'enabled',
'copyright' => 'enabled'
)
);
What I hope for is a solution like this:
Config::update('settings/main_color','blue')
What is the best way to achieve this? Use str_replace and replace the word in the file? I hope there's a better way of doing this, and I'd be very pleased if you could help me out.
EDIT: (my complete init.php file)
<?php
session_start();
define('DS',DIRECTORY_SEPARATOR);
$GLOBALS['config'] = json_decode(__DIR__.DS.'prefs.json', true);
spl_autoload_register(function($class) {
require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';
require_once 'functions/statics.php';
require_once 'functions/pagination.php';
if(Cookie::exists(Config::get('remember/cookie_name')) && !Session::exists(Config::get('sessions/session_name'))) {
$hash = Cookie::get(Config::get('remember/cookie_name'));
$hashCheck = DB::getInstance()->get('users_session', array('hash', '=', $hash));
if($hashCheck->count()) {
$user = new User($hashCheck->first()->user_id);
$user->login();
}
}
$_link = DB::getConnected();
$url_parts = explode('/',$_SERVER['REQUEST_URI']);
$current = $url_parts[count($url_parts)-2];
if($current == 'page'){
$_SESSION['location'] = 1;
}
else{
$_SESSION['location'] = 0;
}
This is somewhat opinion-based, but I use a json or xml file that the config file parses. The update would call the file, parse it then resave it.
/core/init.php
$GLOBALS['config'] = json_decode(__DIR__.DS.'prefs.json', true);
/core/prefs.json
{"mysql":{"host":"localhost:3307","username":"root","password":"usbw","db":"webshop"},"remember":{"cookie_name":"hash","cookie_expiry":604800},"sessions":{"session_name":"user","token_name":"token"},"settings":{"main_color":"#069CDE","front_page_cat":"Best Verkocht,Populaire Producten","title_block_first":"GRATIS verzending van €50,-","title_block_second":"Vandaag besteld morgen in huis!"},"statics":{"header":"enabled","title_block":"enabled","menu":"enabled","slideshow":"enabled","left_box":"enabled","email_block":"enabled","footer":"enabled","keurmerken":"enabled","copyright":"enabled"}}
Pseudo-code for your update:
class Config
{
public static function update($path,$value)
{
# Fetch the file and convert it to json
$file = json_decode(file_get_contents(CONFIG_PATH),true);
# Do some code that will update the value....
# Save file back
file_put_contents(CONFIG_PATH,json_encode($data));
# Call your prefs to GLOBALS function here so the settings
# are updated in your global array
}
}
I have a php file, config.php and it contains following code.
$_config = array(
'db' => array(
'mysolidworks' => array(
'host' => 'localhost',
'username' => 'netvibes',
'password' => 'frakcovsea',
'dbname' => 'sw_mysolidworks',
'driver_options' => array(
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
)
)
)
);
I want to read this file into a variable and then read the variable like an array. Could anyone help?
Simply using PHP the way it's meant to work:
include('config.php');
echo $_config['db']['mysolidworks']['host'];
you can use any of the following
<?php
$file_loc = 'SomeDir/config.php';
require $file_loc; //Throw a fatal if it's not found
require_once $file_loc; //If this might be called somewhere else in your script, only include it once
include $file_loc; //Include but just show a warning if not found
include_once $file_loc; //Include if not already included and throw warning if not
?>
I am new to PHP and I've been following one tutorial where I am building a user registration system using OOP in PHP. I've been digesting the code quite well and learning fast, but encountered one minor (but frustrating) problem.
My web browser doesn't want to display echo'ed values if the echo command follows require_once command unless I add an extra echo '<br'> before the values that I want to echo. This is how it looks like:
<?php
require_once 'core/init.php';
echo Config::get('mysql/host');
This code above WILL NOT echo the last line in the browser.
<?php
require_once 'core/init.php';
echo '<br>';
echo Config::get('mysql/host');
The code above WILL echo the last line in the browser.
Why do I need to add echo '<br>' to display?
I will be very grateful for your advice.
Best,
Lucas
The code in core/init.php looks like that:
<?php
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'db' => 'lr'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user'
)
);
spl_autoload_register(function ($class) {
require_once 'classes/' .$class . '.php';
});
require_once '/../functions/sanitize.php';
echo $config['mysql']['host'];
I've tested the code and everything works fine. By the way you should consider not using $GLOBALS in your code.
Below the full working code:
index.php
<?php
require_once 'core/init.php';
echo Config::get('mysql/host');
core/init.php
<?php
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'db' => 'lr'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user'
)
);
spl_autoload_register(function ($class) {
require_once 'classes/' .$class . '.php';
});
require_once '/../functions/sanitize.php';
echo $config['mysql']['host'];
functions/sanitize.php
<?php
function escape ($string)
{
return htmlentities($string, END_QUOTES, 'UTF-8');
}
classes/Config.php
<?php
class Config {
static function get($what) {
$what = explode ('/', $what);
echo $GLOBALS['config'][$what[0]][$what[1]];
}
}
Result of launching index.php file in browser is:
127.0.0.1127.0.0.1
as expected (127.0.0.1 is being displayed 2 times without space between)