Zen Cart Logo
Forums / General Questions / PHP Fatal error: Duplicate entry for key insert into sessions

PHP Fatal error: Duplicate entry for key insert into sessions

Locked

Views: 1,992

Results 1 to 3 of 3
This thread is locked. New replies are disabled.
6 Jan 2015, 12:22 AM
#1
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,957
Plugin Contributions:
8

PHP Fatal error: Duplicate entry for key insert into sessions

hi all,
i have done some research into this problem, and have yet to see an adequate solution. part of the problem (for me) is that it is difficult to recreate the steps that cause the problem to go off. in addition, most of the troubleshooting seems to revolve session variables contained in the php.ini file; some add-ons, etc.

i have decided to take a different tack on resolving this error. as far as i can tell, this error can only come from the function _sess_write in the includes/functions/sessions.php script as that is the only place an insert gets made into the sessions table. the code for this function is:

  function _sess_write($key, $val) {
    global $db;
    if (!is_object($db)) return;
    $val = base64_encode($val);

    global $SESS_LIFE;
    $expiry = time() + $SESS_LIFE;

    $qid = "select count(*) as total
            from " . TABLE_SESSIONS . "
            where sesskey = '" . zen_db_input($key) . "'";
    $total = $db->Execute($qid);

    if ($total->fields['total'] > 0) {
      $sql = "update " . TABLE_SESSIONS . "
              set expiry = '" . zen_db_input($expiry) . "', value = '" . zen_db_input($val) . "'
              where sesskey = '" . zen_db_input($key) . "'";
      $result = $db->Execute($sql);
    } else {
      $sql = "insert into " . TABLE_SESSIONS . "
              values ('" . zen_db_input($key) . "', '" . zen_db_input($expiry) . "', '" .
                       zen_db_input($val) . "')";
      $result = $db->Execute($sql);
    }
    return (!empty($result) && !empty($result->resource));
  }

why not change the sql INSERT into an INSERT ON DUPLICATE UPDATE command?? seems like that could resolve the issue as well reduce 3 mySQL statements into 1. the new function would look something like this:

  function _sess_write($key, $val) {
    global $db;
    if (!is_object($db)) return;
    $val = base64_encode($val);

    global $SESS_LIFE;
    $expiry = time() + $SESS_LIFE;

    $sql = "insert into " . TABLE_SESSIONS . "
              values ('" . zen_db_input($key) . "', '" . zen_db_input($expiry) . "', '" .
                       zen_db_input($val) . "')
              on duplicate key update expiry = '" . zen_db_input($expiry) . "', value = '" . zen_db_input($val) . "'";
    $result = $db->Execute($sql);
    return (!empty($result) && !empty($result->resource));
  }

any opinions from the zen team? it seems like a good solution to me...

6 Jan 2015, 1:32 AM
#3
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,957
Plugin Contributions:
8

Re: PHP Fatal error: Duplicate entry for key insert into sessions

sorry i missed that!

thanks for the quick reply.