Home  >  Article  >  Backend Development  >  Analysis of the function of encrypted parameters passed by U method in thinkPHP

Analysis of the function of encrypted parameters passed by U method in thinkPHP

不言
不言Original
2018-06-09 14:22:202218browse

This article mainly introduces the U method encrypted parameter transfer function in thinkPHP, and analyzes the relevant operating techniques of thinkPHP using U method to encrypt parameters when passing parameters through get, combined with examples. Friends who need it can refer to it

The example in this article describes the U method encrypted parameter passing function in thinkPHP. Share it with everyone for your reference, the details are as follows:

The U method in thinkPHP is used to assemble the URL address. The corresponding URL address can be automatically generated based on the current URL mode and settings.

The specific code is as follows:

<?php
/**
 * 简单对称加密算法之加密
 * @param String $string 需要加密的字串
 * @param String $skey 加密EKY
 */
function encode($string = &#39;&#39;, $skey = &#39;yourkey&#39;) {
 $strArr = str_split(base64_encode($string));
 $strCount = count($strArr);
 foreach (str_split($skey) as $key => $value)
  $key < $strCount && $strArr[$key].=$value;
 return str_replace(array(&#39;=&#39;, &#39;+&#39;, &#39;/&#39;), array(&#39;O0O0O&#39;, &#39;o000o&#39;, &#39;oo00o&#39;), join(&#39;&#39;, $strArr));
}
/**
 * 简单对称加密算法之解密
 * @param String $string 需要解密的字串
 * @param String $skey 解密KEY
 */
function decode($string = &#39;&#39;, $skey = &#39;yourkey&#39;) {
 $strArr = str_split(str_replace(array(&#39;O0O0O&#39;, &#39;o000o&#39;, &#39;oo00o&#39;), array(&#39;=&#39;, &#39;+&#39;, &#39;/&#39;), $string), 2);
 $strCount = count($strArr);
 foreach (str_split($skey) as $key => $value)
  $key <= $strCount && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
 return base64_decode(join(&#39;&#39;, $strArr));
}
/**
将以上两个函数放在Common下的function.php公共函数中。
用法:常用语get传参
 前端:<a href="<{:U(&#39;Index/view&#39;,array(&#39;id&#39;=>encode($data[&#39;id&#39;]),&#39;name&#39;=>encode($data[&#39;title&#39;])))}>" rel="external nofollow" ><{$data.title}></a>
 后台:view方法中:$id = decode(trim(I("get.id")));即可还原
 view模板中:<font color="red"><{$Think.get.name|decode}></font>
**/
/*建议将key自行修改,尽量不要太长,不然url很长,适当即可,加密性能很好,亲测*/

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis of the N method of ThinkPHP

Usage analysis of the I method of ThinkPHP

The above is the detailed content of Analysis of the function of encrypted parameters passed by U method in thinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn