Perfectly fine to 301 redirect all HTTP pages to their HTTPS equivalent. Either via httpd.conf on Apache servers, .htaccess rewrites or with a PHP include like your example. Any inbound links would pass about 97% of weight through a 301 redirect.
To follow on from bybe's comment above as well, theThe best handling internally would be to not use the protocol for internal linkage so that compatibility is maximised sitewide with minimal intervention as he rightly states.
Https as a ranking signal is minimal, it is also not even categorically confirmed that there are site ranking benefits to be had by serving all URL's over https (this algorithm change affects on a per URL basis). That said however, John Mueller at Google recently confirmed that if you already have TLS on the domain, switching everything over to https ought to be a no-brainer (so more for the benefit of visitor trust than rankability).
This isn't straight forward of course for every site depending on the amount of embedded content it may have that is served on http within HTTPS pages (e.g. put an iframe, script, image, etc. that uses HTTP on a page that uses HTTPS). In any instances such as this, visitors could be seeing the red cross over the padlock or red/yellow browser warnings constantly on a site without each of these instances being taken care of and handled appropriately.