<?php
// Force the user into https mode, can be either included or linked to. When linking you need a referer!

// If this is the running script and thus not included, check for referer and try to make referer https.
if($_SERVER['SCRIPT_FILENAME'] ===  __FILE__ && isset($_SERVER['HTTP_REFERER']))
  
$secureurl str_replace('http://','https://',$_SERVER['HTTP_REFERER']);

// At this point the script is either included, of doesn't have a referer. Try to go https either way.
elseif(!isset($_SERVER["HTTPS"])) $secureurl "https://" $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 

// finally redirect back to the secure version of this page.
if(isset($secureurl)) { header("location: $secureurl"); exit(); }
?>