ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Get Current URL

March 17, 2008 10:57pm

Subscribe [11]
  • #1 / Mar 17, 2008 10:57pm

    cinewbie81

    123 posts

    Is there any way in codeigniter for me to get current URL ?

    I need to pass currrent URL to one javascript function, then in my javascript function i’ll redirect the page as following:

    function ReloadCurrentUrl(currentUrl)<br /> {<br /> // Set some value into cookie<br /> window . location . href = currentUrl;<br /> }

  • #2 / Mar 17, 2008 11:10pm

    steelaz

    252 posts

    I don’t think you need codeigniter to get current url, you could just use:

    <?= $_SERVER['REQUEST_URI'] ?>
  • #3 / Mar 18, 2008 3:08am

    cinewbie81

    123 posts

    Hi steelaz, cheers ..

    It works well in my local machine, but when i upload it to the server, $_SERVER[‘REQUEST_URI’] will return me ‘/’ instead of ‘/index.php/controller/function’.. any idea ??

  • #4 / Mar 18, 2008 3:41am

    xwero

    4145 posts

    You could echo uri->uri_string.

  • #5 / Mar 18, 2008 5:07am

    cinewbie81

    123 posts

    uri->uri_string will only return me the controller and function ..
    for eg:

    <a href="http://abc.com/index.php/controller/function?button=edit&d=1&sid=0.19077332919474066">http://abc.com/index.php/controller/function?button=edit&d=1&sid=0.19077332919474066</a>

    the call of uri->uri_string will only return me /controller/function .. I need others details behind as well (Yes, i know it’s not a good practice to have a URL like that, but I neeed to use it for certain reason) ..

    cheers !!

  • #6 / Mar 18, 2008 5:26am

    xwero

    4145 posts

    You could try this

    function full_url()
    {
       $ci=& get_instance();
       $return = $ci->config->site_url().$ci->uri->uri_string();
       if(count($_GET) > 0)
       {
          $get =  array();
          foreach($_GET as $key => $val)
          {
             $get[] = $key.'='.$val;
          }
          $return .= '?'.implode('&',$get);
       }
       return $return;
    }

    But i think there are better options.

  • #7 / Mar 18, 2008 5:31am

    cinewbie81

    123 posts

    Cheers, it’s works ..
    but i wonder what would be the better option

  • #8 / Mar 18, 2008 7:39am

    Pascal Kriete

    2589 posts

    You can reduce xwero’s code a little more:

    $query = $_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '';
    echo $this->config->site_url().$this->uri->uri_string(). $query;
  • #9 / Mar 18, 2008 9:07am

    Vince Stross

    61 posts

    if you’re using javascript anyway - couldn’t you just use the current value of [removed]?

    function ReloadCurrentUrl()
    {
        // Set some value into cookie
        window . location = window . location;
    }

    or, there is a refresh() method that I haven’t used in a while so I forget the exact syntax:

    window.refresh();

    something like that. However - I know the first example of setting the location property will work because I’ve done it many times. In javascript, anytime the “location” property of the window object is manipulated, it automatically sets the browser to change (or in your case refresh) the browser.

  • #10 / Mar 18, 2008 11:29am

    deeaamon

    4 posts

    $base_url=$GLOBALS[‘CFG’]->config[‘base_url’];

    I’ve been using javascripts in all of my scripts and they all work.  I just create a folder in the codeigniter root called scripts then call the javscripts using the $base_url variable for example

    echo “\n”;

    This ads the javascript to my page.

    Hope this helps

    DEEAAMON :coolsmile:

  • #11 / Dec 05, 2008 6:10pm

    mihu

    7 posts

    will current_url() works?

    I got similar issue.
    I need to use javascript to direct request to a processing step and redirect back to original place.
    Could someone guide me how I can get this done?

  • #12 / Dec 05, 2008 6:27pm

    Pascal Kriete

    2589 posts

    You could put the current_url into a cookie, redirect, and read the cookie to get back again.

    The url helper’s current_url() will work fine - the function didn’t exist when this thread was started.

  • #13 / Jun 13, 2011 4:54pm

    ortolano

    1 posts

    site_url(uri_string()) will do the job.

  • #14 / Jun 13, 2011 6:35pm

    danmontgomery

    1802 posts

    site_url(uri_string()) will do the job.

    This thread is 3 years old, but current_url() is more to the point.

  • #15 / Sep 01, 2011 5:06am

    guillemsola

    2 posts

    Yeah, post code is old but I found that actually current_url() doesn’t work properly with rewrite in htaccess, at least with my sites using language URL

    so here comes my trick.

    $url = base_url().uri_string()

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases