In my projects i often use get params as a temporary way to test things with different values.
However it seems like you cannot access get params in code igniter?
I am aware I could build a param into my functions and pass the value as a url segment.
But i dont want to be doing that every time I wish to test something.
So,
is there any way to use get values in CI?
You can parse $_SERVER['QUERY_STRING'] and set it as $_GET:
parse_str($_SERVER['QUERY_STRING'], $_GET);
You can enable query strings in your config file. Find this.
$config['enable_query_strings'] = FALSE;
and change it to true.
Related
I'm kind of a noob at this stuff.
But I've been browsing around and I see sites that are kind alike this
www.store.com/product.php?id=123
this is really cool. but How do I do it?
Im stuck using something like this
www.store.com/product/product123.php
If you could tell me how I can go about do this it would be awesome!
What you're looking at is a $_GET argument.
In your PHP code, try writing something like this:
$value = $_GET['foo'];
Then open your page like this:
hello.php?foo=123
This will set $value to 123.
You need to use the $_GET here.
if you use the following:
?id=123
then this will be how to use it and the result
$_GET['id'] (returns the 123)
You can use as many $_GET arguments as you need, for example:
?id=123&foo=bar&type=product
$_GET is an array of what parameters are in the url, so you use it the same way as an array.
Create a file called product.php with this code:
<?php
echo "The argument you passed was: " . $_GET['id'];
?>
Now run this URL in your browser:
http://<yourdomain>/product.php?id=123
and you will understand how $_GET works.
Those are called URL parameters (what they're contained in is called a query string), and they're not unique to PHP but can be accessed in PHP using the $_GET superglobal.
Similarly, you can get POST parameters using the $_POST superglobal, though in POST requests, these parameters are not appended to the URL.
Note: Generally, for usability purposes (and thus also SEO purposes), you want to avoid using query strings as much as possible. These days, the standard practice is to use URL rewriting to display friendly URLs to the user. So your application might accept a URL like:
/products.php?id=32
But the user only sees:
/product/32
You can do this by using mod_rewrite or similar URL rewriting capabilities to turn the friendly URL into the former query string URL internally, without having the user type out the query string.
You might want to have a look at the documentation at www.php.net, especially these pages: http://www.php.net/manual/en/reserved.variables.php
Specifically, have a look at $_GET and $_POST, which are two frequently used ways to transmit information from a browser to the server. (In short, GET-parameters are specified in the URL, as in your question, while POST-parameters are "hidden from view", but can contain more data - typically the contents of forms etc, such as the textbox you posted your question in).
A 3rd party service that I'm using returns the users to a url like this:
site.com/something.php?id=XXX&something=abc....
Therefore, I need to be able to accept $_GET parameters for only one part of the site.
Is there a way to put a file outside of codeigniter's application directory which will do something like this:
<?
$id = $_GET['id'];
$something = $_GET['something'];
//Do something so codeigniter thinks this is a request to site.com/process/$id/$something
require('index.php'); //codeigniter's index.php file
?>
I remember using putenv() to achieve this in the past, but don't remember the details.
Not sure about putenv, but if worse comes to worse you can get the contents of the get array by exploding $_SERVER['QUERY_STRING']
get doesn't need to be enabled and you still have access.
Try just using a redirect:
<?php
$id = $_GET['id'];
$something = $_GET['something'];
header('Location: http://www.site.com/' . $id . '/' . $something);
As long as the initial something.php request is made directly to that file and not index.php, then CodeIgniter won't run (actually, that would depend on your .htaccess [or equivalent] file, so you may need to tweak it).
That's the safest way I can think to do it, and you won't have to break up CodeIgniter's program flow or enable query strings in your application, which may be unsafe.
Maybe I'm misunderstanding your architecture but couldn't you just enable GET for codeigniter..?
See $config['allow_get_array'] in the docs:
http://codeigniter.com/user_guide/libraries/input.html
And see $this->input->get() on the same page..
try
$this->config->set_item("allow_get_array",TRUE);
in your controller's constructor before parent::__construct();.
The security filtering function is called automatically when a new
controller is invoked. It does the following:
If $config['allow_get_array'] is FALSE(default is TRUE), destroys the
global GET array.
I have successfully created clean url for my project.. now what i need to do is add variables to URL..
localhost/user1/file?action=add
localhost/user2/file2?action=delete
where user and file are already rewritten i dont want it to be like localhost/user/file/add because localhost/user/folder/file will be mistaken to to the action parameter.. please help
Try using the ampersand instead of question mark:
localhost/user2/file2&action=delete
In your htaccess, the rewrite rule might look something like this:
RewriteRule ^user([0-9]+)/file([0-9]+)$ /page\.php?user=$1&file=$2
As you can see, the question mark is already there even though it is masked in the address bar. Appending another variable to the query string would require the ampersand for successful concatenation.
You need to get the url and start parsing the url from the question mark. I would save the contents then to an array, so that you've got a key and a value.
$uri = $_SERVER["REQUEST_URI"];
$questionMark = explode('?',$uri);
$questionMark[1] is the action=delete then. There are probably better ways then using explode() method here, but I just wanted to show how you get the string.
You can read GET variables in PHP by accessing the global $_GET array:
http://php.net/manual/en/reserved.variables.get.php
In your example, the php file that is used for handling files would be able to read in:
echo $_GET['action']; // 'add' or 'delete'
I got this URL:
http://twitternieuws.com/class/function/ID?oauth_token=xxxxx&oauth_verifier=xxxxx
And I keep getting errors like "The page you requested was not found" or "The URI you submitted has disallowed characters". I tried changing the following options with different settings:
$config['permitted_uri_chars'];
$config['enable_query_strings'];
$config['uri_protocol'];
Is there anything I can do to make it work? I am using codeigniter 1.7.2
Query strings in 1.7.2 are a joke, it uses ?c=controller&m=method to basically change your pretty urls to psuedo $_GET params. I really can't see why anyone would use it the way it's intended, it's very misleading and is not the same as normal query strings.
I highly suggest you check out the latest version of Codeigniter, where they do not unset the $_GET array (normal query strings are now usable). In one of the core files in the older versions it says CI does not use $_GET so we are going to unset() the global $_GET array. Well, what if I need to use $GET? I always thought it was crazy, and people have been screaming for true $_GET support for forever.
Seriously though, it's time to upgrade:
Latest: https://bitbucket.org/ellislab/codeigniter-reactor/
Stable: http://codeigniter.com/
When upgrading to CodeIgniter 2 is not an option, you can recreate the $_GET variable like so (add this to every controller where you need the query string):
parse_str($_SERVER['QUERY_STRING'],$_GET);
And change this in your config.php file:
// $config['uri_protocol'] = "AUTO"; // change from this
$config['uri_protocol'] = "PATH_INFO"; // to this
I'm trying to make a bookmarklet that will take the URL of the current page you are on, and send it to an application written using CodeIgniter.
The problem I keep running into is that I can't do a standard AJAX call, because it's cross-domain. It is disallowed, and I can't figure out a way to use the JSONP via $_GET method since CodeIgniter blows away the $_GET parameter.
At this point I'll take any suggestions on how to do this. Please note that I need to send a URL, and if it's to be passed via a URL itself it obviously needs to be encoded or something. This I also haven't figured out how to do, so any pointers on that end would be appreciated as well.
Codeigniter unsets $_GET but you can get the data from the query string. It is a little inefficient because PHP will probably end parsing the query string twice, but it should work:
parse_str($_SERVER['QUERY_STRING'], $get);
print_r($get);
All the GET variables should be accesible in the variable $get. See parse_str() documentation for some more information.
As an alternative you could url-encode the current URL and append it to what you are requesting e.g.
var url = 'http://example.com/bookmarklet/'
+ encodeURIComponent(window.location);
Then in Codeigniter do something like:
//you might have to call urldecode() on this value
$url = $this->uri->segment(0);
but you may find you then have this problem
It is possible to enable query strings in Codeigniter, but watch out for the caveats - you can't use the URL helper, for example.