-1

i am having trouble with my open-cart site. I want to run a cron-job thru direct-admin. and i get this in my email:

PHP Notice: Undefined index: SERVER_PORT in /xxx/xxx/xxx/domain.com/public_html/system/startup.php on line 59

the code:

// Check if SSL
if ((isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) || $_SERVER['SERVER_PORT'] == 443) {
    $_SERVER['HTTPS'] = true;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
    $_SERVER['HTTPS'] = true;
} else {
    $_SERVER['HTTPS'] = false;
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Jun 20 '16 at 00:51
  • You don't give much information about your cron job but `SERVER_PORT` (together with some other keys form `$_SERVER`) does not make sense in the command line thus doesn't exist. – Álvaro González Jan 10 '17 at 15:18

3 Answers3

2

It seems to be a bug in opencart code in system/startup.php. You can see an open issue here

Following solution did work for me:
change line

if ((isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) || $_SERVER['SERVER_PORT'] == 443) { 

with

if ((isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) || (isset($_SERVER['HTTPS']) && $_SERVER['SERVER_PORT'] == 443)) { 

It's better if you do this using vqmod so you don't change the core file.

Maddish
  • 81
  • 6
1

I believe the real problem is that the command is called from cron and not from your web server (which would fill the _SERVER array). In the PHP file, all other keys except SERVER_PORT (like e.g. _SERVER['DOCUMENT_ROOT']) are filled with reasonable values if not set.

Try adding the following lines before the offending one (59):

if (!isset($_SERVER['SERVER_PORT'])) {
    $_SERVER['SERVER_PORT'] = getenv('SERVER_PORT');
}

and define SERVER_PORT as an environment variable before running the PHP command. If your port is always 80 or 443, you could also set it directly instead of getting it from the environment.

spassas
  • 4,778
  • 2
  • 31
  • 39
0

Change this if ((isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) || $_SERVER['SERVER_PORT'] == 443) {

to this:

if ((isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) || (isset($_SERVER['SERVER_PORT'])) && ($_SERVER['SERVER_PORT'] == 443)) {

Explanation: Seems like on your $_SERVER array, SERVER_PORT is not available - so PHP is showing a notice. You can fix it by checking if the key exists in the array before checking if the value is equal to 443.

  • Hello Muhammad, I replaced it. Now i get the following error.: PHP Parse error: syntax error, unexpected 'if' (T_IF) in /xxx/xxx/xxx/domain.com/public_html/system/system/startup.php on line 59 –  Jun 19 '16 at 10:55
  • Hello Muhammad, thanks for the reply again, when i replace the code, the entire website is not working anymore. So i think this is also not working. –  Jun 20 '16 at 16:35