Welcome Guest, Not a member yet? Register   Sign In
Solution for Forced URL Suffix, and enabled query strings without disrupting the Segment URL's

After my last post, where I wanted to force the URL Suffix, I made two extensions: one for the URI Class and another for the Input and Security Class. These two combined allow for the forcing of the URL Suffix, and allow query strings to be present without them affecting the way CodeIgniter compiles the URL from segments.

Thanks to Hermawan Haryanto, for replying to my post (in less than 10 minutes) and leading me in the right direction!

I consider myself quite advanced in PHP, but this is only my second post on this forum, and this work/code/development attempt is the first piece of work I have ever submitted anywhere, not just CodeIgniter. (Hardly anyone even knows what XML is let alone PHP in my lovely town of Tewkesbury, famous for its Abbey, Annual Medieval Fayre and the GREAT FLOOD 2007!)
I would greatly appreciate ANY and ALL criticism to my work, whats wrong with it, what could be improved, performance and security-wise. Thankyou!

This can be downloaded as a zip file, containing both files, and two HTML Files for documentation (I failed GCSE English to don't expect for it to be perfect Tongue). Please rename the file as '.zip' as I'm not allowed to upload anything but images... naughty me Tongue

EDIT: Funnily enough I do believe the zip file thing doesn't work... follow this link instead: http://zafr.googlecode.com/files/codeign...suffix.zip

<?php if(!defined('BASEPATH')) {header('HTTP/1.1 404 Not Found'); exit;}
class MY_URI extends CI_URI

   // This sets $_GET to a string so CI_URI doesn't try to create the URL from it.
   // Serialized so we can retrieve it back in MY_Input.
   function MY_URI() {
    if(is_array($_GET) && (@count($_GET) == 1)) $_GET = serialize($_GET);

   // Forces the URL suffix to be present if " @FORCE" is added to the end of the config setting.
   function _remove_url_suffix() {
    $suffix = $this->config->item('url_suffix');
    $permitted = $this->config->item('permitted_uri_chars');
    if(eregi("^([\/".$this->config->item('permitted_uri_chars')."]+) [email protected]$", $suffix, $regs)) {
     $esc = preg_quote(trim($regs[1]));
     if(eregi($esc."$", $this->uri_string)) {
      $this->uri_string = eregi_replace($esc."$", "", $this->uri_string);
      $this->config->set_item('url_suffix', $regs[1]);}
     else {
    elseif($suffix != "") {
     $this->uri_string = preg_replace("|".preg_quote($suffix)."$|", "", $this->uri_string);}}


<?php if(!defined('BASEPATH')) {header('HTTP/1.1 404 Not Found'); exit;}
class MY_Input extends CI_Input
   private $MY_getGlobalArray = false;

   function MY_Input() {
    if(is_array($_GET) && (@count($_GET) > 0))
     $this->MY_getGlobalArray = $_GET;
     if(is_array(($usg = @unserialize($_GET))))
      $this->MY_getGlobalArray = $usg;

   // Returns the value of $index in $_GET, false if it isn't set or $_GET isn't set.
   // Because $_GET is a major security threat, XSS Filtering is NOT an option.
   function get($index = '') {
     return false;
    if(is_array($this->MY_getGlobalArray[$index])) {
     foreach($this->MY_getGlobalArray[$index] as $key => $val)
      $this->MY_getGlobalArray[$index][$key] = $this->xss_clean($val);}
     return $this->xss_clean($this->MY_getGlobalArray[$index]);
    return $this->MY_getGlobalArray[$index];}

   // Returns the amount of entries in the $_GET array.
   function get_count() {
    return (!is_array($this->MY_getGlobalArray)) ? 0 : count($this->MY_getGlobalArray);}

   // Returns true or false whether $_GET contains the keys passed as values in $array.
   // Strict means whether $_GET contains and ONLY contains the keys passed as values in $array.
   function get_contains($array, $strict = false) {
    if((!is_array($array)) || (!is_bool($strict)))
     show_error('Wrong data type passed to Input::Get_contains()<br />If a second parameter is passed it must be a boolean value');
    if(!is_array($this->MY_getGlobalArray) || (@count($this->MY_getGlobalArray) == 0))
     return false;
    if($strict && (count($array) != count($this->MY_getGlobalArray)))
     return false;
    foreach($array as $key) {
      return false;}
    return true;}


After this I have a new found respect for the CodeIgniter Dev Team, not because of their Framework, but for their seemingly perfect ability to write brilliant documentation! I never expected it to be so hard! (Obviously these aren't that hard but I'm currently working on a Template and Document Assets Class, and the documentation is the thing that makes me want to smash my computer in!)

Hello and thanks for your contribution.

It looks very cool but I'm curious, what is the aim of gorcing the URL suffix ?

Is it in order to seo ?

Yes SEO is a very good use for it, but no, initially I did it just because being able to access it without the suffix annoyed me, no other reason Tongue

Ok thnak's !


[eluser]James V[/eluser]
For users interested in forcing a suffix, make a small change in the .htaccess file.

Example, to get www.mysite.com/blog.html I changed the following .htaccess line
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^(.*)\.html$ index.php?/$1 [L]

Theme © iAndrew 2016 - Forum software by © MyBB