PHP Snippet – Script Runtime

REQUEST_TIME_FLOAT is a timestamp of the start of the request, with microsecond precision (PHP 5.4.0+).
microtime(true) returns a float of the current Unix timestamp (microseconds precision).

<?php
$time_start = call_user_func(function(){
                $result = filter_input(INPUT_SERVER, 'REQUEST_TIME_FLOAT', FILTER_SANITIZE_NUMBER_FLOAT);
                return false === $result || null === $result ? microtime(true) : $result;
              });

echo microtime(true) - $time_start
?>

– This is a “dictionary code”, in-order to save the extra anonymous-function call and checking if REQUEST_TIME_FLOAT is available, just use microtime all-along :)

Best practice:
Always use filter_input with some sort of filtering, try to avoid accessing the global variables directly ($_SERVER),
It will also make most of the code-validation engines happy ;)

SEO – Set Your Server’s Timezone Using The .htaccess-File

#####################################################
# set timezone - "UTC"|"GMT"|"Asia/Jerusalem"|etc.. #
#####################################################
<IfModule mod_env.c>
  SetEnv TZ                "Asia/Jerusalem"
</IfModule>

<IfModule php5_module>
  php_value date.timezone  "Asia/Jerusalem"
</IfModule>
#####################################################

Apache .htaccess Entry For Serving Correct MimeType For Some Of The More Common File-Types

Put this part in your .htaccess to make sure your files are served with correct mimetype, and will eventually trigger the target-browser/OS, proper, handler.

<IfModule mod_mime.c> 
  AddType application/font-woff                  woff
  AddType application/javascript                 js
  AddType application/octet-stream               safariextz
  AddType application/vnd.ms-fontobject          eot
  AddType application/x-chrome-extension         crx
  AddType application/x-font-ttf                 ttf ttc
  AddType application/x-font-woff                woff
  AddType application/x-mpegurl                  m3u8
  AddType application/x-xpinstall                xpi
  AddType audio/mp4                              m4a
  AddType audio/mpeg                             mp3
  AddType audio/ogg                              oga ogg
  AddType font/opentype                          otf
  AddType image/webp                             webp
  AddType image/x-icon                           ico
  AddType text/cache-manifest                    appcache manifest
  AddType text/plain                             srt sub ssa
  AddType text/vtt                               vtt
  AddType text/x-component                       htc
  AddType text/x-vcard                           vcf
  AddType video/3gpp                             3gp
  AddType video/mp2t                             ts
  AddType video/mp4                              mp4 m4v
  AddType video/ogg                              ogv ogg
  AddType video/quicktime                        mov
  AddType video/x-ms-wmv                         wmv
  AddType video/webm                             webm
  AddType video/x-flv                            flv
  AddType video/x-msvideo                        avi

  # Web application-manifests.  [https://github.com/mdn/to-do-notifications/blob/gh-pages/.htaccess]
  AddType application/x-web-app-manifest+json    webapp

  # SVG.
  #   Required for svg webfonts on iPad [twitter.com/FontSquirrel/status/14855840545]
  AddType image/svg+xml                          svg svgz 
  AddEncoding gzip                               svgz
</IfModule>

Content-Disposition Header – Like A Boss 👔☕

##place inside a folder that contains some music videos and documents, this will override the default handler.

<FilesMatch "\.(mov|mp3|pdf)$">

  <IfModule mod_mime.c> 
    ForceType application/octet-stream
  </IfModule>

  <IfModule mod_headers.c> 
    ##fix a bug in old GoDaddy servers.
    Header unset X-Content-Type-Options
    Header unset Content-Disposition

    ##prevent mimetype sniffing (first few bytes can determine that a file should be opened in browser).
    Header set X-Content-Type-Options "nosniff"

    ##extract filename, apply to proper (for example) Content-Disposition: attachment; file="my music.mp3"
    SetEnvIf Request_URI "^.*/([^/]*)$" FILENAME=$1
    Header set "Content-Disposition" "attachment;filename=\"%{FILENAME}e\""
    UnsetEnv FILENAME
  </IfModule>

</FilesMatch>

forcing type / because of ‘reasons’? 👐
If this header is used in a response with the application/octet- stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as…’ dialog.


also edit your HTML so links would use HTML5’s download attribute:

<a href="mydirectory/my%20music.mp3" download="my music.mp3" type="application/octet-stream" title="click to download">hello!!</a>

(which is good enough for Google).

✨And, naturally.. 👀 you can combine both of the solutions!

reference: W3 / Protocols – RFC2616 (Section 19).

also answered on stackoverflow

No Robots!

(A Not So Active) Anti Google|* Bot headers

  1. robots.txt

    User-agent: *
    
    Disallow : /
  2. .htaccess header (useful for non-html files..)

    <IfModule mod_headers.c>
      Header set X-Robots-Tag "noindex, nofollow, nosnippet, noarchive, noodp, notranslate, noimageindex, unavailable_after: 25 Jun 2010 15:00:00 PST"
    </IfModule>

PHP snippet – string of X latest modified files in a JS supported format

I am using this one, along with selenium, and an automated screen-capture programs I’ve made in C#,
to write screenshots of tests to Apache hdocs folder, so it will be available as a page resource.
my program does not overrides the image name, it keeps couple of screenshots in a folder and the php snippet attached here, selects the 3 (can be more) of the last modified files,
puts a nice string in a format suitable for the later usage in page’s JS code,
for example, to be used in a roller or something..