php教程 php手册 编写php拓展实例

编写php拓展实例

Jun 06, 2016 pm 07:46 PM
php yaf 회사 확장하다 액자 갑자기 쓰다

最近公司换了yaf框架,突然对用c实现php拓展感兴趣了,如果一个功能已经很稳定很成熟而且用的地方很多,那么我们就可以尝试用拓展实现(不一定每种情况都可以写成拓展),写成拓展后就不用每次用都包含一下,工具类直接随php启动加载进内存里。 我这次是把用

 

    最近公司换了yaf框架,突然对用c实现php拓展感兴趣了,如果一个功能已经很稳定很成熟而且用的地方很多,那么我们就可以尝试用拓展实现(不一定每种情况都可以写成拓展),写成拓展后就不用每次用都包含一下,工具类直接随php启动加载进内存里。

    我这次是把用户会话加密类写成了php的拓展,用户类是基于des加密的,主要实现了,

  1. isLogin //判断是否登录
  2. setLogin //就是种个加密的cookie
  3. getUid
  4. getUsername 

我给这个项目起名slime(史莱姆),勇者斗恶龙里的小怪,黏黏的。。。。。。

ok,先来看下php的实现,因为我们是php -> c,所以先看下php的实现,其实就是很普通用户类

<span> 1</span> <span>php
</span><span> 2</span> <span>class</span><span> Session_DES
</span><span> 3</span> <span>{
</span><span> 4</span>     <span>var</span> <span>$key</span><span>;
</span><span> 5</span>     <span>var</span> <span>$iv</span>; <span>//</span><span>偏移量</span>
<span> 6</span>     
<span> 7</span>     <span>function</span> Session_DES( <span>$key</span>="AAAAAAAA", <span>$iv</span>="BBBBBBBB"<span>) {
</span><span> 8</span>     <span>//</span><span>key长度8例如:1234abcd</span>
<span> 9</span>         <span>$this</span>-><span>key</span> = <span>$key</span><span>;
</span><span>10</span>         <span>if</span>( <span>$iv</span> == ""<span> ) {
</span><span>11</span>             <span>$this</span>->iv = <span>$key</span>; <span>//</span><span>默认以$key 作为 iv</span>
<span>12</span>         } <span>else</span><span> {
</span><span>13</span>             <span>$this</span>->iv = <span>$iv</span>; <span>//</span><span>mcrypt_create_iv ( mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );</span>
<span>14</span> <span>        }
</span><span>15</span> <span>    }
</span><span>16</span>     
<span>17</span>     <span>function</span> encrypt(<span>$str</span><span>) {
</span><span>18</span>     <span>//</span><span>加密,返回大写十六进制字符串</span>
<span>19</span>         <span>$size</span> = mcrypt_get_block_size ( MCRYPT_DES,<span> MCRYPT_MODE_CBC );
</span><span>20</span>         <span>$str</span> = <span>$this</span>->pkcs5Pad ( <span>$str</span>, <span>$size</span><span> );
</span><span>21</span>         <span>return</span> <span>strtoupper</span>( <span>bin2hex</span>( mcrypt_cbc(MCRYPT_DES, <span>$this</span>-><span>key</span>, <span>$str</span>, MCRYPT_ENCRYPT, <span>$this</span>-><span>iv ) ) );
</span><span>22</span> <span>    }
</span><span>23</span>     
<span>24</span>     <span>function</span> decrypt(<span>$str</span><span>) {
</span><span>25</span>     <span>//</span><span>解密</span>
<span>26</span>         <span>$strBin</span> = <span>$this</span>->hex2bin( <span>strtolower</span>( <span>$str</span><span> ) );
</span><span>27</span>         <span>$str</span> = mcrypt_cbc( MCRYPT_DES, <span>$this</span>-><span>key</span>, <span>$strBin</span>, MCRYPT_DECRYPT, <span>$this</span>-><span>iv );
</span><span>28</span>         <span>$str</span> = <span>$this</span>->pkcs5Unpad( <span>$str</span><span> );
</span><span>29</span>         <span>return</span> <span>$str</span><span>;
</span><span>30</span> <span>    }
</span><span>31</span>     
<span>32</span>     <span>function</span> hex2bin(<span>$hexData</span><span>) {
</span><span>33</span>         <span>$binData</span> = ""<span>;
</span><span>34</span>         <span>for</span>(<span>$i</span> = 0; <span>$i</span> strlen ( <span>$hexData</span> ); <span>$i</span> += 2<span>) {
</span><span>35</span>             <span>$binData</span> .= <span>chr</span> ( <span>hexdec</span> ( <span>substr</span> ( <span>$hexData</span>, <span>$i</span>, 2<span> ) ) );
</span><span>36</span> <span>        }
</span><span>37</span>         <span>return</span> <span>$binData</span><span>;
</span><span>38</span> <span>    }
</span><span>39</span> 
<span>40</span>     <span>function</span> pkcs5Pad(<span>$text</span>, <span>$blocksize</span><span>) {
</span><span>41</span>         <span>$pad</span> = <span>$blocksize</span> - (<span>strlen</span> ( <span>$text</span> ) % <span>$blocksize</span><span>);
</span><span>42</span>         <span>return</span> <span>$text</span> . <span>str_repeat</span> ( <span>chr</span> ( <span>$pad</span> ), <span>$pad</span><span> );
</span><span>43</span> <span>    }
</span><span>44</span>     
<span>45</span>     <span>function</span> pkcs5Unpad(<span>$text</span><span>) {
</span><span>46</span>         <span>$pad</span> = <span>ord</span> ( <span>$text</span> {<span>strlen</span> ( <span>$text</span> ) - 1<span>} );
</span><span>47</span>         <span>if</span> (<span>$pad</span> > <span>strlen</span> ( <span>$text</span><span> ))
</span><span>48</span>             <span>return</span> <span>false</span><span>;
</span><span>49</span>         <span>if</span> (<span>strspn</span> ( <span>$text</span>, <span>chr</span> ( <span>$pad</span> ), <span>strlen</span> ( <span>$text</span> ) - <span>$pad</span> ) != <span>$pad</span><span>)
</span><span>50</span>             <span>return</span> <span>false</span><span>;
</span><span>51</span>         <span>return</span> <span>substr</span> ( <span>$text</span>, 0, - 1 * <span>$pad</span><span> );
</span><span>52</span> <span>    }    
</span><span>53</span> }
로그인 후 복사

