Get Current Website or Webpage URL

Three useful PHP functions for locating yourself on your site. They can come in useful for when you want to create links in code for external use. The nice part about these is that if you are on SSL, they will generate an SSL link.

Get Current Website URL

function getCurrentWebsiteURL() {
    $url = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
    } else {
        $url .= $_SERVER["SERVER_NAME"];
    }
    return $url;
}

The above will generate the following string.

http://mywebsite.com

Get Current File URL

function getCurrentFileURL() {
    $url = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER['PHP_SELF'];
    } else {
        $url .= $_SERVER["SERVER_NAME"].$_SERVER['PHP_SELF'];
    }
    return $url;
}

The above will generate the following string.

http://mywebsite.com/test.php

Get Current URL (With GET Requests)

function getCurrentURL() {
    $url = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $url;
}

The above will generate the following string.

http://mywebsite.com/test.php?message=hello

How to Check if ‘mod_rewrite’ is On Using PHP

I needed to rewrite my menu’s based on whether mod_rewrite was turned on in the htaccess file and not necessarily if the module existed. This is how I did it in two parts.

Part 1- In the .htaccess file

#Turn on rewrite 
RewriteEngine On

#Notify PHP that rewrite is on
SetEnv HTTP_MOD_REWRITE on

#Continue with your rewrite rules below

Part 2- In the PHP file

function modRewriteIsEnabled() {
    if (strtolower($_SERVER['HTTP_MOD_REWRITE']) == 'on') {
      return true;
    } else {
      return false;
    }
}

How to Keep a JavaScript or CSS File From Caching

Depending on your browser and server settings sometimes JavaScript and CSS files are cached while you are trying to modify a webpage. Here is a quick copy/paste way to assure this happens if you are using PHP or ASP (Classic ASP but I think it should work in .NET as well). Please note the only reason you would want to do this is to assure every time you hit the page you are trying to modify, a fresh copy of the CSS or JavaScript file is downloaded. This should be removed when your site hits production.

Please excuse my wordpress code plugin. It doesn’t seem to be converting less and greater than characters correctly.

PHP

ASP

Set Expires Header

I’ve noticed setting the Expire time for files on the HTACCESS file tends to fail when loading a file using the readfile() function in PHP. A workaround is setting the Expire header before loading the file. This is what that would look like.

header('Expires: '.gmdate('D, d M Y H:i:s', time() + 60*15); // seconds, minutes
header("Content-Type:image/jpeg");
readfile($jpeg_image);

Now when you check the expiration headers via your browser’s Developer Tools you will see this particular file expires one month from now.