It's only set for named vhosts specifically. If you access localhost as default server using HTTP/1.0 sans Host: header, then it's absent. Obviously also for cron/cmdline invocation
是不是加个 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') 判断比较好?