Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle race condition #244

Merged
merged 3 commits into from Dec 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions includes/classes/cache.php
Expand Up @@ -125,9 +125,9 @@ function sql_cache_store($zf_query, $zf_result_array) {
return true;
}
$result_serialize = $db->prepare_input(base64_encode(serialize($zf_result_array)));
$sql = "insert into " . TABLE_DB_CACHE . " set cache_entry_name = '" . $zp_cache_name . "',
cache_data = '" . $result_serialize . "',
cache_entry_created = '" . time() . "'";
$sql = "insert ignore into " . TABLE_DB_CACHE . " (cache_entry_name, cache_data, cache_entry_created) VALUES (:cachename, :cachedata, time() )";
$sql = $db->bindVars($sql, ':cachename', $zp_cache_name, 'string');
$sql = $db->bindVars($sql, ':cachedata', $result_serialize, 'string');
$db->Execute($sql);
return true;
break;
Expand Down
24 changes: 8 additions & 16 deletions includes/functions/sessions.php
Expand Up @@ -60,22 +60,14 @@ function _sess_write($key, $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);
}
$sql = "insert into " . TABLE_SESSIONS . " (sesskey, expiry, `value`)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the extra backticks around value are simply to provide specificity since value is a MySQL keyword too.

values (:zkey, :zexpiry, :zvalue)
ON DUPLICATE KEY UPDATE `value`=:zvalue, expiry=:zexpiry";
$sql = $db->bindVars($sql, ':zkey', $key, 'string');
$sql = $db->bindVars($sql, ':zexpiry', $expiry, 'integer');
$sql = $db->bindVars($sql, ':zvalue', $val, 'string');
$result = $db->Execute($sql);

return (!empty($result) && !empty($result->resource));
}

Expand Down