如何将编码 url 更改为 php?
How to change the encode url with php?
我正在开发一个项目,我需要在其中对传递的 ID 进行编码,这样我的 ID 就不会显示给用户。所以我已经开发了它,但是我收到了一个错误,因为 'Fatal error: Cannot redeclare encryptString() (previously declared )' 当我将它与循环一起使用但没有循环它工作得很好时会显示这一点。我正在粘贴代码,希望您能发现其中的任何错误。
这是我的代码
<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', $password, true), OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext.$iv, hash('sha256', $password, true), true);
return $encoding == "hex" ? bin2hex($iv.$hmac.$ciphertext) : ($encoding == "base64" ? base64_encode($iv.$hmac.$ciphertext) : $iv.$hmac.$ciphertext);
}
function decryptString($ciphertext, $password, $encoding = null) {
$ciphertext = $encoding == "hex" ? hex2bin($ciphertext) : ($encoding == "base64" ? base64_decode($ciphertext) : $ciphertext);
if (!hash_equals(hash_hmac('sha256', substr($ciphertext, 48).substr($ciphertext, 0, 16), hash('sha256', $password, true), true), substr($ciphertext, 16, 32))) return null;
return openssl_decrypt(substr($ciphertext, 48), "AES-256-CBC", hash('sha256', $password, true), OPENSSL_RAW_DATA, substr($ciphertext, 0, 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 提供的建议是正确的,请在您的文件中使用该功能,就像您使用下面的连接代码一样,那么您将不会遇到任何问题,这个是一种安全的方法,但尽管您可以使用它,但在互联网上没有什么是安全的。
我正在开发一个项目,我需要在其中对传递的 ID 进行编码,这样我的 ID 就不会显示给用户。所以我已经开发了它,但是我收到了一个错误,因为 'Fatal error: Cannot redeclare encryptString() (previously declared )' 当我将它与循环一起使用但没有循环它工作得很好时会显示这一点。我正在粘贴代码,希望您能发现其中的任何错误。 这是我的代码
<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', $password, true), OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext.$iv, hash('sha256', $password, true), true);
return $encoding == "hex" ? bin2hex($iv.$hmac.$ciphertext) : ($encoding == "base64" ? base64_encode($iv.$hmac.$ciphertext) : $iv.$hmac.$ciphertext);
}
function decryptString($ciphertext, $password, $encoding = null) {
$ciphertext = $encoding == "hex" ? hex2bin($ciphertext) : ($encoding == "base64" ? base64_decode($ciphertext) : $ciphertext);
if (!hash_equals(hash_hmac('sha256', substr($ciphertext, 48).substr($ciphertext, 0, 16), hash('sha256', $password, true), true), substr($ciphertext, 16, 32))) return null;
return openssl_decrypt(substr($ciphertext, 48), "AES-256-CBC", hash('sha256', $password, true), OPENSSL_RAW_DATA, substr($ciphertext, 0, 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 提供的建议是正确的,请在您的文件中使用该功能,就像您使用下面的连接代码一样,那么您将不会遇到任何问题,这个是一种安全的方法,但尽管您可以使用它,但在互联网上没有什么是安全的。