Skip to content

Instantly share code, notes, and snippets.

@joshkoenig
Created April 10, 2015 19:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshkoenig/cff5ec2120eefec9f61b to your computer and use it in GitHub Desktop.
Save joshkoenig/cff5ec2120eefec9f61b to your computer and use it in GitHub Desktop.
SERVER_NAME and SERVER_PORT woes
<?php
/**
* Problem: there's code out there that relies on $_SERVER['SERVER_NAME'] and sometimes $_SERVER['SERVER_PORT']
* to construct urls, either to "call itself" or to create urls that are passed to third parties and expect to
* be routed back.
*
* This doesn't work well on Pantheon because the environmental data will be for ephemeral container data.
*
* In general, you don't want your code to rely on this, but if you are using some piece of contrib you may
* not have a choice. In that case, you will need to do something in your `settings.php` or `wp-config.php`
* to insure that the right values are present.
*
* Some possible solutions below.
*/
/**
* 1) Use HTTP_HOST instead of SERVER_NAME
*
* Note that $_ENV will also be around for command-line uses. $_SERVER is only set up when handling a web
* initiated request.
*/
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
/**
* 2) Setting the SERVER_PORT correctly
*/
if (isset($_SERVER['PANTHEON_ENVIRONMENT'])) {
if (isset($_SERVER['HTTP_X_SSL']) && $_SERVER['HTTP_X_SSL'] != 'ON') {
$_SERVER['SERVER_PORT'] = 443;
}
else {
$_SERVER['SERVER_PORT'] = 80;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment