How to get current url with its get request in CodeIgniter?
Asked Answered
F

5

8

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

Foti answered 4/5, 2017 at 2:16 Comment(3)
If your url helper is loaded try current_url() -- codeigniter.com/user_guide/helpers/url_helper.htmlCinematograph
Thanks, bro. But do you know how to get it with its request ('my_id')?Foti
echo $this->input->get('my_id');Yiddish
Y
18

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 .

Yiddish answered 4/5, 2017 at 2:33 Comment(1)
$currentURL = current_url(); $params = (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : ''); $fullURL = $currentURL . '?' . $params; echo $fullURL; ======================== OR ================ echo current_url().(!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');Cordes
J
1

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

Josephinejosephson answered 10/5, 2023 at 4:58 Comment(0)
E
0

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

Effort answered 11/4, 2023 at 9:59 Comment(0)
B
0

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.

Brehm answered 22/10, 2023 at 15:6 Comment(0)
M
-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
Minton answered 5/11, 2022 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.