Passing parameters through URL in CodeIgniter - 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.

Related

Get previous url in laravel

I am trying to get previous url in laravel, I have tried this code,
echo redirect()->back()->getTargetUrl();
die();
when i simply echo this code, This correctly returns the url, but when i try to save it in some variable, It does not work
You can find it in the docs.
https://laravel.com/docs/8.x/urls#accessing-the-current-url
// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();

codeigniter's link does not work and cannot match with the function parameter

I have the function
index($errorMsg, $successMsg) {....}
It works when I type in the URL.
http://localhost/website/index.php/home/index/1234/5678
But It does not work But when I type in the URL.
http://localhost/website/index.php/home/index//5678
5678 will be $errorMsg.
Is there any hints
Really bad solution for passing success or error parameters via function arguments by get method in CI.
Try use session flash data to pass success or error messages in redirection view.
$this->session->set_flashdata('errorMsg', '1234');
$this->session->set_flashdata('successMsg', '5678');
And show variables:
function index()
{
echo $this->session->flashdata('errorMsg');
echo $this->session->flashdata('successMsg');
}
Use this solution to avoid errors.
Your solution
Declare function like this
index($errorMsg, $successMsg=NULL) {....}
Explanation
index($errorMsg, $successMsg) function required both arguments(variables). If you don't pass it will produce error which is happening in your case.
index($errorMsg, $successMsg=NULL) function required first one and 2nd one is optional.If you don't pass 2nd argument $successMsg value will be null.
Note
/home/index//5678 no need use double slash after index.One will solve your purpose.You need to just check $successMsg.If it is null means you passed only $errorMsg

Codeigniter - htmlspecialchars() on input not working

I'm using htmlspecialchars() on input field for user last name to prevent xss, but it's not working..
Let's say $user_data->user_last_name; is my user last name, so I did:
htmlspecialchars( $user_data->user_last_name, ENT_QUOTES, 'UTF-8' );
When I try to save user last name as 'Lastname<script>alert("xss")</script>', I get JS alert with 'xss' message.
Any clue maybe?
Try this, may work:
$string = htmlentities($user_data->user_last_name, ENT_QUOTES, 'ISO-8859-15');
While retrieving the input you should use:
$value = $this->input->post('input_name', true);
Here, true will clean the input value of xss.
It works, but The output is interpreted by your browser as HTML
// use That Simple Line Above Your Code To See The Real output
<?php
header('Content-Type: text/plain');
?>

Jsonp request with Swedish character gives null response

I'm using jsonp to request data from a web server to my application (built in sencha). The request has a dynamic parameter called 'sokt'. Sometimes the parameter has a swedish character (å, ä, ö) and sometimes it doesn't.
As long as there's no swedish charachter the server returns the expected result: for example:
http://mywebsite.se/jsonnew.php?sokt=test&_dc=1370095960312&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback31
But if there's a swedish character in the request, the server returns nothing. Example:
http://mywebsite.se/jsonnew.php?sokt=enastående&_dc=1370096101366&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback39
But then if i manually url encode the request enastående to enast%E5ende I get the expected result.
So, to summarize, the problem seems to be that the request from jsonp doesn't url encode the query string, it sends the unicode string which is not accepted by the php script that formats the response (which has a utf_8-header). If this really is the reason this is not working, which I'm not sure of, how would I solve this?
EDIT with code:
This is my request:
Ext.getStore('storen').setProxy({ type: 'jsonp', url: 'http://mywebsite.se/synonymer/jsonnew.php?sokt=' + param}).load()
And this is php script:
<?php
header('Content-Type: text/javascript; charset=utf8');
include("config.php");
$dbh = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$sokt = $_GET['sokt'];
$stmt = $dbh->prepare("SELECT * FROM table WHERE w1 = :sokt");
$stmt->bindParam(':sokt', $sokt);
$stmt->execute();
$output = array();
while ( $row = $stmt->fetch() ) {
$output[] = array("key" => utf8_encode($row['w2']));
}
$callback = $_REQUEST['callback'];
// Create the output object.
//start output
if ($callback) {
echo $callback . '(' . json_encode($output) . ');';
} else {
echo json_encode($output);
}
?>
You are attaching the value of sokt in the url of the proxy, hence the proxy want change anything. So you have to care about this yourself. There is a native method for that encodeURIComponent()
Ext.getStore('storen').setProxy({ type: 'jsonp', url: 'http://mywebsite.se/synonymer/jsonnew.php?sokt=' + encodeURIComponent(param)}).load()
As I can see someone other already answered this but deleted his answer cause you told him this want work, so here are some additional infomation:
Now your request should be send encoded like this
http://mywebsite.se/jsonnew.php?sokt=enast%C3%A5ende&_dc=1370096101366&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback39
Verify that by using your browsers developer tools. If that is so the frontend has done anything right and you will have to check the backend.
There you have to check if the param is URLdecoded and if not decode it
$sokt = urldecode($_GET['sokt']);
and you have to ensure that your database is really storing all the correct way. So use some tool like PHPMyAdmin (MySQL), ManagementStudio (MS SQL) or any query tool to see if your database behaves correct and the that the Data is stored in the correct format.
Checking all this should lead you to the error
When dealing with utf-8, first make sure everything is configured for utf-8
HTML
PHP, file store (file encoding)
Web Server
Database Columns (Collation)
Database Connection (Client and Server)
If done so, you're almost close to entirely forget about troubles with äöüß (No swedish chars here ;-) ).
And obviously you don't need any utf8_en|decode functions which do not work properly anyway, see the comments on php.net to these functions.
I've recently switched from ISO-8859-1 to utf-8 and it was a hell of work, but afterwards any "char conversion" was necessary any longer at all.
So: leaveing those äöü in an URL unencoded may still lead to problems. I don't kno Ext but assume that their doing a proper UTF-8 encoding of the extraParams as json requires that!
Have you tried
Ext.getStore('storen').setProxy({
type: 'jsonp'
,url: 'http://mywebsite.se/synonymer/jsonnew.php'
,extraParams: {
sokt: encodeURIComponent(params)
}
}).load();
Try using the extraParams option of the proxy instead of hardcoding it in the URL:
Ext.getStore('storen').setProxy({
type: 'jsonp'
,url: 'http://mywebsite.se/synonymer/jsonnew.php'
,extraParams: {
sokt: param
}
}).load();
Or, even simple, the params option of the load() method:
Ext.getStore('storen').load({params: {sokt: param});
Try decoding your param on the server side:
$sokt = urldecode($_GET['sokt']);
Try forcing your database connection to UTF8 before executing your query:
$dbh->prepare("SET NAMES 'utf8'")->execute();
Maybe one of these or the combination of both will work.

passing a large string through url in 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.

Resources