如何将数据从文本区域POST到PHP脚本并获得响应



这听起来很容易,但我仍在学习。我有一个文本区域,我把数据放在那里,我想把它发布到php脚本中解密

这是我的HTML:

<html>
<form action="php.php" method="post">
<textarea id="input" name="input" rows="4" cols="50"</textarea>
<input type="submit" name="decrypt" class="button" value="decrypt" />
</form>

<p><?php echo $decrypt; ?></p>

</html>

PHP代码:

<?php
function Decrypt($ciphertext)
{
$key = 1;
$c = base64_decode($ciphertext);
$ivlen = openssl_cipher_iv_length($cipher = "AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len = 32);
$ciphertext_raw = substr($c, $ivlen + $sha2len);
$original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, $key, true);
if (hash_equals($hmac, $calcmac)) {
return $original_plaintext;
}
}

if (!empty($_POST)) {
$decrypt = Decrypt($_POST['decrypt']);
print_r($_POST);
}

错误我正在接收"Warning: Undefined array key "decrypt" on line20"

预期行为:输出原始明文。

如果你想测试一条有效的加密线路,请使用:

W9aMvbRmmN/52Kmv1rr9i59ecKu2KYIhrL+Mj+dD8VE3BtwUiFIEBqrRc/e3aw8li2GKKu4B3FVyx/dkRnNnmw==
if (isset($_POST['submit'])) {
$decrypt = Decrypt($_POST['decrypt']);
}

然后在其他地方读取结果:

<p><?php echo isset($decrypt)? 'Decryption answer is ' . $decrypt : 'answer goes here' ; ?></p>

您还应该在表单中有文本区域:

<form action="php.php" method="post">
<textarea id="input" name="decrypt" rows="4" cols="50"></textarea>
<input type="submit" name="submit" class="button" />
</form>

这个名字太重要了。post数组接受表单中输入的名称,并使其成为$_post数组中的关键字,其值是用户在其中键入的值。

最新更新