HTTP Location Headers

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!

HTTP Location Headers

Postby cmb » Thu Jun 07, 2012 11:12 pm

Hello Developers,

often there's the need to do a redirect through a HTTP Location header (particularly when using the PRG pattern). But even if it works on nearly every browser with relative URLs, the internet standard (RFC 1945) clearly states, that a fully qualified absolute URL has to be given. So I suggest to add a constant to the core, say CMSIMPLE_URL, perhaps as follows:
Code: Select all

define
('CMSIMPLE_URL', 'http://'.(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 's' : '')
    .$_SERVER['SERVER_NAME'].preg_replace('/index.php$/', '', $_SERVER['PHP_SELF']));
 

There might be problems with the correct recognition of HTTP vs. HTTPS in case of proxies, but that's probably no real problem, as CMSimple sites seldom are accessed through HTTPS.

With this constant, redirection would be really simple, e.g.
Code: Select all
header('Location: '.CMSIMPLE_URL.'?'.$su); 


Plugins could use this constant in a downward compatible way:
Code: Select all

if 
(!defined('CMSIMPLE_URL')) {
    define('CMSIMPLE_URL', ...);
}
 


What do you think about it?

Christoph
Christoph M. Becker---Plugins for CMSimple_XH
cmb
 
Posts: 5484
Joined: Tue Jun 21, 2011 11:04 am
Location: Germany

Re: HTTP Location Headers

Postby cmb » Mon Jul 16, 2012 10:22 am

As bca pointed out, this definition won't work for non default port numbers. And I've found another bug related to HTTPS. So the definition should be:
Code: Select all
define('CMSIMPLE_URL', 'http'
    . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 's' : '')
    . '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']
    . preg_replace('/index.php$/', '', $_SERVER['PHP_SELF'])); 
Christoph M. Becker---Plugins for CMSimple_XH
cmb
 
Posts: 5484
Joined: Tue Jun 21, 2011 11:04 am
Location: Germany

Re: HTTP Location Headers

Postby cmb » Mon Dec 10, 2012 8:03 pm

The W3C validator complains about the explicit port number specified for ports < 1024. So I suggest the following change:
Code: Select all
define('CMSIMPLE_URL', 'http'
   . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 's' : '')
   . '://' . $_SERVER['SERVER_NAME']
   . ($_SERVER['SERVER_PORT'] < 1024 ? '' : ':' . $_SERVER['SERVER_PORT'])
   . preg_replace('/index.php$/', '', $_SERVER['PHP_SELF'])); 


We should keep an eye on the outcome of Rob's login problems, and adjust $_SERVER['PHP_SELF'] appropriately.
Christoph M. Becker---Plugins for CMSimple_XH
cmb
 
Posts: 5484
Joined: Tue Jun 21, 2011 11:04 am
Location: Germany

Re: HTTP Location Headers

Postby cmb » Tue Dec 11, 2012 5:46 pm

cmb wrote:We should keep an eye on the outcome of Rob's login problems, and adjust $_SERVER['PHP_SELF'] appropriately.

Even for Rob's "internal" domain redirect, the definition of CMSIMPLE_URL would work[1]. But I suggest to replace PHP_SELF with SCRIPT_NAME to avoid the problems disclosed by Holger in http://cmsimpleforum.com/viewtopic.php?f=6&t=5444&st=0&sk=t&sd=a&start=30#p32172.

So I suggest:
Code: Select all
define('CMSIMPLE_URL', 'http'
   . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 's' : '')
   . '://' . $_SERVER['SERVER_NAME']
   . ($_SERVER['SERVER_PORT'] < 1024 ? '' : ':' . $_SERVER['SERVER_PORT'])
   . preg_replace('/index.php$/', '', $_SERVER['SCRIPT_NAME'])); 


[1] However, not for Ajax requests.
Last edited by cmb on Tue Dec 11, 2012 9:43 pm, edited 1 time in total.
Reason: added footnote
Christoph M. Becker---Plugins for CMSimple_XH
cmb
 
Posts: 5484
Joined: Tue Jun 21, 2011 11:04 am
Location: Germany


Return to Open Development

Who is online

Users browsing this forum: No registered users and 3 guests

cron