Ukasha
Ukasha

Reputation: 2334

How to get current url with its get request in CodeIgniter?

This is my current url:

http://myhost/main?my_id=1,3

How can I get this url in CodeIgniter, and is it included with 'my_id'?

NOTE:

I mean when echo $url, it shows like this: http://myhost/main?my_id=1,3

Upvotes: 8

Views: 51767

Answers (5)

Kaabi
Kaabi

Reputation: 3

In Codeigniter 3

create a file in application/helpers/MY_url_helper.php

and add the following code to it

<?php
function current_url($ReturnQueryParams = FALSE)
{
    $CI =& get_instance();
    return $CI->config->site_url($CI->uri->uri_string() . (($ReturnQueryParams) ? "?{$_SERVER['QUERY_STRING']}" : ''));
}

and voila! just call your function current_url(true) will not return with server get parameters if the argument is set to true.

Upvotes: 0

Rahmad
Rahmad

Reputation: 66

In CodeIgniter 4, that problem have simple solution, you just have to use these:

$uri = current_url(true);
echo (string) $uri; // result for your url: http://myhost/main?my_id=1,3

This is explained in CodeIgniter 4 documentation: URI Strings

Upvotes: 1

Aldo
Aldo

Reputation: 923

The function current_url() can also return an class (URI).

This example uses the core functionally of CI4:


$uri = current_url(true);   // URI
$full_url = URI::createURIString(
    $uri->getScheme(),     // https
    $uri->getAuthority(),  // example.com
    $uri->getPath(),       // /foo/bar
    $uri->getQuery(),      // my_id=10&display=detail
); // https://example.com/foo/bar?my_id=10&display=detail

Upvotes: 0

vikram kuldeep
vikram kuldeep

Reputation: 1

simple way to get current url with its get request in CodeIgniter!

following this code

Loading a Helper

$this->load->helper('helper_file_name');  


$currentURL = current_url(); //http://domain/home

$params   = $_SERVER['QUERY_STRING']; //query=what+is+php   

$url = $currentURL . '?' . $params; 

echo $url;   //http://domain/home?query=what is php

Upvotes: -1

JYoThI
JYoThI

Reputation: 12085

simple get like this

echo $this->input->get('my_id');

Load the URL helper To get current url

$currentURL = current_url(); //http://myhost/main

$params   = $_SERVER['QUERY_STRING']; //my_id=1,3

$fullURL = $currentURL . '?' . $params; 

echo $fullURL;   //http://myhost/main?my_id=1,3

Note : make sure that query string is not empty and then concatenate .

Upvotes: 18

Related Questions