Codeigniter 4:无法解密加密的帖子数据
Posted
技术标签:
【中文标题】Codeigniter 4:无法解密加密的帖子数据【英文标题】:Codeigniter 4: failed to decrypt encrypted post data 【发布时间】:2022-01-21 08:00:37 【问题描述】:对不起我的英语 我是 Codeigniter 4 的新手。我尝试从加密的发布请求中解密数据。我在视图中成功加密了我的数据,但在使用 javascript 发布后无法在我的控制器中再次解密。这是我的代码 我的控制器
<?php namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\Store_MDL;
class Home extends BaseController
protected $ProductModel;
protected $request;protected $session;protected $encrypt;
public function __construct()
$this->ProductModel = new Store_MDL();
$this->request = \Config\Services::request();
$this->encrypt = \Config\Services::encrypter();
$this->session = \Config\Services::session();
$this->session->start();
public function index()
$data['encrypter'] = $this->encrypt;
return view('home_page', $data);
public function encthis()
$val = $_POST['str'];
$encrypters = $this->encrypt;
//$ciphertext = $encrypters->encrypt('My secret message');
echo $encrypters->decrypt($val);
//echo $val;
return view('product/product-v');
这里是我的观点 home_page.php
<?php $n=0; foreach ($product as $rows)
$n++;
//echo count($rows);
if($n < count($rows))
$dev = '<div class="divider mb-3 mt-n2"></div>';
else$dev = '';
if($n % 2 == 0)
echo '<a href="#" value = "'.base_url().'/home/encthis" data-v="'.$encrypter->encrypt($rows['prno']).'" class = "n-link"><div class="row">
<div class="col-6">
<h1 class="font-20 mb-0 line-height-l">'.$rows['prname'].'</h1>
<p class="mb-3">
'.$rows['prdescription'].'
</p>
<h2 class="font-16 mb-0">Rp. '.$rows['prprice'].'</h2>
</div>
<div class="col-6">
<img src="'.base_url().'/public/uploads/'.$rows['prpath'].'/'.$rows['primage'].'" class="img-fluid rounded-circle shadow-xl">
</div>
</div></a>'.$dev;
else
echo '<a href="#" value = "'.base_url().'/home/encthis" data-v="'.$encrypter->encrypt($rows['prno']).'" class = "n-link"><div class="row">
<div class="col-6">
<img src="'.base_url().'/public/uploads/'.$rows['prpath'].'/'.$rows['primage'].'" class="img-fluid rounded-circle shadow-xl">
</div>
<div class="col-6">
<h1 class="font-18 mb-0 line-height-l">'.$rows['prname'].'</h1>
<p class="mb-3">
'.$rows['prdescription'].'
</p>
<h2 class="font-16 mb-0">Rp. '.$rows['prprice'].'</h2>
</div>
</div></a>'.$dev;
?>
比这里我的脚本
<script>
$('.n-link').on('click', function ()
var link = $(this).attr('value');
var pos = $(this).attr('data-v');
$.post(link, str:pos)
.done(function(data)
document.getElementById("context").innerhtml = data;
);
);
</script>
我总是收到通知 500(内部服务器错误)。
【问题讨论】:
【参考方案1】:经过几个小时的挣扎,我找到了一些线索, 换个方式
$encrypter->encrypt($rows['prno']);
base64_encode($encrypter->encrypt($rows['prno']));
在控制器中, 改变
$encrypters->decrypt($val);
$encrypters->decrypt(base64_decode($val));
希望有用
【讨论】:
以上是关于Codeigniter 4:无法解密加密的帖子数据的主要内容,如果未能解决你的问题,请参考以下文章