• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automatic config[base_url]

#31
[eluser]Adods[/eluser]
Yes, but if user call url like: http://www.domain.com/controller/method

will this script return 'http://www.domain.com' or 'http://www.domain.com/controller/method'??

#32
[eluser]paulopmx[/eluser]
[quote author="Adods" date="1209020764"]Yes, but if user call url like: http://www.domain.com/controller/method

will this script return 'http://www.domain.com' or 'http://www.domain.com/controller/method'??[/quote]

only http://www.domain.com/, but if your application is in a subfolder, then http://www.domain.com/subfolder/

#33
[eluser]Majd Taby[/eluser]
I would just like to say that I use this with CodeExtinguisher and haven't had problems with myself or anyone using codeextinguisher

#34
[eluser]bhogg[/eluser]
I neglected to add the last line in the code, though it could probably be pieced together from the previous posts:

Code:
$proto = "http" .
    ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
$server = isset($_SERVER['HTTP_HOST']) ?
    $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$server .= '/';

    if (strstr($server, 'localhost'))
        $server .= 'subdirectory/';
        
$config['base_url']    = $proto . $server;

As already said, this would return only the base http://www.domain.com/ or http://www.domain.com/subdirectory/ depending on what you specify for "$server .= 'subdirectory/' when testing on localhost.

The main advantage we've got now is the handling of http/https, you'll still need to update your subdirectory for each project's config.php file...

#35
[eluser]paulopmx[/eluser]
[quote author="bhogg" date="1209074987"]I neglected to add the last line in the code, though it could probably be pieced together from the previous posts:

Code:
$proto = "http" .
    ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
$server = isset($_SERVER['HTTP_HOST']) ?
    $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$server .= '/';

    if (strstr($server, 'localhost'))
        $server .= 'subdirectory/';
        
$config['base_url']    = $proto . $server;

As already said, this would return only the base http://www.domain.com/ or http://www.domain.com/subdirectory/ depending on what you specify for "$server .= 'subdirectory/' when testing on localhost.

The main advantage we've got now is the handling of http/https, you'll still need to update your subdirectory for each project's config.php file...[/quote]

But my original contribution already handles subdirectory, maybe you can just combine the two, where you handle the protocol.
Code:
$proto = "http" .
    ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
$server = isset($_SERVER['HTTP_HOST']) ?
    $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$server .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

#36
[eluser]bhogg[/eluser]
We'll need to tweak it a bit more... doesn't basename() return the final portion of the URL? So with someone accessing a page such as:

http://localhost/subdirectory/controllername/function/parm1/parm2

It would return something like "http://localhost/subdirectory/controllername/function/parm1" with the last piece removed. We could use a simple search for the first / after the root URL to get the subdirectory, if we limit it to one level deep. But what about when it's launched to www.domain.com? Wouldn't it then interpret the controllername as the subdirectory instead?

Brian

#37
[eluser]paulopmx[/eluser]
[quote author="bhogg" date="1209117011"]We'll need to tweak it a bit more... doesn't basename() return the final portion of the URL? So with someone accessing a page such as:

http://localhost/subdirectory/controllername/function/parm1/parm2

It would return something like "http://localhost/subdirectory/controllername/function/parm1" with the last piece removed. We could use a simple search for the first / after the root URL to get the subdirectory, if we limit it to one level deep. But what about when it's launched to www.domain.com? Wouldn't it then interpret the controllername as the subdirectory instead?

Brian[/quote]

Nope. That's why we are getting the scriptname, because we are only looking at the location of index.php only, and not actually processing the whole url. Why don't you try it first so you'll know.

I've tested this approach even if the application is in a sub/sub/subfolder, it wouldn't matter.

#38
[eluser]bhogg[/eluser]
I did try it first and it was cutting off the subdirectory, likely since I'm not using index.php (mod rewrite)...

#39
[eluser]bhogg[/eluser]
My apologies, I had an extra $server .= "/"; in there from a previous version of the code, your code works just fine!

#40
[eluser]wonderbread[/eluser]
Thanks for this. I will probably change servers a few times so this is always handy Tongue


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.