passing a large string through url in codeigniter - codeigniter

how do i pass a large string as a variable in codeigniter? i am trying show the user an article, if the article has more than 800 characters and less than 3044 characters i am showing it in a jquery pop up window, and if the article is more than 3044 charcters i want to pass the article body and title through the url to a controller function.
here is what i have tried:
<?php
if(strlen($home_content[1]['content'])>800 && strlen($home_content[1]['content'])<3044)
{
$substr=substr($home_content[1]['content'],0,786);
echo $substr.'<p id="button"><i>read more...</i></p>';
}
else if(strlen($home_content[1]['content'])<800)
{
echo $home_content[1]['content'];
}
else
{
$substr=substr($home_content[1]['content'],0,786);
echo $substr.'<br/>';
echo anchor('site/read_article/'.$home_content[1]['title'].$home_content[1]['content'],'<i>read more...</i>');
}
?>
and this is the url after passing the data:
http://192.168.1.111/my_project/site/read_article/title%20mid%20left%3Cp%3Etesttesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lifesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lifesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.%20True%20Mirror,%20can%20come%20to%20life.ife.%20True%20Mirror,%20can%20come%20to%20life.ife.%3C/p%3E%3Cp%3E%C2%A0%3C/p%3E%3Cp%3Etesttesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lifesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lifesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.%20True%20Mirror,%20can%20come%20to%20life.ife.%20True%20Mirror,%20can%20come%20to%20life.ife.%3C/p%3E%3Cp%3Etesttesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%3C/p%3E.html
and i get this error message:
An Error Was Encountered
The URI you submitted has disallowed characters.
how do i do it correctly? the url looks very messy, how do pass the string and still have a clean url? please help me with it.

Why not pass the article ID instead? You could then access the article through the controller function, count the characters and decide the method of display.
Alternatively, you could use CI's Session Flashdata to pass the article title/body to the next controller and access it that way.
The URI is failing as security is set up to deny specific characters being passed in the URL. This is for your protection, but, although not recommended, could be disabled in the config files if required.

Related

Codeigniter get parameters from url

