分类 PHP 下的文章

做个人主页有时候会因为一些原因出现只能使用静态页面的情况,这时候想要在html中引入一些其他html文件,经过搜索学习发现有下面5种方法可以实现,有些需要特定环境才可以,有些比较通用,实现方法如下:


<ul>
    <li>iframe</li>
    <IFRAME SRC="iframe.html" frameborder="0"  width="100%" height="40px"></IFRAME>
    <li>object</li>
    <object style="border:0px" type="text/x-scriptlet" data="object.html" width=100% height=40></object>
    <li>Behavior_download(此方法只支持IE5-9浏览器,已过时)</li>
    <span id=showImport></span>
    <IE:Download ID="oDownload" STYLE="behavior:url(#default#download)" />
    <script>
        function onDownloadDone(downDate){
            showImport.innerHTML=downDate;
        }
        oDownload.startDownload('Behavior_download.html',onDownloadDone);
    </script>
    <li>javascript_window.onload</li>
    <span id=showInclude1></span>
    <li>javascript_window.addEventListener_load(推荐使用此方法,演示代码未优化,只是展示实现方法)</li>
    <span id=showInclude2></span>
    <script>
        var includeData = "read_file_var";
        function readAjaxFile(url){
            xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function(){
            if(xhr.readyState===4 && xhr.status===200){
                includeData = xhr.responseText;
            }
        }
            xhr.open("post",url,true);
            xhr.send(null);
        }
        window.onload = function(){
            readAjaxFile("jsreadfile1.html");
            setTimeout(function(){showInclude1.innerHTML=includeData},300);
        }
        window.addEventListener("load",function(){
            setTimeout(function(){readAjaxFile("jsreadfile2.html")},310);
            setTimeout(function(){showInclude2.innerHTML=includeData},610);
        });
    </script>
</ul>

效果展示如下:

- 阅读剩余部分 -

好久没有学习PHP了,今天继续学习源文件 install.php

 // 源文件 /install.php
1407    // display version
1408    if (install_is_cli()) {
1409        echo $options->generator . "\n";
1410        echo 'PHP ' . PHP_VERSION . "\n";
1411    }
  • 如果是在使用客户端运行网站代码就输出typecho和PHP的版本
 // 源文件 /install.php
1413    // install finished yet
1414    if (
1415        install_check('config')
1416        && install_check('db_structure')
1417        && install_check('db_data')
1418    ) {
1419        // redirect to siteUrl if not cli
1420        if (!install_is_cli()) {
1421            install_redirect($options->siteUrl);
1422        }
1423
1424        exit(1);
1425    }
  • 如果三个install_check()函数的返回值都为真,并且不是在用php客户端运行网站代码就执行install_redirect()函数,不管是不是在用PHP客户端运行网站代码都不会再往下执行后面的代码。
 // 源文件 /install.php
440 /**
441  * check install
442  *
443  * @param string $type
444  * @return bool
445  */
446 function install_check(string $type): bool
447 {
448     switch ($type) {
449         case 'config':
450            return file_exists(__TYPECHO_ROOT_DIR__ . '/config.inc.php');
451         case 'db_structure':
452         case 'db_data':
453             global $installDb;
454 
455             if (empty($installDb)) {
456                 return false;
457             }
458 
459             try {
460                 // check if table exists
461                 $installed = $installDb->fetchRow($installDb->select()->from('table.options')
462                     ->where('user = 0 AND name = ?', 'installed'));
463
464                 if ($type == 'db_data' && empty($installed['value'])) {
465                     return false;
466                 }
467             } catch (\Typecho\Db\Adapter\ConnectionException $e) {
468                 return true;
469             } catch (\Typecho\Db\Adapter\SQLException $e) {
470                 return false;
471             }
472
473             return true;
474         default:
475             return false;
476     }
477 }
  • 由于还未安装完毕case 'config'的返回值为假,case 'db_structure'内无代码,会执行default返回假,case 'db_data':因为$installDb为空返回值为假

最近都没有学习PHP了,今天接着上一章的内容继续学习源文件 install.php

 // 源文件 /install.php
1484
1485 install_dispatch();
1486
  • 在install.php文件末尾调用了install_dispatch()函数,这个函数也是在install.php文件中定义的
 // 源文件 /install.php
