我读到了一些有关如何使用PHP在HTML中通过post方法加密数据的帖子。但我真的无法弄清楚应该如何执行。
我来举个简单的例子。
我有一个仪表板,用户可以在其中查看其安装情况。想象一下有很多安装程序,每个程序都属于不同的用户。有些用户拥有一个安装程序,有些用户拥有五个安装程序。
事实是,我开发了一个按钮,用于从其安装程序下载Excel信息。
我已经完成了contactosDownload.php文件,并传递了$_GET['idInstalacion']参数,因此在主仪表板上按钮看起来像这样:
我来举个简单的例子。
我有一个仪表板,用户可以在其中查看其安装情况。想象一下有很多安装程序,每个程序都属于不同的用户。有些用户拥有一个安装程序,有些用户拥有五个安装程序。
事实是,我开发了一个按钮,用于从其安装程序下载Excel信息。
我已经完成了contactosDownload.php文件,并传递了$_GET['idInstalacion']参数,因此在主仪表板上按钮看起来像这样:
<a href="contactosDownload.php?idInstalacion=
<? echo $idInstalacionPOST ?>"
class="btnDownload btn-success btn btn-info pull-left">
<i class="fa fa-download"></i> Descargar
</a>
它的功能非常完美,当按钮被按下时,contactosDowload会获取idInstalacion并进行mysql查询,然后下载包含安装信息的Excel表格。 PHPExcel的表现非常出色。
即使这个用户有三个安装程序,也可以通过Ajax选择其中一个安装程序,并更新信息,我使用了jQuery进行更新:
$('#InstaSelect').change(function(e) {
e.preventDefault();
.....
$('.btnDownload').attr("href", "contactosDownload.php?idInstalacion="+idInstalacion);
....
});
这也是有效的。
就像你们所有人都认为,发送id并不是一个明智的想法。即使因为如果有人获得了链接,就可以开始使用以下方法检索数据:
http://www.aaaaa.com/contactosDownload.php?idInstalacion=1
http://www.aaaaa.com/contactosDownload.php?idInstalacion=2
请问什么是最佳选择来解密和加密信息,并使帖子信息更加安全?
谢谢。
编辑:
我忘了告诉你,用户在使用他们的用户名和密码进行授权。当他们登录时,我使用var session存储所有用户。
$user_browser = $_SERVER['HTTP_USER_AGENT']; // Get the user-agent string of the user.
$dbid = preg_replace("/[^0-9]+/", "", $dbid); // XSS protection as we might print this value
$_SESSION['user_id'] = $dbid;
$_SESSION['login_string'] = hash('sha512', $tContra.$user_browser);
$_SESSION['ultimoAcceso'] = date("Y-n-j H:i:s");
$_SESSION['tTipoUsuLogged'] = $tTipo;
$_SESSION['tRolUsuLogged'] = $tRolUsuario;
$_SESSION['tEmail'] = $tUseNam;
每个PHP文件都有(include 'validateSesion.php')来验证用户和超时。
现在我将在contactosDownload上引入此验证,如果用户未经身份验证,则会显示/index.php(登录)。
第一级完成。
现在我需要对POST中的ID进行编码,并检查SESSION中的用户是否有下载安装权限。
使用以下方式加密/解密ID怎么样:
function encryptor($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
//pls set your unique hashing key
$secret_key = 'long logn text secret';
$secret_iv = '1a2b3c4d5e';
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
//do the encyption given text/string/number
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
//decrypt the given text/string/number
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}