1

Is there are a way to get base url (url_helper in codeigniter) like codeigniter using only php.

My project address looks like

http://localhost/proj1/

and it can be any name
how to get the URL which will give the same base URL on any file in the structure tree, depending on all situations like http:// or https://

1
  • I've provided the script and explained that codeigniter uses so you can implement it yourself. Commented Sep 5, 2014 at 10:46

3 Answers 3

2

Setup a file config.php

<?php

   define('base_url','http://localhost/proj1/');

?>

On every page include config.php. Like this :

page1.php

<?php

 include_once('config.php');

 echo base_url;


?>

For more info about define(); read this

Edit 1 :

if you want you can try this also :

<?php

       define('base_url','http://'.$_SERVER['HTTP_HOST'].'/');

    ?>

Or if your using sub folder then :

$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
define('base_url',$root);

Edit 2 : Try this one :

define('ROOTPATH', realpath(dirname(__FILE__)) . '/');


// installed in the docroot?
if (realpath(dirname(__FILE__)) == $_SERVER['DOCUMENT_ROOT'])
{
    define('ROOT', '/');
}
else
{
    define('ROOT', substr(ROOTPATH, strlen($_SERVER['DOCUMENT_ROOT'])+1));
}


$url = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . ROOT;

define('base_url',$url);

echo base_url;exit;
Sign up to request clarification or add additional context in comments.

12 Comments

how can it be portable? if i host it somewhere else the address would be something else...
You have to change the URL in config.php file if address has changed
Since you are using constant base_url on every page so if you change the URL in config.php file it will effect on every page
i know i can... but way without that? like codeigniter guesses automaticaly
Codeigniter guesses automatically ? In codeigniter also you have to set a base_url inside your config file
|
0

Or if you want it in php and not codeigniter try the $_SERVER variables of php

     echo "http://" . $_SERVER['SERVER_NAME']; 

Link

2 Comments

Then just keep the substr between $_SERVER['SERVER_NAME'] with the $_SERVER['REQUEST_URI']
Or $url = str_replace($_SERVER['REQUEST_URI'],"", $_SERVER['SERVER_NAME']); and you are done. If you think thats not a good solution i would like to explain it a bit further if you have the time in order to understand why. Thanks
0

This is how codeigniter does it, straight from the git repository:

https://github.com/EllisLab/CodeIgniter

CodeIgniter / application / config / config.php

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = '';

CodeIgniter / system / core / Config.php

/**
 * Class constructor
 *
 * Sets the $config data from the primary config.php file as a class variable.
 *
 * @return  void
 */
public function __construct()
{
    $this->config =& get_config();
    log_message('debug', 'Config Class Initialized');

    // Set the base_url automatically if none was provided
    if (empty($this->config['base_url']))
    {
        if (isset($_SERVER['HTTP_HOST']))
        {
            $base_url = (is_https() ? 'https' : 'http')
                .'://'.$_SERVER['HTTP_HOST']
                .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME'])));
        }
        else
        {
            $base_url = 'http://localhost/';
        }

        $this->set_item('base_url', $base_url);
    }
}

You should provide a base url in the config file, I presume this will then get autoloaded by codeigniter and the base_url() will return this value.

Whenever config.php gets instantiated it checks to see if the base url has been configured in the config.php, if it hasn't it will try and guess by checking the host, protocol base name of the script etc. Then the set_item method is called, which would set $config['base_url'] to whatever the logic above has determined to be the base url

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.