Question

I'm developing a php application using codeigniter framework with hmvc library, the application will have modules, I want to have different memory limit for each module. Is this possible?

Was it helpful?

Solution

You might be able to add an .htaccess to the root of each module with memory limit config:

<IfModule mod_php5.c>
    php_value memory_limit 32M
</IfModule>

To update the file dynamically, you need to consider that .htaccess is a dangerous thing to expose.

I would take great pains to make sure there is NO way to allow user input (even yours because if you can do it someone else can do it) to be written to an .htaccess file.

That said, pseudo-code example (caveat emptor):

private function _set_mem_limit($limit = 0, $module = '')
{
    //modules
    $available_modules = array(
        'module1' => '/var/www/myApp/modules/module1/',
        'module2' => '/var/www/myApp/modules/module2/',
    );

    //limits
    $available_limits = array(
        '32'    => '32M',
        '64'    => '64M',
        '128'   => '128M',
        '256'   => '256M',
        '512'   => '512M'
    );

    $string = 'php_value memory_limit ';

    // make sure limit is an integer and it is in the available_limits array
    // if so, set string with the selected limit from array. otherwise, exit
    if(gettype($limit) === 'integer' and in_array($limit, $available_limits))
    {
        $string .= $available_limits[$limit];
    }
    else
    {
        die('Problem setting mem limit.');
    }

    // make sure incoming module variable is only alphanumeric and exists in
    // available_modules array. if so, write the limit to the file. otherwise, exit
    if(ctype_alpha($module) and in_array($module, $available_modules))
    {
        $path = $available_modules[$module].'.htaccess';
        $f = fopen($path, 'w');
        fwrite($f, $string);
        fclose($f);

    }
    else
    {
        die('Problem setting path for mem limit.');
    }
}

Even though this may work, I wouldn't trust it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top