【问题标题】:How to change the encode url with php?如何用php更改编码url?
【发布时间】:2020-12-23 16:48:04
【问题描述】:

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

【问题讨论】:

  • 在循环外声明函数。
  • @ceejayoz 好的,我正在尝试做
  • 谢谢@ceejayoz 它对我有用,但我可以再请你帮忙吗???这种加密方式是否可行或使用安全。
  • 我不知道这段代码的目的是什么,为什么要加密这些数据等等,所以很难回答。
  • 我只是不想为此向用户显示 id 值

标签: php url decode encode


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多