再来看Session_User

 

编写php拓展实例编写php拓展实例

<span>  1</span> <span>php
</span><span>  2</span> <span>define</span> ('BACK_AUTH_NAME','xxxx'); <span>//</span><span>用到的cookie名</span>
<span>  3</span> <span>class</span><span> Session_User { 
</span><span>  4</span>     <span>static</span> <span>$obj</span><span>;
</span><span>  5</span>     <span>private</span> <span>$uid</span><span>;
</span><span>  6</span>     <span>private</span> <span>$username</span><span>;
</span><span>  7</span>     <span>private</span> <span>$chineseName</span><span>;
</span><span>  8</span>     <span>public</span> <span>$auth_name</span> =<span> BACK_AUTH_NAME;
</span><span>  9</span>     <span>private</span> <span>$login_url</span> = <span>null</span><span>;
</span><span> 10</span>     <span>public</span> <span>$domain</span> = <span>null</span><span>;
</span><span> 11</span>     
<span> 12</span>     <span>private</span> <span>function</span>  __construct(<span>$login_url</span> = <span>null</span>, <span>$domain</span> = <span>null</span><span>){
</span><span> 13</span>         <span>$host</span> = <span>$_SERVER</span>["HTTP_HOST"<span>];
</span><span> 14</span>         <span>if</span> (!<span>$login_url</span><span>) {
</span><span> 15</span>             <span>$this</span>->login_url = "http://{<span>$host</span>}/login"<span>;
</span><span> 16</span> <span>        }
</span><span> 17</span>         <span>else</span><span> {
</span><span> 18</span>             <span>$this</span>->login_url = <span>$login_url</span><span>;
</span><span> 19</span> <span>        }
</span><span> 20</span>         
<span> 21</span>         <span>if</span> (!<span>$domain</span><span>) {
</span><span> 22</span>             <span>$domain</span> = <span>$_SERVER</span>["SERVER_NAME"<span>];
</span><span> 23</span>             <span>$this</span>->domain = <span>$domain</span><span>;
</span><span> 24</span> <span>        }
</span><span> 25</span>         
<span> 26</span>         <span>if</span> (<span>empty</span> ( <span>$_COOKIE</span> [<span>$this</span>-><span>auth_name] )) {
</span><span> 27</span>             <span>return</span><span>;
</span><span> 28</span> <span>        }
</span><span> 29</span>         
<span> 30</span>         <span>list</span> ( <span>$uid</span>, <span>$username</span>, <span>$ua</span>, <span>$tm</span>, <span>$chineseName</span> ) = @<span>$this</span>->decodeAuth (<span>$_COOKIE</span> [<span>$this</span>-><span>auth_name]);
</span><span> 31</span> 
<span> 32</span>         
<span> 33</span>         <span>//</span><span>ua检验</span>
<span> 34</span>         <span>if</span> (<span>empty</span> ( <span>$uid</span> ) || <span>$ua</span> !== <span>md5</span>(<span>$_SERVER</span> ['HTTP_USER_AGENT'<span>])) {
</span><span> 35</span>             <span>return</span><span>;
</span><span> 36</span> <span>        }
</span><span> 37</span> 
<span> 38</span>         <span>//</span><span>TODO:过期时间检验</span>
<span> 39</span>         
<span> 40</span>         <span>$this</span>->uid = <span>$uid</span><span>;
</span><span> 41</span>         <span>$this</span>->username = <span>$username</span><span>;
</span><span> 42</span>         <span>$this</span>->chineseName = <span>$chineseName</span><span>;
</span><span> 43</span> <span>    }
</span><span> 44</span>     
<span> 45</span>     <span>static</span> <span>public</span> <span>function</span> instance(<span>$login_url</span> = <span>null</span>, <span>$domain</span> = <span>null</span><span>){
</span><span> 46</span>         <span>if</span>(self::<span>$obj</span><span>)
</span><span> 47</span>             <span>return</span> self::<span>$obj</span><span>;
</span><span> 48</span>         <span>else</span><span>{
</span><span> 49</span>             self::<span>$obj</span> = <span>new</span> Session_User(<span>$login_url</span>, <span>$domain</span><span>);
</span><span> 50</span> <span>        }
</span><span> 51</span>         <span>return</span> self::<span>$obj</span><span>;
</span><span> 52</span> <span>    }
</span><span> 53</span> 
<span> 54</span>     
<span> 55</span>     <span>/*</span><span>*
</span><span> 56</span> <span>     * 用户是否登陆
</span><span> 57</span> <span>     * </span><span>*/</span>
<span> 58</span>     <span>public</span> <span>function</span><span> isLogin(){
</span><span> 59</span>         <span>if</span>(! <span>empty</span>(<span>$this</span>-><span>uid))
</span><span> 60</span>             <span>return</span> <span>true</span><span>; 
</span><span> 61</span>         <span>else</span>
<span> 62</span>             <span>return</span> <span>false</span><span>;
</span><span> 63</span> <span>    }
</span><span> 64</span>     <span>/*</span><span>*
</span><span> 65</span> <span>     * 
</span><span> 66</span> <span>     * 跳转到登录页面
</span><span> 67</span> <span>     * @param unknown_type $forward
</span><span> 68</span> <span>     * @param unknown_type $exit
</span><span> 69</span>      <span>*/</span>
<span> 70</span>     <span>public</span> <span>function</span> requireLogin(<span>$forward</span> = '', <span>$exit</span> = <span>true</span><span>){
</span><span> 71</span>         <span>if</span>(! <span>$this</span>-><span>isLogin()){
</span><span> 72</span>             <span>if</span>(<span>$forward</span> === <span>null</span><span>)
</span><span> 73</span> <span>            {
</span><span> 74</span>                 <span>header</span>("location: " . <span>$this</span>-><span>login_url);
</span><span> 75</span>                 
<span> 76</span> <span>            }
</span><span> 77</span>             <span>else</span>
<span> 78</span> <span>            {
</span><span> 79</span>                 <span>if</span>(<span>empty</span>(<span>$forward</span><span>))
</span><span> 80</span> <span>                {
</span><span> 81</span>                     <span>$forward</span> = 'http://'.<span>$_SERVER</span>['HTTP_HOST'].<span>$_SERVER</span>['REQUEST_URI'<span>];
</span><span> 82</span> <span>                }
</span><span> 83</span>                 <span>$forward</span> = <span>urlencode</span>(<span>$forward</span><span>);
</span><span> 84</span>                 <span>header</span>("location: ". <span>$this</span>->login_url . "?forward=<span>$forward</span>"<span>);
</span><span> 85</span> <span>            }
</span><span> 86</span>             <span>if</span>(<span>$exit</span><span>)
</span><span> 87</span>                 <span>exit</span><span>;
</span><span> 88</span> <span>        }
</span><span> 89</span> <span>    }
</span><span> 90</span>     <span>/*</span><span>*
</span><span> 91</span> <span>     * 
</span><span> 92</span> <span>     *设置登录状态
</span><span> 93</span> <span>     * @param unknown_type $uid
</span><span> 94</span> <span>     * @param unknown_type $username
</span><span> 95</span> <span>     * @param unknown_type $ua
</span><span> 96</span> <span>     * @param unknown_type $outtime
</span><span> 97</span>      <span>*/</span>
<span> 98</span>     
<span> 99</span>     <span>public</span> <span>function</span> setLogin(<span>$uid</span>, <span>$username</span>, <span>$ua</span> = <span>null</span>,<span>$outtime</span> = <span>null</span>, <span>$chineseName</span> = <span>null</span><span>){
</span><span>100</span>         <span>if</span>(<span>empty</span>(<span>$ua</span><span>)){
</span><span>101</span>             <span>$ua</span> = <span>$_SERVER</span>['HTTP_USER_AGENT'<span>];
</span><span>102</span> <span>        }
</span><span>103</span>         
<span>104</span>         <span>$str</span> = <span>$this</span>->encodeAuth(<span>$uid</span>, <span>$username</span>, <span>$ua</span>, <span>$chineseName</span><span>);
</span><span>105</span>         <span>setcookie</span>(<span>$this</span>->auth_name,<span>urlencode</span>(<span>$str</span>),<span>$outtime</span>,'/',<span>$this</span>-><span>domain);
</span><span>106</span> <span>    }
</span><span>107</span>     <span>/*</span><span>*
</span><span>108</span> <span>     * 用户退出
</span><span>109</span>       <span>*/</span>
<span>110</span>     <span>public</span> <span>function</span><span> setLogout(){
</span><span>111</span>         <span>setcookie</span>(<span>$this</span>->auth_name,'',-1,'/',<span>$this</span>-><span>domain);
</span><span>112</span> <span>    }
</span><span>113</span>     
<span>114</span>     <span>public</span> <span>function</span> __get(<span>$key</span><span>){
</span><span>115</span>         <span>if</span>('uid' == <span>$key</span><span>){
</span><span>116</span>             <span>return</span> <span>$this</span>-><span>uid;
</span><span>117</span>         }<span>elseif</span> ('username' == <span>$key</span><span>) {
</span><span>118</span>             <span>return</span> <span>$this</span>-><span>username;
</span><span>119</span>         }<span>elseif</span> ('chineseName' == <span>$key</span><span>) {
</span><span>120</span>             <span>return</span> <span>$this</span>-><span>chineseName;
</span><span>121</span> <span>        }
</span><span>122</span>         <span>return</span><span> ;
</span><span>123</span> <span>    }
</span><span>124</span>     
<span>125</span>     <span>public</span>  <span>function</span><span> getUid(){
</span><span>126</span>         <span>return</span> <span>$this</span>-><span>uid;
</span><span>127</span> <span>    }    
</span><span>128</span>     
<span>129</span>     <span>public</span> <span>function</span><span> getUserName(){
</span><span>130</span>         <span>return</span> <span>$this</span>-><span>username;
</span><span>131</span> <span>    }    
</span><span>132</span>     
<span>133</span>     <span>public</span> <span>function</span><span> getChineseName(){
</span><span>134</span>         <span>return</span> <span>$this</span>-><span>chineseName;
</span><span>135</span> <span>    }
</span><span>136</span> 
<span>137</span>     <span>/*</span><span>*
</span><span>138</span> <span>     * 生成加密的登陆cookie
</span><span>139</span>      <span>*/</span>
<span>140</span>     <span>private</span> <span>function</span>  encodeAuth(<span>$uid</span>,<span>$username</span>,<span>$ua</span>,<span>$chineseName</span>=<span>null</span><span>){
</span><span>141</span>         <span>$tm</span> = <span>time</span><span>();
</span><span>142</span>         <span>$ua</span> = <span>md5</span>(<span>$ua</span><span>);
</span><span>143</span>         <span>$info</span> = "<span>$uid</span>\t<span>$username</span>\t<span>$ua</span>\t<span>$tm</span>\t<span>$chineseName</span>"<span>;
</span><span>144</span>         <span>$des</span> = <span>new</span><span> Session_DES();
</span><span>145</span>         <span>$str</span> = <span>$des</span>->encrypt(<span>$info</span><span>);
</span><span>146</span>         <span>return</span> <span>$str</span><span>;
</span><span>147</span> <span>    }
</span><span>148</span> 
<span>149</span>     <span>/*</span><span>*
</span><span>150</span> <span>     * 解析加密cookie 
</span><span>151</span>      <span>*/</span>
<span>152</span>     <span>private</span> <span>function</span> decodeAuth(<span>$str</span><span>){
</span><span>153</span>         <span>$des</span> = <span>new</span><span> Session_DES();
</span><span>154</span>         <span>$info</span> = <span>explode</span>("\t",@<span>$des</span>->decrypt(<span>$str</span><span>));
</span><span>155</span>         <span>if</span>(<span>is_array</span>(<span>$info</span><span>)){
</span><span>156</span>             <span>return</span> <span>$info</span><span>;
</span><span>157</span>         }<span>else</span><span>{
</span><span>158</span>             <span>return</span> <span>array</span><span>();
</span><span>159</span> <span>        }
</span><span>160</span> <span>    }
</span><span>161</span>     
<span>162</span>     <span>public</span> <span>function</span> auth(<span>$controller</span>,<span>$action</span><span>)
</span><span>163</span> <span>    {
</span><span>164</span>         <span>if</span>(!<span>in_array</span>(<span>$controller</span>,<span>$conArr</span><span>)){
</span><span>165</span>             <span>return</span> <span>false</span><span>;
</span><span>166</span> <span>        }
</span><span>167</span>         <span>if</span>(!<span>in_array</span>(<span>$action</span>,<span>$actArr</span><span>)){
</span><span>168</span>             <span>return</span> <span>false</span><span>;
</span><span>169</span> <span>        }
</span><span>170</span>         <span>return</span> <span>true</span><span>;
</span><span>171</span> <span>    }
</span><span>172</span> }
로그인 후 복사
View Code

结合上面两段代码,分离出来其实得到的会话类(常常做的就是把一样的的跟不一样的分离出来),只需要3个变量两个是des用到的iv偏移量,key加密秘钥,跟cookie名,然后我们就可以写我们的拓展了,其中踩到了很多坑,一些makefile,config.w4等小语法,lib依赖,总之还是学到了很多

代码地址:https://github.com/lietdai/ldclass

 

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

이번 장에서는 라우팅과 관련된 다음과 같은 주제를 학습하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

CakePHP 데이터베이스 작업 CakePHP 데이터베이스 작업 Sep 10, 2024 pm 05:25 PM

CakePHP에서 데이터베이스 작업은 매우 쉽습니다. 이번 장에서는 CRUD(생성, 읽기, 업데이트, 삭제) 작업을 이해하겠습니다.

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

See all articles