CodeIgniter: urlencoded URL in URI segment does not work - codeigniter

I'm trying to put a URL as the value of one of my URI segments in CI. My controller method is defined to accept such an argument. However, when I go to the URL, I get a 404 error. For example:
www.domain.com/foo/urlencoded-url/
Any ideas what's wrong? Should I do this via GET instead?
UPDATE:
// URL that generates 404
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
// This is in my profile_manager controller
public function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '', $url_current = '')
If I remove the second URI segement, I don't get a 404: http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/

It seems that the %2F breaks things for apache.
Possible solutions:
preg_replace the /'s to -'s (or something else) before sending the url then switch it back on the other end.
Set apache to AllowEncodedSlashes On
bit of a hack, but you could even save the url to a session variable or something instead of sending through the url *shrug *
double url encode it before sending

Pass urlendode()'d URL in segment and then decode it with own (MY_*) class:
application/core/MY_URI.php:
class MY_URI extends CI_URI {
function _filter_uri($str)
{
return rawurldecode(parent::_filter_uri($str));
}
}
// EOF

You may need to change the rule in config/route.php to accept the encoded characters in URL. Also you can take a look at some of the solution from below articles:
http://codeigniter.com/forums/viewthread/81365/
http://sholsinger.com/archive/2009/04/passing-email-addresses-in-urls-with-codeigniter/
Passing URL in Codeigniter URL segment

