Handling question mark in url in codeigniter - url

I am using a payment method which on success returns a url like mysite/payment/sucess?auth=SDX53641sSDFSDF, but since i am using codeigniter, question marks in url are not working for me.
I tried routing but it didnt work. As a final resort i created a pre system hook and unset the GET part from the url for with i had to set
$config["uri_protocol"] = "REQUEST_URI";
It worked that way but all other links in my site were not working as intended, tried changing the uri_protocol but could not make it work by any means.
So basically my problem is handling the ?auth=SDFSEFSDFXsdf5345sdf part in my url, whenever the paypment method redirects to my site with the url mentioned above, it gets redirected to homepage instead of the function inside controller.
How can i handle this, i'm using codeIgniter 1.7 version, and couldnt find any way.
Please suggest some solution.

I think I would extend the core URI class, by creating new file at application/libraries/MY_URI.php which will extend the CI_URI class, then copy the _fetch_uri_string method and here you can add your logic if the $_SERVER['QUERY_STRING'] is present:
class MY_URI extends CI_URI
{
function __construct()
{
parent::CI_URI();
}
//Whatever this method returns the router class is using to map controller and action accordingly.
function _fetch_uri_string()
{
if(isset($_SERVER['QUERY_STRING']) AND !empty($_GET['auth']))
{
//Do your logic here, For example you can do something like if you are using REQUEST_URI
$this->uri_string = 'payment/success/'.$_GET['auth'];
return;
//You will set the uri_string to controller/action/param the CI way and process further
}
//Here goes the rest of the method that you copied
}
}
Also please NOTE that you must do security check of your URL and I hope this works for you, otherwise you can try extending the CI_Router class or other methods (experiment little bit with few methods, though the _set_routing is important). These 2 classes are responsible for the intercepted urls and mapping them to controller/action/params in CI.

I believe this thread holds the answer.
Basically add
$config['enable_query_strings'] = TRUE;
to your config.php

hope this will help you ! no need to change htaccess,just change your code.
redirect(base_url()."controller_name/function_name");

Looking at the example code in the Stripe docs:
https://stripe.com/docs/payments/checkout/set-up-a-subscription
one might conclude that you can only retrieve a checkout_session_id as a named GET parameter like this:
'success_url' => 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
After playing around with .htaccess and config settings (as in previous answers) and finally getting it to work, it suddenly dawned on me that the DEVELOPER is in control of the success_url format, not Stripe. So one can avoid all the problems with a GET parameter by specifying the success url in the normal, clean Codeigniter format, like this:
'success_url' => 'https://example.com/success/{CHECKOUT_SESSION_ID}',
In my routes file, the incoming CHECKOUT_SESSION_ID string is passed to the Subscription controller's checkout_success method with this route:
$route['success/(:any)'] = 'subscription/checkout_success/$1';
The method accepts the $checkout_session_id like this:
function checkout_success($checkout_session_id)
{
// Do something with the $checkout_session_id...
}
I'm new to Stripe Checkout but this seems to simplify the integration without breaking Codeigniter's GET processing rules.

Related

Laravel variable to URL without changing url-route path

I'm new to laravel and am interested in adding a variable from a db to the end of my url without effecting the path. for example if a user logs in and has variable = hello assigned in the db the url will read http://app.com/home/hello. However I don't want the this to change where my routing, so its still looking at /home and not for /home/hello
I've tried to change the routing in web.php as follows:
Route::get('/home',function(){
return redirect('home/{{Auth::user()->variable}}');
});
Route::get('/home/{{Auth::user()->variable}}', 'HomeController#index');
But this seems messy and it's not pulling my variable from the db. I'm sure there's a better way I haven't been
able to find. I'm not looking for a complete solution, just a link to a tutorial or point in the right direction.
You should be able to do something like:
Route::get('/home',function(){
return redirect('home/'.Auth::user()->variable);
});
Route::get('/home/{variable}', 'HomeController#index');
Then the value of 'variable' would be available in your HomeController index method
public function index($variable) { ... }
https://laravel.com/docs/5.7/routing#route-parameters

How to get domain root url in Laravel 4?

