Part of the EllisLab Network
   
1 of 2
1
Patch for GET values
Posted: 10 May 2007 01:03 AM   [ Ignore ]  
Summer Student
Total Posts:  8
Joined  02-19-2007

This forum doesn’t allow zip attachments, so I’ve posted the patch here:

Input.php Patch

This patch is for the Input.php file in /system/libraries

To enable GET parameters, you’ll need to change your config to use PATH_INFO.  Look for the following line in /application/config/config.php:

$config['uri_protocol']    = "AUTO";

Change it to:

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

The patch for Input.php are pretty minor.  I’ve added a new member variable to the input class that tells you if you are involved in a postback or not.  Usage:

if ($this->input->is_postback)
{
   
...
}

Finally I changed the post() method of the Input class to check both the $_POST and the $_REQUEST.  It checks post first, then request.  If specified in the config, it’ll XSS clean them both.


I understand that the main developer feels strongly about using GET, but my angle is that GET should “modify” the resource so that your URL’s point to “resources” and your GET parameters “act” on those resources.

Anyways, I hope this is helpful to someone.  We’re using it in production on a public API we’re developing for our site.

Thanks,

Jon Gilkison
massify.com

Profile
 
 
Posted: 10 May 2007 01:42 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  124
Joined  04-05-2007

Errm please explain again why we need $_GET ?

Profile
 
 
Posted: 10 May 2007 03:40 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  13
Joined  03-10-2007
gazza - 10 May 2007 01:42 AM

Errm please explain again why we need $_GET ?

If I want to track affiliate on every single page, I will do:

http://www.example.com/dir/page1234?aff=xxxx

Do you have a better way to implement this without using $_GET?

Profile
 
 
Posted: 10 May 2007 07:49 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006

How about:

http://www.example.com/dir/page1234/aff/xxxx

Then you can use either of these:

$aff = $this->uri->segment(4);

$array = $this->uri->uri_to_assoc();
$aff = $array['aff'];

Check URI Class in the User Guide.

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
Posted: 10 May 2007 10:32 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  124
Joined  04-05-2007

Yeah

Do you have a better way to implement this without using $_GET?

linux pretty much said it all.

Profile
 
 
Posted: 10 May 2007 01:16 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  8
Joined  02-19-2007

I’m a bit of a formalist when it comes to RESTful URLs.

Let’s say you want to get a list of users but filter them by certain criteria.  You want that criteria bookmarkable by end users.  This requirement alone rules out using POST variables for your filtering, and using URI segments would be unmanageable. 

Let’s say your resource endpoint looks like:

http://www.massify.com/people/

By default this is a list of everyone on your site.  But you need to filter by four or five fields out of, let’s say, fifteen total fields that could be filtered on.  For this example, I want to see everyone that’s within 15 miles of the zipcode 11231 that have joined within the last 10 days.  How would you structure your URI so that was a) possible and b) semantic?  The answer is that you can’t.

Without GET, you’d have to come up with some insane URI scheme that would be hard to manage and that would ultimately end up defeating the purpose of RESTful URL’s.  Which url is more descriptive, eg. semantic?

http://www.massify.com/people/11231/5/10

http://www.massify.com/people?zip_code=11231&max_distance=5&joined_before=5-1-07

I suppose you could do something like:

http://www.massify.com/people/zip_code/11231/max_distance/5/joined_before/5-1-07

But that scheme doesn’t really work for CI because CI will now look for a method called zip_code and 404 because it can’t find it.

My personal belief is that URL’s should be:

http://whatever.com/<OBJECT>[/<ACTION>/<IDENTIFIER>]?<PARAMETERS>

For example:

http://whatever.com/people/detail/jawngee

To each their own, I think my patch opens up the best of both worlds.

Profile
 
 
Posted: 10 May 2007 01:20 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  8
Joined  02-19-2007
linuxbz - 10 May 2007 07:49 AM

How about:

http://www.example.com/dir/page1234/aff/xxxx

Then you can use either of these:

$aff = $this->uri->segment(4);

$array = $this->uri->uri_to_assoc();
$aff = $array['aff'];

Check URI Class in the User Guide.