1392 /**
1393  * dispatch install action
1394  *
1395  */
1396 function install_dispatch()
1397 {
1398     // disable root url on cli mode
1399     if (install_is_cli()) {
1400         define('__TYPECHO_ROOT_URL__', 'http://localhost');
1401     }
1402
  • 根据install_is_cli()函数的返回值来决定 '__TYPECHO_ROOT_URL__' 变量的值,下面看看install_is_cli()函数的内容
 // 源文件 /install.php
61 /**
62  * detect cli mode
63  *
64  * @return bool
65  */
66 function install_is_cli(): bool
67 {
68     return \Typecho\Request::getInstance()->isCli();
69 }
70
  • 函数install_is_cli()的返回值为布尔型,返回的是\Typecho\Request::getInstance()->isCli()这个函数的值,接着看这个函数是个啥
 // 源文件 /var/Typecho/Request.php
440     /**
441      * @return bool
442      */
443     public function isCli(): bool
444     {
445         return php_sapi_name() == 'cli';
446     }
447
  • 函数isCli()返回的是表达式 php_sapi_name() == 'cli' 的比较结果,php_sapi_name:返回 web 服务器和 PHP 之间的接口类型。继续下面继续看install_dispatch()函数
 // 源文件 /install.php
1403    // init default options
1404    $options = \Widget\Options::alloc(install_get_default_options());
1405    \Widget\Init::alloc();
1406
  • install_get_default_options()在install.php第237行进行了定义,\Widget\Options::alloc()在/var/Typecho/Widget.php第185行进行了定义,\Widget\Init::alloc()与\Widget\Options::alloc()相同
 // 源文件 /install.php
232 /**
233  * list all default options
234  *
235  * @return array
236  */
237 function install_get_default_options(): array
238 {
239     static $options;
240
241     if (empty($options)) {
242         $options = [
            'theme' => 'default',
            'theme:default' => 'a:2:{s:7:"logoUrl";N;s:12:"sidebarBlock";a:5:{i:0;s:15:"ShowRecentPosts";i:1;s:18:"ShowRecentComments";i:2;s:12:"ShowCategory";i:3;s:11:"ShowArchive";i:4;s:9:"ShowOther";}}',
            'timezone' => '28800',
            'lang' => install_get_lang(),
            'charset' => 'UTF-8',
            'contentType' => 'text/html',
            'gzip' => 0,
            'generator' => 'Typecho ' . \Typecho\Common::VERSION,
            'title' => 'Hello World',
            'description' => 'Your description here.',
            'keywords' => 'typecho,php,blog',
            'rewrite' => 0,
            'frontPage' => 'recent',
            'frontArchive' => 0,
            'commentsRequireMail' => 1,
            'commentsWhitelist' => 0,
            'commentsRequireURL' => 0,
            'commentsRequireModeration' => 0,
            'plugins' => 'a:0:{}',
            'commentDateFormat' => 'F jS, Y \a\t h:i a',
            'siteUrl' => install_get_site_url(),
            'defaultCategory' => 1,
            'allowRegister' => 0,
            'defaultAllowComment' => 1,
            'defaultAllowPing' => 1,
            'defaultAllowFeed' => 1,
            'pageSize' => 5,
            'postsListSize' => 10,
            'commentsListSize' => 10,
            'commentsHTMLTagAllowed' => null,
            'postDateFormat' => 'Y-m-d',
            'feedFullText' => 1,
            'editorSize' => 350,
            'autoSave' => 0,
            'markdown' => 1,
            'xmlrpcMarkdown' => 0,
            'commentsMaxNestingLevels' => 5,
            'commentsPostTimeout' => 24 * 3600 * 30,
            'commentsUrlNofollow' => 1,
            'commentsShowUrl' => 1,
            'commentsMarkdown' => 0,
            'commentsPageBreak' => 0,
            'commentsThreaded' => 1,
            'commentsPageSize' => 20,
            'commentsPageDisplay' => 'last',
            'commentsOrder' => 'ASC',
            'commentsCheckReferer' => 1,
            'commentsAutoClose' => 0,
            'commentsPostIntervalEnable' => 1,
            'commentsPostInterval' => 60,
            'commentsShowCommentOnly' => 0,
            'commentsAvatar' => 1,
            'commentsAvatarRating' => 'G',
            'commentsAntiSpam' => 1,
            'routingTable' => serialize(install_get_default_routers()),
            'actionTable' => 'a:0:{}',
            'panelTable' => 'a:0:{}',
            'attachmentTypes' => '@image@',
            'secret' => \Typecho\Common::randString(32, true),
            'installed' => 0,
            'allowXmlRpc' => 2
304         ];
305     }
306
  • 函数定义了$options变量的值
 // 源文件   /var/Typecho/Widget.php
177     /**
178     * alloc widget instance
179     *
180     * @param mixed $params
181     * @param mixed $request
182     * @param bool|callable $disableSandboxOrCallback
183     * @return $this
184     */
185    public static function alloc($params = null, $request = null, $disableSandboxOrCallback = true): Widget
186    {
187        return self::widget(static::class, $params, $request, $disableSandboxOrCallback);
188    }
189
  • 函数返回一个widget类的实例,限于篇幅本章到此结束,下一章接着往下学习。