超実践的な PHP 関数のまとめ
PHP の暗号化と復号化関数は、一部の有用な文字列を暗号化してデータベースに保存し、その文字列を可逆的に復号化するために使用できます。この関数は、base64 および MD5 の暗号化と復号化を使用します。
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> encryptDecrypt(<span style="color: #800080;">$key</span>, <span style="color: #800080;">$string</span>, <span style="color: #800080;">$decrypt</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$decrypt</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #800080;">$decrypted</span> = <span style="color: #008080;">rtrim</span>(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, <span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span>), <span style="color: #008080;">base64_decode</span><span style="color: #008080;"> 6</span> (<span style="color: #800080;">$string</span>), MCRYPT_MODE_CBC, <span style="color: #008080;">md5</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span>))), "12"<span style="color: #000000;">); </span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$decrypted</span><span style="color: #000000;">; </span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span> <span style="color: #800080;">$encrypted</span> = <span style="color: #008080;">base64_encode</span>(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, <span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span>),<span style="color: #008080;">13</span> <span style="color: #800080;">$string</span>, MCRYPT_MODE_CBC, <span style="color: #008080;">md5</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span><span style="color: #000000;">)))); </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$encrypted</span><span style="color: #000000;">; </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #000000;"> } </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> }
使用方法は次のとおりです:
<span style="color: #008080;">1</span> <span style="color: #008000;">//</span><span style="color: #008000;">以下是将字符串“Helloweba欢迎您”分别加密和解密 </span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #008000;">//加密: </span><span style="color: #008080;">4</span> <span style="color: #008080;">5</span> <span style="color: #0000ff;">echo</span> encryptDecrypt('password', 'Helloweba欢迎您',0<span style="color: #000000;">); </span><span style="color: #008080;">6</span> <span style="color: #008080;">7</span> <span style="color: #008000;">//</span><span style="color: #008000;">解密: </span><span style="color: #008080;">8</span> <span style="color: #008080;">9</span> <span style="color: #0000ff;">echo</span> encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);
を生成します。必要な場合 次の関数を使用して、ランダムな名前、一時パスワード、その他の文字列を生成できます:
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> generateRandomString(<span style="color: #800080;">$length</span> = 10<span style="color: #000000;">) { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #800080;">$characters</span> = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #800080;">$randomString</span> = ''<span style="color: #000000;">; </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> < <span style="color: #800080;">$length</span>; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #800080;">$randomString</span> .= <span style="color: #800080;">$characters</span>[<span style="color: #008080;">rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$characters</span>) - 1<span style="color: #000000;">)]; </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #000000;"> } </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$randomString</span><span style="color: #000000;">; </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> }
使用方法は次のとおりです:
<span style="color: #008080;">1</span> <span style="color: #0000ff;">echo</span> generateRandomString(20);
次の関数は、ファイル拡張子または接尾辞を迅速に取得できます。
<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span> getExtension(<span style="color: #800080;">$filename</span><span style="color: #000000;">){ </span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #800080;">$myext</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$filename</span>, <span style="color: #008080;">strrpos</span>(<span style="color: #800080;">$filename</span>, '.'<span style="color: #000000;">)); </span><span style="color: #008080;">4</span> <span style="color: #008080;">5</span> <span style="color: #0000ff;">return</span> <span style="color: #008080;">str_replace</span>('.','',<span style="color: #800080;">$myext</span><span style="color: #000000;">); </span><span style="color: #008080;">6</span> <span style="color: #008080;">7</span> }
使用方法は次のとおりです。
<span style="color: #008080;">1</span> <span style="color: #800080;">$filename</span> = '我的文档.doc'<span style="color: #000000;">; </span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #0000ff;">echo</span> getExtension(<span style="color: #800080;">$filename</span>);
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> formatSize(<span style="color: #800080;">$size</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #800080;">$sizes</span> = <span style="color: #0000ff;">array</span>(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"<span style="color: #000000;">); </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$size</span> == 0<span style="color: #000000;">) { </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #0000ff;">return</span>('n/a'<span style="color: #000000;">); </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #0000ff;">return</span> (<span style="color: #008080;">round</span>(<span style="color: #800080;">$size</span>/<span style="color: #008080;">pow</span>(1024, (<span style="color: #800080;">$i</span> = <span style="color: #008080;">floor</span>(<span style="color: #008080;">log</span>(<span style="color: #800080;">$size</span>, 1024)))), 2) . <span style="color: #800080;">$sizes</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]); </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #000000;"> } </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> }使用方法は次のとおりです。
<span style="color: #008080;">1</span> <span style="color: #800080;">$thefile</span> = <span style="color: #008080;">filesize</span>('test_file.mp3'<span style="color: #000000;">); </span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #0000ff;">echo</span> formatSize(<span style="color: #800080;">$thefile</span>);5. PHP はタグ文字を置き換えます 文字列とテンプレート タグを指定されたコンテンツに置き換えるには、次の関数を使用できます:
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> stringParser(<span style="color: #800080;">$string</span>,<span style="color: #800080;">$replacer</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #800080;">$result</span> = <span style="color: #008080;">str_replace</span>(<span style="color: #008080;">array_keys</span>(<span style="color: #800080;">$replacer</span>), <span style="color: #008080;">array_values</span>(<span style="color: #800080;">$replacer</span>),<span style="color: #800080;">$string</span><span style="color: #000000;">); </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">; </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #800080;">$string</span> = '<span style="color: #000000;">The {b}anchor text{/b} is the {b}actual word{/b} or words used </span><span style="color: #008080;">12</span> {br}to describe the link {br}itself'<span style="color: #000000;">; </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #800080;">$replace_array</span> = <span style="color: #0000ff;">array</span>('{b}' => '<b>','{/b}' => '</b>','{br}' => '<br >'<span style="color: #000000;">); </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #0000ff;">echo</span> stringParser(<span style="color: #800080;">$string</span>,<span style="color: #800080;">$replace_array</span>);</br >6. PHP はディレクトリ
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> listDirFiles(<span style="color: #800080;">$DirPath</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$dir</span> = <span style="color: #008080;">opendir</span>(<span style="color: #800080;">$DirPath</span><span style="color: #000000;">)){ </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">while</span>((<span style="color: #800080;">$file</span> = <span style="color: #008080;">readdir</span>(<span style="color: #800080;">$dir</span>))!== <span style="color: #0000ff;">false</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_dir</span>(<span style="color: #800080;">$DirPath</span>.<span style="color: #800080;">$file</span><span style="color: #000000;">)) </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #000000;"> { </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #0000ff;">echo</span> "filename: <span style="color: #800080;">$file</span><br >"<span style="color: #000000;">; </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #000000;"> } </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #000000;"> } </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #000000;"> } </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #000000;">}</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> listDirFiles('home/some_folder/');7. PHP は現在のページの URL を取得します
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> curPageURL() { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #800080;">$pageURL</span> = 'http'<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_SERVER</span>['HTTPS'])) {<span style="color: #800080;">$pageURL</span> .= "s"<span style="color: #000000;">;} </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #800080;">$pageURL</span> .= "://"<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_SERVER</span>["SERVER_PORT"] != "80"<span style="color: #000000;">) { </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #800080;">$pageURL</span> .= <span style="color: #800080;">$_SERVER</span>["SERVER_NAME"].":".<span style="color: #800080;">$_SERVER</span>["SERVER_PORT"].<span style="color: #800080;">$_SERVER</span><span style="color: #008080;">12</span> ["REQUEST_URI"<span style="color: #000000;">]; </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #800080;">$pageURL</span> .= <span style="color: #800080;">$_SERVER</span>["SERVER_NAME"].<span style="color: #800080;">$_SERVER</span>["REQUEST_URI"<span style="color: #000000;">]; </span><span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #000000;"> } </span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$pageURL</span><span style="color: #000000;">; </span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #000000;">}</span><span style="color: #008080;">23</span> <span style="color: #008080;">24</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span> <span style="color: #0000ff;">echo</span> curPageURL();8. PHP はファイルを強制的にダウンロードします
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> download(<span style="color: #800080;">$filename</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">if</span> ((<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$filename</span>))&&(<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">))){ </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #008080;">header</span>("Content-length: ".<span style="color: #008080;">filesize</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">)); </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #008080;">header</span>('Content-Type: application/octet-stream'<span style="color: #000000;">); </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #008080;">header</span>('Content-Disposition: attachment; filename="' . <span style="color: #800080;">$filename</span> . '"'<span style="color: #000000;">); </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #008080;">readfile</span>("<span style="color: #800080;">$filename</span>"<span style="color: #000000;">); </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #0000ff;">echo</span> "Looks like file does not exist!"<span style="color: #000000;">; </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #000000;"> } </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #000000;">}</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> download('/down/test_45f73e852.zip');9. PHP は文字列の長さをインターセプトします
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/*</span> <span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #008000;"> Utf-8、gb2312都支持的汉字截取函数 </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #008000;"> cut_str(字符串, 截取长度, 开始长度, 编码); </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #008000;"> 编码默认为 utf-8 </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #008000;"> 开始长度默认为 0 </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">function</span> cutStr(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$sublen</span>, <span style="color: #800080;">$start</span> = 0, <span style="color: #800080;">$code</span> = 'UTF-8'<span style="color: #000000;">){ </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$code</span> == 'UTF-8'<span style="color: #000000;">){ </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #800080;">$pa</span> = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"<span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #008080;">preg_match_all</span>(<span style="color: #800080;">$pa</span>, <span style="color: #800080;">$string</span>, <span style="color: #800080;">$t_string</span><span style="color: #000000;">); </span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #008080;">23</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">count</span>(<span style="color: #800080;">$t_string</span>[0]) - <span style="color: #800080;">$start</span> > <span style="color: #800080;">$sublen</span>) <span style="color: #0000ff;">return</span> <span style="color: #008080;">join</span>('', <span style="color: #008080;">array_slice</span><span style="color: #008080;">24</span> (<span style="color: #800080;">$t_string</span>[0], <span style="color: #800080;">$start</span>, <span style="color: #800080;">$sublen</span>))."..."<span style="color: #000000;">; </span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span> <span style="color: #0000ff;">return</span> <span style="color: #008080;">join</span>('', <span style="color: #008080;">array_slice</span>(<span style="color: #800080;">$t_string</span>[0], <span style="color: #800080;">$start</span>, <span style="color: #800080;">$sublen</span><span style="color: #000000;">)); </span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">29</span> <span style="color: #008080;">30</span> <span style="color: #800080;">$start</span> = <span style="color: #800080;">$start</span>*2<span style="color: #000000;">; </span><span style="color: #008080;">31</span> <span style="color: #008080;">32</span> <span style="color: #800080;">$sublen</span> = <span style="color: #800080;">$sublen</span>*2<span style="color: #000000;">; </span><span style="color: #008080;">33</span> <span style="color: #008080;">34</span> <span style="color: #800080;">$strlen</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$string</span><span style="color: #000000;">); </span><span style="color: #008080;">35</span> <span style="color: #008080;">36</span> <span style="color: #800080;">$tmpstr</span> = ''<span style="color: #000000;">; </span><span style="color: #008080;">37</span> <span style="color: #008080;">38</span> <span style="color: #008080;">39</span> <span style="color: #008080;">40</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span><<span style="color: #800080;">$strlen</span>; <span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #008080;">41</span> <span style="color: #008080;">42</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span>>=<span style="color: #800080;">$start</span> && <span style="color: #800080;">$i</span><(<span style="color: #800080;">$start</span>+<span style="color: #800080;">$sublen</span><span style="color: #000000;">)){ </span><span style="color: #008080;">43</span> <span style="color: #008080;">44</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">ord</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$i</span>, 1))>129<span style="color: #000000;">){ </span><span style="color: #008080;">45</span> <span style="color: #008080;">46</span> <span style="color: #800080;">$tmpstr</span>.= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$i</span>, 2<span style="color: #000000;">); </span><span style="color: #008080;">47</span> <span style="color: #008080;">48</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">49</span> <span style="color: #008080;">50</span> <span style="color: #800080;">$tmpstr</span>.= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$i</span>, 1<span style="color: #000000;">); </span><span style="color: #008080;">51</span> <span style="color: #008080;">52</span> <span style="color: #000000;"> } </span><span style="color: #008080;">53</span> <span style="color: #008080;">54</span> <span style="color: #000000;"> } </span><span style="color: #008080;">55</span> <span style="color: #008080;">56</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">ord</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$i</span>, 1))>129) <span style="color: #800080;">$i</span>++<span style="color: #000000;">; </span><span style="color: #008080;">57</span> <span style="color: #008080;">58</span> <span style="color: #000000;"> } </span><span style="color: #008080;">59</span> <span style="color: #008080;">60</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$tmpstr</span>)<<span style="color: #800080;">$strlen</span> ) <span style="color: #800080;">$tmpstr</span>.= "..."<span style="color: #000000;">; </span><span style="color: #008080;">61</span> <span style="color: #008080;">62</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$tmpstr</span><span style="color: #000000;">; </span><span style="color: #008080;">63</span> <span style="color: #008080;">64</span> <span style="color: #000000;"> } </span><span style="color: #008080;">65</span> <span style="color: #008080;">66</span> <span style="color: #000000;">}</span><span style="color: #008080;">67</span> <span style="color: #008080;">68</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">69</span> <span style="color: #008080;">70</span> <span style="color: #800080;">$str</span> = "jQuery插件实现的加载图片和页面效果"<span style="color: #000000;">; </span><span style="color: #008080;">71</span> <span style="color: #008080;">72</span> <span style="color: #0000ff;">echo</span> cutStr(<span style="color: #800080;">$str</span>,16);10. PHP はクライアントの実際の IP を取得します
<span style="color: #008080;"> 1</span> <span style="color: #008000;">//</span><span style="color: #008000;">获取用户真实IP </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getIp() { </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">if</span> (<span style="color: #008080;">getenv</span>("HTTP_CLIENT_IP") && <span style="color: #008080;">strcasecmp</span>(<span style="color: #008080;">getenv</span>("HTTP_CLIENT_IP"),<span style="color: #008080;"> 6</span> "unknown"<span style="color: #000000;">)) </span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #800080;">$ip</span> = <span style="color: #008080;">getenv</span>("HTTP_CLIENT_IP"<span style="color: #000000;">); </span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> <span style="color: #0000ff;">else</span> <span style="color: #008080;">11</span> <span style="color: #008080;">12</span> <span style="color: #0000ff;">if</span> (<span style="color: #008080;">getenv</span>("HTTP_X_FORWARDED_FOR") && <span style="color: #008080;">strcasecmp</span>(<span style="color: #008080;">getenv</span>("HTTP_X_FORWARDED_FOR"), "unknown"<span style="color: #000000;">)) </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #800080;">$ip</span> = <span style="color: #008080;">getenv</span>("HTTP_X_FORWARDED_FOR"<span style="color: #000000;">); </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #0000ff;">else</span> <span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #0000ff;">if</span> (<span style="color: #008080;">getenv</span>("REMOTE_ADDR") && <span style="color: #008080;">strcasecmp</span>(<span style="color: #008080;">getenv</span>("REMOTE_ADDR"), "unknown"<span style="color: #000000;">)) </span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span> <span style="color: #800080;">$ip</span> = <span style="color: #008080;">getenv</span>("REMOTE_ADDR"<span style="color: #000000;">); </span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #0000ff;">else</span> <span style="color: #008080;">23</span> <span style="color: #008080;">24</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span> (<span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR']) && <span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR'] && <span style="color: #008080;">25</span> <span style="color: #008080;">strcasecmp</span>(<span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR'], "unknown"<span style="color: #000000;">)) </span><span style="color: #008080;">26</span> <span style="color: #008080;">27</span> <span style="color: #800080;">$ip</span> = <span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR'<span style="color: #000000;">]; </span><span style="color: #008080;">28</span> <span style="color: #008080;">29</span> <span style="color: #0000ff;">else</span> <span style="color: #008080;">30</span> <span style="color: #008080;">31</span> <span style="color: #800080;">$ip</span> = "unknown"<span style="color: #000000;">; </span><span style="color: #008080;">32</span> <span style="color: #008080;">33</span> <span style="color: #0000ff;">return</span> (<span style="color: #800080;">$ip</span><span style="color: #000000;">); </span><span style="color: #008080;">34</span> <span style="color: #008080;">35</span> <span style="color: #000000;">}</span><span style="color: #008080;">36</span> <span style="color: #008080;">37</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">38</span> <span style="color: #008080;">39</span> <span style="color: #0000ff;">echo</span> getIp();11. PHP は SQL インジェクションを防止します
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> injCheck(<span style="color: #800080;">$sql_str</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #800080;">$check</span> = <span style="color: #008080;">preg_match</span>('<span style="color: #000000;">/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into</span><span style="color: #008080;"> 4</span> |load_file|outfile/', <span style="color: #800080;">$sql_str</span><span style="color: #000000;">); </span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$check</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">echo</span> '非法字符!!'<span style="color: #000000;">; </span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> <span style="color: #0000ff;">exit</span><span style="color: #000000;">; </span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$sql_str</span><span style="color: #000000;">; </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #000000;"> } </span><span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #000000;">}</span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #0000ff;">echo</span> injCheck('1 or 1=1');12. PHP ページのプロンプトとジャンプ
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> message(<span style="color: #800080;">$msgTitle</span>,<span style="color: #800080;">$message</span>,<span style="color: #800080;">$jumpUrl</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$str</span> = '<!DOCTYPE HTML>'<span style="color: #000000;">; </span><span style="color: #008080;"> 3</span> <span style="color: #800080;">$str</span> .= '<html>'<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span> <span style="color: #800080;">$str</span> .= '<head>'<span style="color: #000000;">; </span><span style="color: #008080;"> 5</span> <span style="color: #800080;">$str</span> .= '<meta charset="utf-8">'<span style="color: #000000;">; </span><span style="color: #008080;"> 6</span> <span style="color: #800080;">$str</span> .= '<title>页面提示</title>'<span style="color: #000000;">; </span><span style="color: #008080;"> 7</span> <span style="color: #800080;">$str</span> .= '<style type="text/css">'<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #800080;">$str</span> .= '<span style="color: #000000;">*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial, sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; </span><span style="color: #008080;">10</span> line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}'<span style="color: #000000;">; </span><span style="color: #008080;">11</span> <span style="color: #800080;">$str</span> .= '</style>'<span style="color: #000000;">; </span><span style="color: #008080;">12</span> <span style="color: #800080;">$str</span> .= '</head>'<span style="color: #000000;">; </span><span style="color: #008080;">13</span> <span style="color: #800080;">$str</span> .= '<body>'<span style="color: #000000;">; </span><span style="color: #008080;">14</span> <span style="color: #800080;">$str</span> .= '<div>'<span style="color: #000000;">; </span><span style="color: #008080;">15</span> <span style="color: #800080;">$str</span> .= '<h3>'.<span style="color: #800080;">$msgTitle</span>.'</h3>'<span style="color: #000000;">; </span><span style="color: #008080;">16</span> <span style="color: #800080;">$str</span> .= '<div>'<span style="color: #000000;">; </span><span style="color: #008080;">17</span> <span style="color: #800080;">$str</span> .= '<h4>'.<span style="color: #800080;">$message</span>.'</h4>'<span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #800080;">$str</span> .= '<p>系统将在 <span style="color:blue;font-weight:bold">3</span> 秒后自动跳转,如果不想等待,直接点击 <a href="{$jumpUrl}">这里</a> 跳转</p>'<span style="color: #000000;">; </span><span style="color: #008080;">19</span> <span style="color: #800080;">$str</span> .= "<script>setTimeout('location.replace(\'".<span style="color: #800080;">$jumpUrl</span>."\')',2000)</script>"<span style="color: #000000;">; </span><span style="color: #008080;">20</span> <span style="color: #800080;">$str</span> .= '</div>'<span style="color: #000000;">; </span><span style="color: #008080;">21</span> <span style="color: #800080;">$str</span> .= '</div>'<span style="color: #000000;">; </span><span style="color: #008080;">22</span> <span style="color: #800080;">$str</span> .= '</body>'<span style="color: #000000;">; </span><span style="color: #008080;">23</span> <span style="color: #800080;">$str</span> .= '</html>'<span style="color: #000000;">; </span><span style="color: #008080;">24</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$str</span><span style="color: #000000;">; </span><span style="color: #008080;">25</span> <span style="color: #000000;">}</span><span style="color: #008080;">26</span> <span style="color: #008080;">27</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下 </span><span style="color: #008080;">28</span> <span style="color: #008080;">29</span> message('操作提示','操作成功!','http://www.helloweba.com/');13. PHP 計算時間
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> changeTimeType(<span style="color: #800080;">$seconds</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$seconds</span> > 3600<span style="color: #000000;">) { </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #800080;">$hours</span> = <span style="color: #008080;">intval</span>(<span style="color: #800080;">$seconds</span> / 3600<span style="color: #000000;">); </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #800080;">$minutes</span> = <span style="color: #800080;">$seconds</span> % 3600<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #800080;">$time</span> = <span style="color: #800080;">$hours</span> . ":" . <span style="color: #008080;">gmstrftime</span>('%M:%S', <span style="color: #800080;">$minutes</span><span style="color: #000000;">); </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #800080;">$time</span> = <span style="color: #008080;">gmstrftime</span>('%H:%M:%S', <span style="color: #800080;">$seconds</span><span style="color: #000000;">); </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #000000;"> } </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$time</span><span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #000000;">}</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> <span style="color: #800080;">$seconds</span> = 3712<span style="color: #000000;">; </span><span style="color: #008080;">24</span> <span style="color: #008080;">25</span> <span style="color: #0000ff;">echo</span> changeTimeType(<span style="color: #800080;">$seconds</span>);