CodeIgniter PHP Framework - Need to get query string

前端 未结 12 1434
挽巷
挽巷 2020-11-27 04:07

I\'m creating an e-commerce site using CodeIgniter.

How should I get the query string?

I am using a Saferpay payment gateway. The gateway response will be li

相关标签:
12条回答
  • 2020-11-27 04:42

    I have been using CodeIgniter for over a year now. For the most part I really like it (I contribute to the forum and use it in every instance that I can) but I HATE the ARROGANCE of that statement in the manual:

    Destroys the global GET array. Since CodeIgniter does not utilize GET strings, there is no reason to allow it.

    The presumption that you will never need GET in a CodeIgniter application is asinine! Already in just a few days, I've had to deal with post back pages from PayPal and ClickBank (I'm sure there are a million others.) Guess what, they use GET!!!

    There are ways to stop this GET squashing, but they are things that tend to screw other things up. What you don't want to hear is that you have to recode all your views because you enabled querystrings and now your links are broken! Read the manual carefully on that option!

    One that I like (but didn't work because setting REQUEST_URI in config.php broke my site) is extending the Input class:

    class MY_Input extends CI_Input
    {
            function _sanitize_globals()
            {
                $this->allow_get_array = TRUE;
                parent::_sanitize_globals();
            }
    }
    

    But the best no-nonsense way is to test with print_r($_SERVER) at the URL where you need the GET variables. See which URI Protocol option shows your GET variables and use it.

    In my case, I can see what I need in REQUEST_URI

    // defeat stupid CI GET squashing!
    parse_str($_SERVER['REQUEST_URI'], $_GET);
    

    This places your query string back into the $_GET super global for that page instance (You don't have to use $_GET, it can be any variable.)

    EDIT

    Since posting this I found that when using REQUEST_URI, you will lose your first query string array key unless you remove everything before the ?. For example, a URL like /controller/method?one=1&two=2 will populate the $_GET array in this example with array('method?one'=>1,'two'=>2). To get around this, I used the following code:

    parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
    

    I suppose I should have provided an example, so here goes:

    class Pgate extends Controller {
       function postback() {
          parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
          $receipt = $this->input->xss_clean($_GET['receipt']);
       }
    }
    
    0 讨论(0)
  • 2020-11-27 04:43

    If you're using mod_rewrite to remove the index.php file, you can use the following code to obtain the GET variables (via $this->input->get()). Assuming the default configuration, name the file MY_Input.php and place it in your application/libraries directory.

    Usage: $this->input->get()

    class MY_Input extends CI_Input {
    
        function My_Input()
        {
            parent::CI_Input();
    
            // allow GET variables if using mod_rewrite to remove index.php
            $CFG =& load_class('Config');
            if ($CFG->item('index_page') === "" && $this->allow_get_array === FALSE)
            {
                $_GET = $this->_get_array();
            }
    
        }
    
        /**
         * Fetch an item from the GET array
         * 
         * @param string $index
         * @param bool   $xss_clean
         */
        function get($index = FALSE, $xss_clean = FALSE)
        {
            // get value for supplied key
            if ($index != FALSE)
            {
                if (array_key_exists(strval($index), $_GET))
                {
                    // apply xss filtering to value
                    return ($xss_clean == TRUE) ? $this->xss_clean($_GET[$index]) : $_GET[$index];
                }
            }
            return FALSE;
        }
    
        /**
         * Helper function
         * Returns GET array by parsing REQUEST_URI
         * 
         * @return array
         */
        function _get_array()
        {           
            // retrieve request uri
            $request_uri = $this->server('REQUEST_URI');
    
            // find query string separator (?)
            $separator = strpos($request_uri, '?');
            if ($separator === FALSE)
            {
                return FALSE;
            }
    
            // extract query string from request uri
            $query_string = substr($request_uri, $separator + 1);
    
            // parse query string and store variables in array
            $get = array();
            parse_str($query_string, $get);
    
            // apply xss filtering according to config setting
            if ($this->use_xss_clean === TRUE)
            {
                $get = $this->xss_clean($get);
            }
    
            // return GET array, FALSE if empty
            return (!empty($get)) ? $get : FALSE;
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 04:46

    Here's a full working example of how to allow querystrings in Codeignitor, like on JROX platform. Simply add this to your config.php file located at:

    /system/application/config/config.php 
    

    And then you can simply get the querystrings like normal using $_GET or the class below

    $yo = $this->input->get('some_querystring', TRUE);
    $yo = $_GET['some_querystring'];
    

    Here's the code to make it all work:

    /*
    |--------------------------------------------------------------------------
    | Enable Full Query Strings (allow querstrings) USE ALL CODES BELOW
    |--------------------------------------------------------------------------*/
    
    /*
    |----------------------------------------------------------------------
    | URI PROTOCOL
    |----------------------------------------------------------------------
    |
    | This item determines which server global should 
    | be used to retrieve the URI string.  The default 
    | setting of 'AUTO' works for most servers.
    | If your links do not seem to work, try one of 
    | the other delicious flavors:
    |
    | 'AUTO'              Default - auto detects
    | 'PATH_INFO'         Uses the PATH_INFO
    | 'QUERY_STRING'      Uses the QUERY_STRING
    | 'REQUEST_URI'   Uses the REQUEST_URI
    | 'ORIG_PATH_INFO'    Uses the ORIG_PATH_INFO
    |
    */
    if (empty($_SERVER['PATH_INFO'])) {
        $pathInfo = $_SERVER['REQUEST_URI'];
        $index = strpos($pathInfo, '?');
        if ($index !== false) {
            $pathInfo = substr($pathInfo, 0, $index);
        }
        $_SERVER['PATH_INFO'] = $pathInfo;
    }
    
    $config['uri_protocol'] = 'PATH_INFO'; // allow all characters 
    
    $config['permitted_uri_chars'] = ''; // allow all characters 
    
    $config['enable_query_strings'] = TRUE; // allow all characters 
    
    parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
    

    Enjoy :-)

    0 讨论(0)
  • 2020-11-27 04:48

    Open up application/config/config.php and set the following values:

    $config['uri_protocol'] = "PATH_INFO";
    
    $config['enable_query_strings'] = TRUE; 
    

    Now query strings should work fine.

    0 讨论(0)
  • 2020-11-27 04:54

    Thanks to all other posters. This is what hit the spot for me:

        $qs = $_SERVER['QUERY_STRING'];
        $ru = $_SERVER['REQUEST_URI'];
        $pp = substr($ru, strlen($qs)+1);
        parse_str($pp, $_GET);
    
        echo "<pre>";
        print_r($_GET);
        echo "</pre>";
    

    Meaning, I could now do:

    $token = $_GET['token'];
    

    In the .htaccess i had to change:

    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    to:

    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    0 讨论(0)
  • 2020-11-27 04:57

    Set your config file

    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    $config['allow_get_array']      = TRUE;
    $config['enable_query_strings'] = FALSE;
    

    and .htaccess file (root folder)

    <IfModule mod_rewrite.c>
        Options +FollowSymLinks
        Options -Indexes
        RewriteEngine On
        RewriteBase /
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
    
        RewriteCond $1 !^(index\.php)
        RewriteRule ^(.*)$ index.php [L]
    
    
    </IfModule>
    

    Now you can use

    http://example.com/controller/method/param1/param2/?par1=1&par2=2&par3=x
    http://example.com/controller/test/hi/demo/?par1=1&par2=2&par3=X
    

    server side:

    public function test($param1,$param2)
    {
        var_dump($param1); // hi
        var_dump($param2); // demo
        var_dump($this->input->get('par1')); // 1
        var_dump($this->input->get('par2')); // 2
        var_dump($this->input->get('par3')); // X
    }
    
    0 讨论(0)
提交回复
热议问题