I actually had to do urlencode(urlencode(urlencode(
and urldecode(urldecode(urldecode(
3 times!! and it finally worked, twice didn't cut it.

try
function __autoload($class){
if(!empty($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_QUERY_STRING'] = $_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_URL'] = $_SERVER['argv'][0] = urldecode($_SERVER['REQUEST_URI']);
}
}
in config.php
this method work for me

This is very old, but I thought I'd share my solution.
Instead of accepting the parameter as a url path, accept it as a get variable:
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338?url_current=http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
and in code:
function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '') {
$url_current = $this->input->get('url_current');
...
This seems to work.

Related

How do I redirect to a URL with query parameters?

I am trying to do a redirect with query parameters, using the redirect() helper:
$link = 'https://example.com' . '?key1=value1&key2=value2';
return redirect()->to($link);
The problem is that when the $link is passed to the to() method Laravel removes the question mark leading the query string, so it turns this:
https://example.com?key1=value1&key2=value2
into this:
https://example.comkey1=value1&key2=value2
(again, notice the missing ? in the final link).
How do I make a redirect with query params appended to a custom URL?
Use:
return redirect($link);
If you want to redirect a named route with query string, use:
return redirect()->route('route_name',['key'=> $value]);
Documentation
The approved answer explains it all, according to the documentation. However, if you are still interested in finding some kind of "hard-coded" alternative:
$link = "https://example.com?key1={$value1}&key2={$value2}";
Then,
return redirect($link);
Reference
If the link is to a page on your domain, you don't need to re-write the domain name, just:
$link = "?key1=${value1}&key2=${value2}";
Laravel will automatically prepend the URL with your APP_URL (.env)
If you're building a Single Page Application (SPA) and you want to redirect to a specific page within your app from a server request, you can use the query method from the Arr helper class. Here is an example:
$result = Arr::query([
'result' => 'success',
'code' => '200'
]);
return redirect("/purchase?$result");
This will redirect the user to the /purchase page with the query parameters result=success and code=200.
For example, the final url would be:
http://example.com/purchase?result=success&code=200

preg_match to only allow https:// in an URL

I'd like to allow only https:// links to be used as remote avatar images in phpbb to avoid mixed content. This seems to be the code that is used to check whether the entered url is correct (to be found in /phpbb/avatar/driver/remote.php):
if (!preg_match('#^(http|https|ftp)://(?:(.*?\.)*?[a-z0-9\-]+?\.[a-z]{2,4}|(?:\d{1,3}\.){3,5}\d{1,3}):?([0-9]*?).*?\.('. implode('|', $this->allowed_extensions) . ')$#i', $url))
{
$error[] = 'AVATAR_URL_INVALID';
return false;
}
I'd like to add a if{}-condition before this code block to give an informative error message if the user selected an image from a non-secure server. Can anyone help me defining the correct preg_match() string please?
Based on the suggestion by #casimir, I used the following code and it works:
$urlchk = parse_url($url);
$urlscheme = isset($urlchk['scheme']) ? $urlchk['scheme'].'://' : 'http://';
if ($urlscheme=='http://'){
// error message
}

Difficulty with two segment uri

They are trying to create the url, where the first segment is the User and the second is his file, ex: www.exemplo.com/joao/ball
Controller
public function user() {
$user_url = $this->uri->segment(1);
}
^^ This would return the profile with every file: www.exemplo.com/joao
public function arquivo() {
$arquivo_url = $this->uri->segment(2);
}
^^ This specific file: www.exemplo.com/joao/bola
Routes
$route['(:any)'] = 'home/user/$1';
$route['??'] = 'home/arquivo/$1';
To solve your problem, you should use route like this..
$route['(:any)/(:any)'] = 'home/arquivo';
$route['(:any)'] = 'home/user';
but as far you with your project this type of routing will give your some hard time. i suggest you to use explicit route name because (:any) refer any thing can be pass through this url.
You can use routes to map the URI and its parameters to the relevant function. CodeIgniter routes behave differently depending on the version of CI.
In CodeIgniter 2.2.0 (:any) is equivalent to the regex, .+ - matches one or more of any character (excluding line breaks); in 3.0 and the current development version it is equivalent to [^/]+ - one or more of any character, excluding line breaks.
The latter is more useful in this case, as you want to identify the two parameters (separated by a forward slash).
In 2.2.0:
$route['([^/]+)/([^/]+)'] = 'home/arquivo/$1/$2';
$route['(:any)'] = 'home/user/$1';
In 3.0:
$route['(:any)/(:any)'] = 'home/arquivo/$1/$2';
$route['(:any)'] = 'home/user/$1';
Controller functions will usually pass the URI parameters as function parameters like this:
public function user($user)
{
// Show the user's profile
}
public function arquivo($user, $file)
{
// Show the file for the user
}
Able to resolve with the following code.
$route['([^/]+)'] = 'home/user/$1';
$route['(:any)/(:any)'] = 'home/arquivo/$1';

Laravel how to route old urls

I am using Laravel 4.
I have an old url that needs to be routable. It doesn't really matter what it's purpose is but it exists within the paypal systems and will be called regularly but cannot be changed (which is ridiculous I know).
I realise that this isn't the format url's are supposed to take in Laravel, but this is the url that will be called and I need to find a way to route it:
http://domain.com/forum/index.php?app=subscriptions&r_f_g=xxx-paypal
(xxx will be different on every request)
I can't figure out how to route this with laravel, i'd like to route it to the method PaypalController#ipbIpn so i've tried something like this:
Route::post('forum/index.php?app=subscriptions&r_f_g={id}-paypal', 'PaypalController#ipbIpn');
But this doesn't work, infact I can't even get this to work:
Route::post('forum/index.php', 'PaypalController#ipbIpn');
But this will:
Route::post('forum/index', 'PaypalController#ipbIpn');
So the question is how can I route the url, as it is at the top of this question, using Laravel?
For completeness I should say that this will always be a post not a get, but that shouldn't really make any difference to the solution.
Use this:
Route::post('forum/{file}', 'PaypalController#ipbIpn');
And then in the controller, use
public function forum($file) {
$request = Route::getRequest();
$q = (array) $request->query; // GET
$parameters = array();
foreach($q as $key => $pararr) {
$parameters = array_merge($parameters, $pararr);
}
}
You can then access the get parameters via e.g.
echo $parameters['app'];
you can use route redirection to mask and ending .php route ex:
Route::get('forum/index', ['uses'=> 'PaypalController#ipbIpn']);
Route::redirect('forum/index.php', 'forum/index');

Questionmark in url

I'm trying to use this http://api.jqueryui.com/autocomplete/#option-source with Laravel and so I need to send a GET request to a url which ends with "?term=foo". I've tried to escape the "?" with a backslash, which doesn't work. To clarify, this is what I want:
Route::get('search\?term=(:any)', function()
{
//do something
}
Is it possible to have questionmarks in the url with Laravel?
Just for others who may want a Clear Answer :
you have to write and use your code as follows:
Route::get('search', function()
{
$term = Input::get('term');
if(isset($term)){
//do other stuff !
}
}
Having a question mark in the URL should make no difference. You're using a PHP framework, and simply speaking, ...?term=parameters should not be problematic. To my knowledge, there should be no need to escape such a question mark... It is handled appropriately by default.
I believe the slug function is what you are looking for: http://laravel.com/api/class-Laravel.Str.html
From the API Doc:
slug( string $title, string $separator = '-' )
Generate a URL friendly "slug" from a given string.

Resources