This is a total trap:

What happens when you need to restructure your URI segments? 

Two side effects:  You have to change your code, and whatever bookmark or links no longer work because you are using indexing to fetch parameters instead of named keys as you would get with $_REQUEST.

Profile
 
 
Posted: 10 May 2007 01:26 PM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006

I would not use the first one for the reason you gave.

The second, using

$this->uri->uri_to_assoc();

will give you named keys with only the restriction that they should be in pairs.  You can even change the offset if you want some “hardwired” after controller/function.

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
Posted: 10 May 2007 08:03 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  13
Joined  03-10-2007
linuxbz - 10 May 2007 07:49 AM

$aff = $this->uri->segment(4);

My goal is to tracking EVERY single page. So your segment(4) is not general. GET method is still the best and easiest way to achieve my goal.

will give you named keys with only the restriction that they should be in pairs.

Your second solution is difficult to implement too because of this restriction.

Profile
 
 
Posted: 10 May 2007 09:33 PM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  36
Joined  03-17-2007

I’ve done similar things with $this->uri->uri_to_assoc(); for displaying sorted data. I got a quite large number of params, and managed to make it work nicely, by providing default values when not everything is given:

domain.com/products/search/term/aaaa/cat/57/ord/p/dir/a/from/12/to/34/loc/gbe&esp/

so that’s 7 params.

My only good reason for using GET was simple search forms without constructing the url with javascript. I ended up using post method, posting to a function that would take post data, construct the segmented url and redir to that url, so it’s bookmarkable and everything, which turns out to be almost a transparent process for the end user. smile

Profile
 
 
Posted: 20 June 2007 09:03 AM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  5
Joined  05-31-2007

But why should we overload the server with 2 requests just for one search (POST and GET) when we could just do a GET and act on the arguments ? Javascript doesn’t sound good either.
These are 2 hacks for a non existent problem. I understand that it’s nicer to use just PATH_INFO, but is GET itself that bad to be forbidden ?
I’ll just go with the patch, thank you jawngee.

Profile
 
 
Posted: 20 June 2007 02:08 PM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  197
Joined  04-18-2006

Normally I’d avoid using GET vars in CI apps, but sometimes it is unavoidable, such as implementing a facebook app with CI (facebook supplies the URI get vars, I can’t control that.)

To work around this, I simply “undo” the CI GET var clobbering:

parse_str($_SERVER['QUERY_STRING'],$_GET);

Now $_GET has the GET vars again.

 Signature 

http://www.motortopia.com/
http://www.phpinsider.com/

Profile
 
 
Posted: 11 July 2007 05:37 AM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  29
Joined  06-13-2007

Thanks for the patch! I’ve just started using it. Have you experienced any problems with it? Seems to work fine for me…. but just wanted to make sure again..

 Signature 

http://www.straightlineprofits.com
Living Online

Profile
 
 
Posted: 14 July 2007 03:14 PM   [ Ignore ]   [ # 13 ]  
Grad Student
Rank
Total Posts:  42
Joined  07-14-2007

Why patch Input.php when you can extend it to use your own logic?

All I do to enable $_GET is to put this file (MY_Input.php) in my application/libraries folder:

class MY_Input extends CI_Input {

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

I think thats a much more elegant solution as no patching is required!

You still need the following in your config file:

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

Profile
 
 
Posted: 14 July 2007 10:05 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  29
Joined  06-13-2007

Sounds good Al!! I’ll try it right away!

 Signature 

http://www.straightlineprofits.com
Living Online

Profile
 
 
Posted: 15 July 2007 04:03 AM   [ Ignore ]   [ # 15 ]  
Grad Student
Rank
Total Posts:  42
Joined  07-14-2007

Oh, one more thing. Make sure your subclass_prefix is ‘MY_’ (in your config file):

$config['subclass_prefix'] = 'MY_';

Otherwise you will need to rename the MY_Input class and file to <WHATEVER>_Input.

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64453 Total Logged-in Users: 25
Total Topics: 80958 Total Anonymous Users: 0
Total Replies: 435680 Total Guests: 181
Total Posts: 516638    
Members ( View Memberlist )