読者です 読者をやめる 読者になる 読者になる

memo

memo dayo.

超簡易Key-Value型API

php


■__init__.php


// ==============================================
if (function_exists('date_default_timezone_set') !== False) {
date_default_timezone_set('Asia/Tokyo');
}
// ==============================================
// ---- define ----
// dir
define('ZZZ_DIR_HOME' , dirname(__FILE__) . DIRECTORY_SEPARATOR);
define('ZZZ_DIR_TMP' , ZZZ_DIR_HOME . 'tmp' . DIRECTORY_SEPARATOR);
// check
if (is_dir(ZZZ_DIR_TMP) === False) {
mkdir(ZZZ_DIR_TMP);
chmod(ZZZ_DIR_TMP, 0777);
}
if (is_writable(ZZZ_DIR_TMP) === False) {
echo '[' . __FILE__ . '](' . __LINE__ . ')';
exit(0);
}
// ==============================================
// ---- ini_set ----
// memory
ini_set('max_execution_time', 60);
ini_set('memory_limit', '22M');
ini_set('post_max_size', '21M');
ini_set('upload_max_filesize', '20M');
// session
ini_set('session.auto_start', False);
ini_set('session.use_cookies', False);
ini_set('session.use_trans_sid', True);
ini_set('session.gc_divisor', 100);
ini_set('session.gc_probability', 0);
ini_set('session.gc_maxlifetime', PHP_INT_MAX);
ini_set('session.save_handler', 'files');
ini_set('session.name', 'sess');
ini_set('session.save_path', ZZZ_DIR_TMP);
// ==============================================
function set_data ($str_idx, $str_val) {
// init
$bol_return = False;

// check
if (strlen($str_idx) === 0) {
return $bol_return;
}
if (strlen($str_val) === 0) {
return $bol_return;
}

// write
session_id(sha1($str_idx));
session_start();
$_SESSION['zzz'] = '' . $str_val;

// end
$bol_return = True;
return $bol_return;
}
// ==============================================
function del_data ($str_idx) {
// init
$bol_return = False;

// check
if (strlen($str_idx) === 0) {
return $bol_return;
}

// delete
session_id(sha1($str_idx));
session_start();
$_SESSION['zzz'] = '';
$_SESSION = array();
session_destroy();

// end
$bol_return = True;
return $bol_return;
}
// ==============================================
function get_data ($str_idx) {
// init
$str_return = '';

// check
if (strlen($str_idx) === 0) {
return $str_return;
}

// read
session_id(sha1($str_idx));
session_start();
$str_return = $_SESSION['zzz'];

// end
return $str_return;
}
// ==============================================


■set.php


require_once('__init__.php');
// ==============================================
// ---- init ----
$all = array();
$all['zzz'] = False;

// ---- get param ----
$all['req'] = $_REQUEST;

// ---- check param ----
if ( (isset($all['req']['i']) === False) || (strlen($all['req']['i']) === 0)) {
header('content-type: application/octet-stream');
header('content-length: ' . strlen( (int)$all['zzz']));
echo (int)$all['zzz'];
exit(0);
}
if ( (isset($all['req']['v']) === False) || (strlen($all['req']['v']) === 0)) {
header('content-type: application/octet-stream');
header('content-length: ' . strlen( (int)$all['zzz']));
echo (int)$all['zzz'];
exit(0);
}

// ---- get data ----
$all['zzz'] = set_data($all['req']['i'], $all['req']['v']);

// ---- output ----
header('content-type: application/octet-stream');
header('content-length: ' . strlen( (int)$all['zzz']));
echo (int)$all['zzz'];

// ---- end ----
unset($all);
exit(0);


■get.php


require_once('__init__.php');
// ==============================================
// ---- init ----
$all = array();
$all['zzz'] = '';

// ---- get param ----
$all['req'] = $_REQUEST;

// ---- check param ----
if ( (isset($all['req']['i']) === False) || (strlen($all['req']['i']) === 0)) {
header('content-type: application/octet-stream');
header('content-length: ' . strlen($all['zzz']));
echo $all['zzz'];
exit(0);
}

// ---- get data ----
$all['zzz'] = get_data($all['req']['i']);

// ---- output ----
header('content-type: application/octet-stream');
header('content-length: ' . strlen($all['zzz']));
echo $all['zzz'];

// ---- end ----
unset($all);
exit(0);

広告を非表示にする