0

I am trying to learn using php for practical applications by studying opencart source code and I am stuck at a point

// Register Globals
if (ini_get('register_globals')) {
ini_set('session.use_cookies', 'On');
ini_set('session.use_trans_sid', 'Off');

session_set_cookie_params(0, '/');
session_start();

$globals = array($_REQUEST, $_SESSION, $_SERVER, $_FILES);

foreach ($globals as $global) {
    foreach(array_keys($global) as $key) {
        unset(${$key}); 
    }
} 
}
  1. What I understand is we are trying to unset all the session variables,but if the session was not started earlier why we need to unset it ?
  2. What do unset(${$key}) do exactly ?

  3. Why opencart uses myisam engine ?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Krrish Raj
  • 1,505
  • 12
  • 28
  • `unset(${$key})` unsets the variable with the name held by the variable `$key`. Imagine that `$global` is `$_SESSION` for instance; then one element returned from `array_keys()` could be `"USERNAME"` (for instance), meaning that `unset(${$key})` will unset the variable `$USERNAME`. – Max Aug 27 '14 at 06:15
  • I got your point but is it a threat if the variables with same name exist ? – Krrish Raj Aug 27 '14 at 06:22
  • I mean one is simply a variable and other is a key of an array.Is it just the threat of mistyping or something else too ? – Krrish Raj Aug 27 '14 at 06:54
  • Is `register_globals` set? Because in that case, check out: http://stackoverflow.com/questions/3593210/what-are-register-globals-in-php – Max Aug 27 '14 at 16:28

2 Answers2

1

1) It's clearing variables, that has keys in $_SESSION, $_REQUEST, $_SERVER and $_FILES variables, if they are empty, than inner loop iteration for that variable will be skipped (there is no need to check if it's set, since it will always be set by default).
E.g. if there is variable $foo and $_SESSION['foo'] is set, than it will unset $foo, not $_SESSION['foo'].

2) It's variable variable. If $key = 'foo', than unset(${$key}) will unset $foo variable.

3) Ask opencart developers why.

Justinas
  • 41,402
  • 5
  • 66
  • 96
0

Justinas is correct, the section of code you're looking at is basically removing a number of global variables to force the OpenCart developer to use the internal methods available ($this->request->server[] and $this->session for example). The OC developers have done this so that they can ensure the data is consistently handled throughout the store code.

The choice of myisam I believe is for backwards compatibility. When OC 1.x was developed innodb wasn't as widely supported as it currently is. They're working on OC 2 at the moment and I think that will have a choice. There are quite a few blocks of code out there that will change the database format and apply additional indexes for performance.

OpenCart is actually a really nice platform to cut your teeth in MVC principals, however as you mention you're learning PHP, I'd have a hunt round for some generic PHP code too. Depending what you're after doing, MVC could well be overkill.

PeteAUK
  • 968
  • 6
  • 16