问题描述
我正在开发一个项目,我需要在其中对我传递的 ID 进行编码,以便不向用户显示我的 ID。所以我开发了它,但我收到了一个错误,如“致命错误:无法重新声明 encryptString()(先前声明)”,当我将它与循环一起使用时会显示这一点,但没有循环它工作得很好。我正在粘贴代码,希望您能找出其中的任何错误。 这是我的代码
<div id="cars">
<?PHP
$sl_rp = "select * from $tb_products where status='active' order by id DESC";
$res_rp = MysqLi_query($conn,$sl_rp);
if($res_rp){
$nums_rp = MysqLi_num_rows($res_rp);
if($nums_rp > 0){
$i=0;
while($rows_rp = MysqLi_fetch_assoc($res_rp)){
// $productid = $rows_rp['id'].'/'.'asdfghjklqweretyuiophjgbcfhdgvsf';
// $t_encode = base64_encode($productid);
$id = $rows_rp['id'];
function encryptString($plaintext,$password,$encoding = null) {
$iv = openssl_random_pseudo_bytes(16);
$ciphertext = openssl_encrypt($plaintext,"AES-256-CBC",hash('sha256',true),OPENSSL_RAW_DATA,$iv);
$hmac = hash_hmac('sha256',$ciphertext.$iv,true);
return $encoding == "hex" ? bin2hex($iv.$hmac.$ciphertext) : ($encoding == "base64" ? base64_encode($iv.$hmac.$ciphertext) : $iv.$hmac.$ciphertext);
}
function decryptString($ciphertext,$encoding = null) {
$ciphertext = $encoding == "hex" ? hex2bin($ciphertext) : ($encoding == "base64" ? base64_decode($ciphertext) : $ciphertext);
if (!hash_equals(hash_hmac('sha256',substr($ciphertext,48).substr($ciphertext,16),16,32))) return null;
return openssl_decrypt(substr($ciphertext,48),16));
}
echo $enc = encryptString($id,"myPassword","hex");
$i++;
?>
<div class="item">
<div class="teaser <?PHP if($i % 3 == 0){ echo "card-3"; }else if($i % 2 == 0){ echo "card-2"; }else{ echo "card-1"; } ?> transp with_padding big-padding margin_0">
<div class="media xxs-media-left">
<div class="media-left media-middle">
<div class="teaser_icon size_small big_wrapper">
<img src="upload_products/<?PHP echo $rows_rp['p_upload']; ?>" class="img-responsive img-center mar-top-5">
</div>
</div>
<div class="media-body media-middle">
<h4 class="text-white"><?PHP echo $rows_rp['pname']; ?></h4>
<a href="fill-details.PHP?product=<?PHP echo $enc; ?>" class="btn btn-default text-capitalize card_buttons">Order Now</a>
</div>
</div>
</div>
</div>
<?PHP
}
}
}
?>
</div>
解决方法
首先我想说@ceejayoz 提供的建议是正确的,请使用文件中的功能,就像您使用下面的连接代码一样,那么您将不会遇到任何问题,这是一个安全的方法,但没有什么是安全的,尽管您可以使用它。