За последние 24 часа нас посетил 58551 программист и 1791 робот. Сейчас ищут 850 программистов ...

Правдоли может ускорить

Тема в разделе "PHP для новичков", создана пользователем alizee, 21 дек 2018.

  1. alizee

    alizee Новичок

    С нами с:
    31 май 2018
    Сообщения:
    15
    Симпатии:
    0
    правдоли может ускорить скорасть сайта этат скрипт
    PHP:
    1. <?php
    2. function start() {
    3.    ob_start();
    4. }
    5.  
    6. function output($compress = true, $use_etag = true, $send_body = true) {
    7. $min_gz_size = 1024;
    8. $page = ob_get_contents();
    9. $length = strlen($page);
    10.  
    11. if ($compress && extension_loaded('zlib') &&
    12.     (strlen($page) > $min_gz_size) &&
    13.     isset($globals['http_server_vars']['http_accept_encoding'])) {
    14.    $ae = explode(',', str_replace(' ', '', $globals['http_server_vars']['http_accept_encoding']));
    15.    $enc = false;
    16.    if (in_array('gzip', $ae)) {
    17.     $enc = 'gzip';
    18.    } else if (in_array('x-gzip', $ae))
    19.     $enc = 'x-gzip';
    20.  
    21.    if ($enc) {
    22.     $length = strlen($page);
    23.     header('content-encoding: ' . $enc);
    24.     header('vary: accept-encoding');
    25.    } else {
    26.     $compress = false;
    27.    }
    28. } else
    29.    $compress = false;
    30.  
    31. if ($use_etag) {
    32.    $etag = '"' . md5($page) . '"';
    33.    header('etag: ' . $etag);
    34.    if (isset($globals['http_server_vars']['http_if_none_match'])) {
    35.     $inm = explode(',', $globals['http_server_vars']['http_if_none_match']);
    36.     foreach ($inm as $i) {
    37.        if (trim($i) == $etag) {
    38.         header('http/1.0 304 not modified');
    39.         $send_body = false;
    40.         break;
    41.        }
    42.     }
    43.    }
    44. }
    45.  
    46. if ($send_body) {
    47.    header('content-length: ' . $length);
    48.    echo $page;
    49.    }
    50. }
    51. ?>
    PHP:
    1. 1. В .htaccess добавить:
    2.  
    3. FileETag MTime Size
    4. <ifmodule mod_expires.c>
    5. <filesmatch ".(jpg|jpeg|gif|png|ico|css|js)$">
    6. ExpiresActive on
    7. ExpiresDefault "access plus 1 month"
    8. </filesmatch>
    9. </ifmodule>
    10. <ifModule mod_gzip.c>
    11.   mod_gzip_on Yes
    12.   mod_gzip_dechunk Yes
    13.   mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
    14.   mod_gzip_item_include handler ^cgi-script$
    15.   mod_gzip_item_include mime ^text/.*
    16.   mod_gzip_item_include mime ^application/x-javascript.*
    17.   mod_gzip_item_exclude mime ^image/.*
    18.   mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
    19. </ifModule>
    20. <ifModule mod_expires.c>
    21.   ExpiresActive On
    22.   ExpiresDefault "access plus 1 seconds"
    23.   ExpiresByType text/html "access plus 1 seconds"
    24.   ExpiresByType image/gif "access plus 2592000 seconds"
    25.   ExpiresByType image/jpeg "access plus 2592000 seconds"
    26.   ExpiresByType image/png "access plus 2592000 seconds"
    27.   ExpiresByType text/css "access plus 604800 seconds"
    28.   ExpiresByType text/javascript "access plus 216000 seconds"
    29.   ExpiresByType application/x-javascript "access plus 216000 seconds"
    30. </ifModule>
    31. <ifModule mod_headers.c>
    32.   <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
    33.     Header set Cache-Control "max-age=2592000, public"
    34.   </filesMatch>
    35.   <filesMatch "\\.(css)$">
    36.     Header set Cache-Control "max-age=604800, public"
    37.   </filesMatch>
    38.   <filesMatch "\\.(js)$">
    39.     Header set Cache-Control "max-age=216000, private"
    40.   </filesMatch>
    41.   <filesMatch "\\.(xml|txt)$">
    42.     Header set Cache-Control "max-age=216000, public, must-revalidate"
    43.   </filesMatch>
    44.   <filesMatch "\\.(html|htm|php)$">
    45.     Header set Cache-Control "max-age=1, private, must-revalidate"
    46.   </filesMatch>
    47. </ifModule>
    48.  
    49. 2. Работем в файлах index.php и templates.php
    50. 2.1 Заружаем accelerator.php в /system/classes/
    51.  
    52. 2.2 Открить index.php
    53.  
    54. Перед:
    55.  
    56. define('ENGINE_DIR', ROOT_DIR.'/system');
    57.  
    58. Добавить:
    59.  
    60. include('/system/classes/accelerator.php');
    61.  
    62. 2.3 Открить templates.php
    63.  
    64. После:
    65.  
    66. function load_template($tpl_name) {
    67.  
    68. Добавить:
    69.  
    70. if( $this->CacheTemplate[ $tpl_name ] )
    71.     {
    72.         $this->copy_template = $this->CacheTemplate[ $tpl_name ];
    73.         return true;
    74.     }
    75.    
    76. После:
    77.  
    78. $this->copy_template = $this->template;
    79.  
    80. Добавить:
    81.  
    82. $this->CacheTemplate[ $tpl_name ] = $this->template;