Helper for system operation.
- 1. Outline
- 2. Description
- 3. Installation
- 4. Dependency
- 5. Usage
- 6. Example
- 7. Contributing
2. Description
3. Installation
$ composer require nueip/helpers4. Dependency
- PHP7
5. Usage
- library for Cookie operating
- Wrap the PHP native function/variable setcookie()/$_COOKIE to enrich the function and simplify the usage method.
- Extra features:
- Support Cookie prefix packaging
- Support default values: expires, path, domain, secure, httponly
- Dedicated function: Root domain access
- 使用前設定
// 引入Composer autoload
include('../vendor/autoload.php');
// 宣告使用 SystemHelper
use nueip\SystemHelper;最小記憶體設定
- 以M為單位,不做單位轉換
- 如果現有可用記憶體大於本次設定值,不會縮減
- 建議:一般程式不超過512M,匯出程式不超過1024M
- 最大 2048M
memoryMoreThan( [string `$memorySize`] ) : bool
$memorySize (*string*) – 記憶體大小,單位 M,預設 256M
bool
use nueip\helpers\SystemHelper;
SystemHelper::memoryMoreThan('256M');
echo ini_get('memory_limit');
// Result: 256M- library for Cookie operating
- Wrap the PHP native function/variable setcookie()/$_COOKIE to enrich the function and simplify the usage method.
- Extra features:
- Support Cookie prefix packaging
- Support default values: expires, path, domain, secure, httponly
- Dedicated function: Root domain access
- 使用前設定
// 引入Composer autoload
include('../vendor/autoload.php');
// 宣告使用 SystemHelper
use marsapp\helper\system\SystemHelper;
// 常數設定 - 需設 Cookie前綴、過期時間、預設路徑、根網域
define('COOKIE_DEFAULT_PREFIX', 'dev_');
define('COOKIE_DEFAULT_EXPIRES', 0);
define('COOKIE_DEFAULT_PATH', '/');
define('COOKIE_ROOT_DOMAIN', 'dev.local');cookieOption(string $param, string $value = null) : string|null
- $param : string
- The target parameter of the cookie default options.
- $value : string
- New data.
- When $param does not exist, return null
- When there is $param but no $value(null), get the current value of $param
- When there is $param and $value, after updating the data, the updated value will be returned
string|null
// 取得Cookie預設參數值
$httponly = SystemHelper::cookieOption('httponly');
// 設定Cookie預設參數-單筆
$httponly = SystemHelper::cookieOption('httponly', false);cookieOptions(array $options = []) : array
- $options : array
- batch update cookie default options.
array, return complete cookie default options.
// 變數設定
$options = [
// 存放路徑
'path' => '/tmp/',
// 是否只能通過HTTP協議訪問
'httponly' => false,
];
// 回傳完整Cookie預設參數
$cookieOptions = SystemHelper::cookieOptions();
// 設定Cookie預設參數-多筆
$cookieOptions = SystemHelper::cookieOptions($options);cookieSet(string $name, string $value = "", array $options = []) : bool
Set Cookie, Wrap the PHP native function setcookie() to simplify usage.
- $name : string
- The name of the cookie.
- $value : string
- The value of the cookie.
- $options : array
- 'prefix' => '', // Cookie前綴,預設無前綴
- 'expires' => time()+3600, // 過期時間,預設無期限
- 'path' => '/', // 存放路徑
- 'domain' => "", // 所屬網域
- 'secure' => true, // 是否只在https生效
- 'httponly' => false, // 是否只能通過HTTP協議訪問
bool, like PHP function setcookie()
// 變數設定
$name = 'testCookie';
$value = "test-" . mt_rand(1111, 9999);
$options = [
// 存放路徑
'path' => '/',
// 是否只能通過HTTP協議訪問
'httponly' => true,
];
// 設定Cookie
SystemHelper::cookieSet($name, $value, $options);
- $options可用參數
- 'prefix' => '', // Cookie前綴,預設無前綴
- 'expires' => time()+3600, // 過期時間,預設無期限
- 'path' => '/', // 存放路徑
- 'domain' => "", // 所屬網域
- 'secure' => true, // 是否只在https生效
- 'httponly' => false, // 是否只能通過HTTP協議訪問
cookieSetRoot(string $name, string $value = "", array $options = []) : bool
Like
cookieSet(), but domain fixed on root domain.
cookieGet(string $name) : mixed
取得目標Cookie值
cookieExpired(string $name, array $options = []) : bool
Delete target cookie.
cookieExpiredRoot(string $name, array $options = []) : bool
Like
cookieExpired(), but domain fixed on root domain.
6. Example
see: example/example-cookie.php
7. Contributing
- 20210506: MarsHung Fork Cookie function from https://github.com/marshung24/system-helper
- 20210623: MarsHung Fork Cookie function cookieOption(), cookieOptions(), COOKIE_DEFAULT_PREFIX from https://github.com/marshung24/system-helper