我有一个调用函数的php文件,并且从函数中收到“未定义的变量”通知。
PHP:
$secret_key = random_password_generate(32);
$secret_iv = random_password_generate(16);
$encrypted_name = encrypt_decrypt('encrypt', $name);
PHP函数:
function encrypt_decrypt($action, $string)
{
$output = false;
$encrypt_method = "AES-256-CBC";
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
} else if ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string),
$encrypt_method, $key, 0, $iv);
}
return $output;
}
我收到的通知是:
Notice: Undefined variable: secret_key in /myPathTo/functions.php on line 75
Notice: Undefined variable: secret_iv in /myPathTo/functions.php on line 77
有什么建议吗?
谢谢。