6

I wanna know where is the best place to put the ini_set() functions, because I think when the ini_set function is inside the method like this:

private function archiveBackup() {
    ini_set('memory_limit', '128M');
    ini_set('max_execution_time', 0);
    ...
}

The ini_set function doesn't work?!

My script works like that: jQuery ajax query -> ajax.php file (make instance of the class and call some method) -> call the method of the class.

Where is the best place? In ajax.php file or at the start of class or inside methods?

David Harkness
  • 35,992
  • 10
  • 112
  • 134
  • As a side note remember that those ini values are set for the run of that script now. To revert in same script execution you must do it manually. – Sammaye Jun 25 '12 at 22:40

3 Answers3

6

In general, the best place to put ini_set calls is right at (or near) the start of the script. That way, it's pretty much the same as if they had been defined in the php.ini file in the first place.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Yes, but what if the shared hosting doesn't accept my php.ini file? –  Jun 25 '12 at 22:43
  • This will still work. Sinceit is defined in the PHP file. Kolink just mens its like defining it in the php.ini in the way it runs – Sammaye Jun 25 '12 at 22:43
  • If yuor shared hosting does not accept php.ini mods make sure that you are allowed to do ini_set. You might be breaing your terms and conditions by using this command. – Sammaye Jun 25 '12 at 22:44
  • @Sammaye If the host disables `php.ini`, they should be smart enough to use the `disable_functions` directive to disable `ini_set`. If not, they're not a good host. – Niet the Dark Absol Jun 25 '12 at 22:49
  • Yea I answered below cos I realised he is using the function in the right place but says it don't work. – Sammaye Jun 25 '12 at 22:50
  • Now it works, just I must increase the memory... From 128 to 512 :) But this is the only way to get my MySQL without system, shell or etc. :) –  Jun 25 '12 at 22:51
  • It was propbably working the whole time, your script just needed that much RAM. – Sammaye Jun 25 '12 at 22:59
1

I have noticed you have placed ini_set at the start of your main function which is most likely correct.

So:

The ini_set function doesn't work?!

Could be because your shared hosting provider has permission blocked you in some manner.

As Kolink says, it is likely the hosting provider is using disable_functions on you.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
0

Might be considered as a personal preference but I personally avoid using any kind of PHP configuration directives outside of my application configuration files. In other words if a certain class of my project requires specific PHP run-level configuration I note that requirement in the class description but still apply the configuration itself in my main configuration files.

  • 3
    While I agree with your preference in general, there are a couple of places (typically cron scripts) where I override some config settings - usually the memory limit. – Niet the Dark Absol Jun 25 '12 at 22:52
  • Never had an experience with cron that would require me to do something like that so I'll just have to take your word on it :) –  Jun 25 '12 at 22:54