首页 > 解决方案 > 如何用php更改编码url?

问题描述

我正在开发一个项目,我需要对我传递的 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', $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>

标签: phpurldecodeencode

解决方案


首先我想说@ceejayoz 提供的建议是正确的,请使用文件中的功能,就像您使用下面的连接代码一样,您将不会遇到任何问题,这是一种安全的方法,但是尽管您可以使用互联网,但没有什么是安全的。


推荐阅读