I'm ready to scream how hard can this be? I've been trying for too long.
If I have http://www.example.com/more/pages/page.php or similar I want to be able to get
www.example.com.
Thats all. So I can use it as I please. This will of course change if on production or development so I want to ascertain it dynamically.
Request::root()
returns http://www.example.com/more/pages/page.php
URL::to('/')
returns http://www.example.com/more/pages/page.php
How do I get this? Why am I having so much trouble to do this??
UPDATE (2017-07-12)
A better solution is actually to use Request::getHost()
Previous answer:
I just checked and Request::root(); does return http://www.example.com in my case, no matter which route I'm on. You can then do the following to strip off the http:// part:
if (starts_with(Request::root(), 'http://'))
{
$domain = substr (Request::root(), 7); // $domain is now 'www.example.com'
}
You may want to double check or post more code (routes.php, controller code, ...) if the problem persists.
Another solution is to simply use $_SERVER['SERVER_NAME'].
You also may test any of these:
Request::server ("SERVER_NAME")
Request::server ("HTTP_HOST")
It seems better than making any treatment of
Request::root()
All right.
In Laravel 5.1 and later you can use
request()->getHost();
or
request()->getHttpHost();
(the second one will add port if it's not standard one)
My hint:
FIND IF EXISTS in .env:
APP_URL=http://yourhost.dev
REPLACE TO (OR ADD)
APP_DOMAIN=yourhost.dev
FIND in config/app.php:
'url' => env('APP_URL'),
REPLACE TO
'domain' => env('APP_DOMAIN'),
'url' => 'http://' . env('APP_DOMAIN'),
USE:
Config::get('app.domain'); // yourhost.dev
Config::get('app.url') // http://yourhost.dev
Do your magic!
This is for Laravel 5.1 and I am not sure does it work for earlier versions but if somebody search on Google and lands here it might be handy in middleware handle function gets $request parameter:
$request->server->get('SERVER_NAME')
outside of middleware handle method you can access it by helper function request()
request()->server->get('SERVER_NAME')
use directly where you want controller or web.php
Request::getHost();
I think you can use asset('/')

Yiiframework - parse a url string to (module, controller, action) array

I am looking for a tool(class or method or ...) in Yii that, get a string (a url) and return an array contains module, controller, action and other get_params.
I cannot use explode because it is possible that module dose not exists or can not find out that 'a/b' is module/controller or controller/action !
This is kind a weird of Yii - why I can't just do something similar to:
$this->uri->segment(1);
Many FWs have this feature, so simple and obvious.
PS Also, why I should write methods in Helpers like above to do things FW must definitely do? And yes I don't want to act like:
$request = new Request();
$request->setUrl('/site/contact');
Yii::$app->urlManager->parseRequest($request);
This is totally stupid, not after setting the url - just f***ing grab it like in old wonderful days. They should write an alternative either.

Get request URI in jinja2?

I'm working on a Pylons project using Jinja2 templating. I want to test for the request URI and/or controller inside the Jinja2 templates - is there an equivalent to a getRequestUri() call? I can set a context variable as a flag inside all the controller methods to do what I want, but that seems a bit like writing my home address on each and every one of my house keys... i.e. not quite the right way to do it.
Solution: not quite a function call, but I can test against url.environ.PATH_INFO. It only gives me the URL path, not the hostname, and I don't know that it would give me the query string, but it gives me what I need.

Codeigniter _remap function

Please help I want to use first URI segment into my CodeIgniter website.
Like when I open these url they opens my profile:
http://www.facebook.com/buddyforever
or
http://www.myspace.com/zarpio
How can I do this with CodeIgniter? I checked _remap function but first coming controller how to hide controller?
You can do this using the URL routing of codeigniter...
If you want your URL to be http://www.mydomain.com/zarpio and you want it to refer to your_controller, then do the following.
/config/routes.php
$route['(.*)'] = "your_controller/$1"; // Now, `zarpio` will be passed to `your_controller`
You can access it in your controller like this...
$my_name = $this->uri->rsegment(2);
However I do not suggest this way of configuring URLs. A better way would be...
$route['users/(.*)'] = "your_controller/$1";
This way, you're just renaming your controller name your_controller to users.
If you want to access profile of a user, you can do it like this...
$route['users/profile/(.*)'] = "another_controller/method/$1";
$route['users/(.*)'] = "your_controller/$1";
Consider the order of routing. Since you wrote users/(.*) in your route, it will match users/zarpio as well as users/profile/zarpio, and route both of them to your_controller/$1, which in the case of profile will give you a 404 page not found error. That is why you need to write users/profile/(.*) before users/(.*) in your routing configuration.
More information in codeigniter URI class documentation

Resources