0

I'm currently coding one of my first php applications. The application has to connect to a LDAP server and change some user attributes in the directory.

That application has some parameters to read in a mySQL Database in order to run. Some examples of these parameters could be: -LDAP Address -LDAP Service Account -LDAP Password

there are much more parameters, which rule, for example, the way users authenticate to my application,...

Currently, the database is read at each user session initialization, but, it doesn't have any sense because parameters do not vary from a session to another.

So, i'm looking for a way to load these parameters from the database, only one time (for example, at the php service initialization), and access to these parameters in the "normal" php code through variables.

What would be the best way to do this?

Thank you in advance.

2
  • We really need more information about what parameters you are trying to load to give a good answer here. Commented Jan 4, 2015 at 23:48
  • you would need something like xcache, memcache, apc/apcu to store this variables cross requests. Commented Jan 4, 2015 at 23:50

4 Answers 4

2

You are looking for a persistent cross-request storage. There are many options for this.

The simplest is APCu (which can be used in conjunction with Zend OpCache, or for PHP < 5.5, APC).

Simply:

if (apc_exists('mykey')) {
    $data = apc_fetch('mykey');
} else {
    // create it from scratch
    apc_store('mike', $data);
}

$data can be most any PHP type, arrays, objects, or scalars.

You can even put this code in the auto_prepend_file INI setting so it is run automatically on every request.

However: this is per server (and per SAPI, so mod_php/php-fpm/cli don't share the cache) so you will have to create it once per server.

Alternatively, for a multi-server setup you can use something like memcached or redis. These are stand-alone daemons that will let you store arbitrary key/value pairs of string data (so you may need to serialize()/unserialize() on the values).

I personally prefer memcache, which has two extensions for PHP, pecl/memcached and pecl/memcache (I prefer pecl/memcached, it has more features).

Both of them are pretty simple.

pecl/memcached:

$memcache = new Memcached();
$memcache->addServer('localhost', '11211');

$data = $memcache->get('mykey');
if (empty($data)) {
   // Create data
   $memcache->set('mykey', $data);
}

pecl/memcache:

$memcache = new Memcache();
$memcache->connect(); // uses localhost:11211, the default memcache host/port
$data = $memcache->get('mykey');
if (empty($data)) {
    // Create data
    $memcache->set('mykey', $data);
}

Both extensions support storage of arrays and objects without serialization.

You can of course store multiple keys with any of these solutions and just pull them all, instead of using one, or one with an array/object.

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

Comments

1

You can use Memcache do cache database requests. See here how to use.

Another way is using Php Sessions.

<?php
session_start(); // need to be before any html code
$_SESSION['something'] = 'Something here...';

echo $_SESSION['something']; // will show "Something here..."

And you can remove using...

unset($_SESSION['something']);

You also can use cookies, using the function setcookie. See here.

And you can get cookies using...

echo $_COOKIE['something'];

Production mode

In a production mode, this will work as set_transient of Wordpress. You will do the first db request to get the value and will cache this value using cookies, sessions or memcache.

If you want to show this values inside of your page, you can use a standard caching library.

2 Comments

A good answer, although I'd avoid using cookies for something like this, since it seems he uses the authentication/etc information, and most likely wouldn't want unwanted people viewing that info.
Well, if you are working with authentication, i don't recommend use "cookies" directly. But using sessions and a little bit of precaution, this can be done securely as well. Here we have a secure implementation of sessions: gist.github.com/eddmann/10262795
0

My understanding of the question is that you have some SQL data that is more or less constant and you don't want to have to read that in from the SQL connection on every request.

If that is the case you can use memcache to store the data: http://php.net/manual/en/book.memcache.php

The data will still be persistent and you will only need to go to the database if the cached data isn't there or needs to be refreshed.

If the data is specific to a particular user you can just use a session. http://php.net/manual/en/book.session.php

http://php.net/manual/en/session.examples.basic.php

Comments

0

If this is only to be used when starting up your server (so once and done) and you don't want to bother to with memcached/xcache (as they would be over kill) you can still use environment variables. See get_env

Comments

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.