I have a url "http://localhost/codeigniter/index.php/manual_export/". I need to get last segment from the url which is suppose to be a id. So for example "http://localhost/codeigniter/index.php/manual_export/2". I need to get the last segment which is "2".
I tired to use following code:
$id = end($this->uri->segment_array());
This works when I don't add "2" to the url and gives me "manual_export". However when I pass the id to the url I get an error "The page you requested was not found.". I think this is to do with routing. How can I fix this error.
the other way to do it is by defining a route, it will then be converted to a param
so for example if your controller is called manual_export and the method is getrecord
in the file application/routes.php
$route['manual_export/(:any)'] = "manual_export/getrecord/$1";
in your controller manual_export
function getrecord($id){ // etc etc }
You should use:
$this->uri->segment(n);//in your case n == 2 count starts just after index.php
Docs.

CodeIgniter Captcha value munged by session userdata?

I'm attempting to store a CodeIgniter captcha helper challenge word in a Session variable, as follows:
$cap = create_captcha($val /* array of params */); // Works fine
$this->session->set_userdata('cap-word', $cap['word']);
If I echo $cap['word'] before and after the session set, it is correct (i.e. what was displayed in the browser). If I retrieve the session variable immediately after setting it, it's also correct.
However what gets stored in the session is completely different - it's the right length (character count) but a totally different string. Hence, when I try to retrieve the userdata on the server side (captcha validation) it gets the wrong value.
(I'm configured for 'sess_use_database' and inspecting the user data values in phpMyAdmin after each page load. Cookie encryption is disabled.)
Debug attempts:
I've tried prepending / appending known strings to the captcha challenge word before storing it in the session. The known strings make it into the session user data just fine, but are prepended / appended to an incorrect captcha word.
I've tried replacing the challenge word entirely with a fixed string. This makes the round trip through the session no problem.
I've tried saving the captcha challenge word to a different string variable (rather than passing it directly from $cap['word]') with the same result; only the challenge portion gets "munged" on landing in the session.
After debugging, my code looks more like:
$cap = create_captcha($vals);
echo("<br>cap = ");
print_r($cap);
echo("<br>cap['word'] = " . $cap['word']);
$theword = $cap['word'];
echo("<br> Before theword = " . $theword);
$this->session->set_userdata('cap-word', $theword . 'abcdefg');
echo("<br> After theword = " . $theword);
echo("<br> Session output = " . $this->session->userdata('cap-word'));
This produces the following output in the browser:
cap = Array ( [word] => 5CZaDeHm [time] => 1436765602.678 [image] =>
{the image})
cap['word'] = 5CZaDeHm
Before theword = 5CZaDeHm
After theword = 5CZaDeHm
Session output = 5CZaDeHmabcdefg
However, what's stored in the session table userdata fields (and, thus, what pops out when I call $this->session->userdata('cap-word') on submit request) is:
a:2:{s:9:"user_data";s:0:"";s:8:"cap-word";s:15:"3g5hb1I3abcdefg";}
Hence, the substring '5CZaDeHm' within $theword has been seemingly replaced by '3g5hb1I3' during the call to $this->session->set_userdata. I have no idea why, or even how this is possible?!
Update 2015-07-13 07:50 EDT: As usual, Occam's Razor applies. Turns out on each page load, my controller is being called twice, generating 2 captcha images with 2 corresponding challenge words. One of these appeared in the browser, the other in the session's database row. Now to figure out why...
Turns out the CodeIgniter controller was being called twice because I used a relative path in the href parameter of a CSS tag. This resulted in the browser attempting to load the stylesheet relative to the page, which triggered a 2nd load of the page.

How to get a url parameter in Magento controller?

Is there a Magento function to get the value of "id" from this url:
http://example.com/path/action/id/123
I know I can split the url on "/" to get the value, but I'd prefer a single function.
This doesn't work:
$id = $this->getRequest()->getParam('id');
It only works if I use http://example.com/path/action?id=123
Magento's default routing algorithm uses three part URLs.
http://example.com/front-name/controller-name/action-method
So when you call
http://example.com/path/action/id/123
The word path is your front name, action is your controller name, and id is your action method. After these three methods, you can use getParam to grab a key/value pair
http://example.com/path/action/id/foo/123
//in a controller
var_dump($this->getRequest()->getParam('foo'));
You may also use the getParams method to grab an array of parameters
$this->getRequest()->getParams()
If your url is the following structure: http://yoursiteurl.com/index.php/admin/sales_order_invoice/save/order_id/1795/key/b62f67bcaa908cdf54f0d4260d4fa847/
then use:
echo $this->getRequest()->getParam('order_id'); // output is 1795
If you want to get All Url Value or Parameter value than use below code.
var_dump($this->getRequest()->getParams());
If your url is like this: http://magentoo.blogspot.com/magentooo/userId=21
then use this to get the value of url
echo $_GET['userId'];
If you want more info about this click here.
If it's a Magento module, you can use the Varien Object getter. If it's for your own module controller, you may want to use the register method.
Source: http://www.vjtemplates.com/blog/magento/register-and-registry

Passing parameters through URL in CodeIgniter

I'm having a problem regarding passing parameter from url. The function I use sometimes give the correct result and sometimes not.
here is my controller functions:
public function link_gen(){
$text = "i have lost my password please help me";
$encrypted_text = $this->encrypt->encode($text);
$encrypted_url = urlencode($encrypted_text);
echo $encrypted_url. br();
echo br(). $this->retrive(urldecode($encrypted_url));
echo anchor('encryption/ret_back?username='.$encrypted_url, 'click me');
// echo anchor('encryption/ret_back/'.$encrypted_url, 'click me');
}
public function ret_back(){
// echo br(). $this->retrive(urldecode($str));
$user = $this->input->get('username');
echo br(). $this->retrive(urldecode($user));
echo $user. br();
echo "hellooooo". br();
}
For the sake of testing I'm encrypting a text then encode it in URL using urlencode(), then I pass this string to another function using URL, and then decode in the string in URL using urldecode() and then decode the text. But when I run this, sometimes the decoded text appears and sometimes it does not.
Can anybody tell me the reason and solution for this?
I want to use this in a 'forgot password' module.
It depends on how you create your links and how do you retrieve the data from url.
Anyway these are simple examples to get url data:
get from $_GET[] (http://site.com/?q=hello+world)
in this case you get the param q with echo $this->input->get('q',true);
get data from uri segments (http://site.com/id/230)
in this case you get id value via echo $this->uri->segment(2);
Then, if data in url exists it is not possible that the system didn't get that, so be sure data is in your url when opening url.

Best practice in handling invalid parameter CodeIgniter

Let's say I have a method at Controller named
book($chapter,$page);
where $chapter and $page must be integer. To access the method, the URI will look like
book/chapter/page
For example,
book/1/1
If user try to access the URI without passing all parameter, or wrong parameter, like
book/1/
or
book/abcxyz/1
I can do some if else statements to handle, like
if(!empty($page)){
//process
}else{
//redirect
}
My question is, is there any best practice to handle those invalid parameters passed by user? My ultimate goal is to redirect to the main page whenever there is an invalid parameter? How can I achieve this?
Using the CodeIgniter routing in config/routes.php is pretty useful here, something like this:
$route['book/(:num)/(:num)'] = "book/$1/$2";
$route['book/(:any)'] = "error";
$route['book'] = "error";
Should catch everything. You can have pretty much any regular expressions in the routes, so can validate that the parameters are numeric, start with a lowercase letter, etc..
The best logic here seems to be adding the default values:
book($chapter = 1, $page = 1);
and then checking if they are numeric
So it automatically opens the 1st page of the 1st chapter if there are parameter missing or non-numeric.

Resources