0

For including php files I use

if ( !defined('ABSPATH') ) define('ABSPATH', str_replace("core/","",dirname(__FILE__) . '/'));

As my config file is kept in core I have to remove the folder it is in.

Now this works for including php files as it gives the correct file path,and it also works on the server.

But it will not work on the following line

<link rel="stylesheet" href="css/raw/style/site.css" media="screen"/>

As this needs a url instead of a file path. Also the same for images, you need a url for these also.

After searching stack overflow people have said to use the document root and all this, but document root is only ever good on the server.

Locally I use wampserver and I have many different webpages in different folders so document root is no good. Also I write all my webpages locally before uploading. The absolute path has to work locally and on the server. I can't use a fixed path either. As I have many versions of a website. Whenever I update a website I create a new folder and number it. So constantly changing a fixed path is not going to work.

Most of my webpages use relative paths because I haven't come up with a good solution to absolute paths here.

So what is the best solution for absolute path urls which work on localhost and on the server?

PS. I know there are posts on stack overflow which answer this question, but none are giving the answer I want. Most say define a local and server address, but I use multiple folders so this won't work for me.

Thomas Williams
  • 1,528
  • 1
  • 18
  • 37
  • Using Virtual Hosts for each of your projects would solve this issue. Thats why they are suggested, so you dont get into this situation when migrating to the LIVE server [See here for reasons to use VH's and how to set them up](http://stackoverflow.com/questions/23665064/project-links-do-not-work-on-wamp-server/23990618#23990618) – RiggsFolly Aug 27 '16 at 16:51
  • I have never heard of virtual hosts on wampserver before. I know you have them in microsoft IIS. I will read your link to see if it helps. – Thomas Williams Aug 27 '16 at 16:55
  • Actually virtual hosts would not solve my problem. I have one folder which inside has 50 different versions of the same website. As I add new stuff which might break my site I create a new version. If I had to create a virtual host for every folder it would take me a very long time to do it. However I may play with this. So thanks for the suggestion. – Thomas Williams Aug 27 '16 at 18:18
  • A proper source management tool like GIT woudl seem to be what you actually need – RiggsFolly Aug 27 '16 at 18:43
  • I have looked at GIT and decided it is too much trouble to set up with all this command line stuff, and pushing and pulling. Seemed like I would spend longer playing with that instead of getting any coding done. – Thomas Williams Aug 27 '16 at 18:48

1 Answers1

0

I have finally found the answer which works on localhost and on the server.

function getCurrentURL()
{
    $currentURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    $currentURL .= $_SERVER["SERVER_NAME"];

    if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
    {
        $currentURL .= ":".$_SERVER["SERVER_PORT"];
    } 

        $currentURL .= $_SERVER["REQUEST_URI"];
    return $currentURL;
}

I run it in my config with this

if(!isset($_SESSION['ABSURL'])) $_SESSION['ABSURL']=getCurrentURL();

I found the answer on this site http://hayageek.com/php-get-current-url/

Thomas Williams
  • 1,528
  • 1
  • 18
  • 37