Hi,
I have a WAMP based Site and would like to user path as a variable instead of fixed path. I tried variable like

$sgospel = "$server./sgospel/";
$include_path = "$sgospel./includes";
echo($server);
echo($sgospel);

Results are:

C:/wamp/www/
C:/wamp/www/./sgospel/

When I put

$f_path = "$include_path/inc.terms.php";
echo($f_path);

I get
C:/wamp/www/./sgospel/./includes/inc.terms.php

Instead of
http://localhost/sgospel/includes/inc.terms.php

I want it to be relative path instead of absolute path equivalent to :
"/gospel/"
"/"

Thanks alot.

I see there is $_SERVER and $_SERVER;
Which is the best?
Also In google there are alot of $_SERVER but I cannot find it anywhere in PHP manual and doesnt work on my WAMP

I see there is $_SERVER and $_SERVER;
Which is the best?
Also In google there are alot of $_SERVER but I cannot find it anywhere in PHP manual and doesnt work on my WAMP

Each has it's differences.

I don't think there is a HTML_ROOT, just a DOCUMENT_ROOT index in $_SERVER.

The SEVER_NAME is user defined. Your web server first checks the server name registered in DNS, then if that is not found, it will check the server configuration.
If you are distributing code, using the server name can lead to incorrect results since the server name does not reflect the host name, or the domain name in any way. Being the same is just as the host name is just the de facto configuration.

The HTTP_HOST is taken from the HTTP headers. It is the value of the "Host" http request header. This header was added later to HTTP specifications, but almost every HTTP request on the public web will have a HTTP_HOST. In fact, most site won't work without it since it is necessary to virtual host configurations.
You also cannot fully trust the HTTP_HOST value. The reason is that your server can be set up to listen on IPs and ignore the host parameter. The HTTP_HOST is essential, an you cannot gauge from PHP if it is a valid setting, unless you reflect the virtual host configuration in your PHP script, which is impossible if you're on shared hosting.

The DOCUMENT_ROOT is the safest to use. It is always the correct value is it is the last item processed in the HTTP request. It is when the HTTP request is actually mapped to the filesystem, and is virtual host specific (dependent on the HOST http request header, but not reliant on it).

In terms of determining paths to files, why not just use relative paths? I would think generating absolute paths harder then relative paths since it requires knowing where you are relative to the document root. Relative paths only require you to know where your file lie in your php application, relative to your application base directory.

So if your app structure is:

index.php
includes/file.php
includes/file2.php

Then your relative paths only need to be:

"includes/file.php";
"includes/file2.php";

Anyway, you should probably use $_SERVER for the absolute path. Or if you know where the current file resides, then you can use

$base_dir = dirname(__FILE__);

Thanks!
Relative paths you provided works.
I will retain DOCUMENT_ROOT for includes. I hope it is good idea
Isnt that so? Just reply though I have marked it solved

Thanks!
Relative paths you provided works.
I will retain DOCUMENT_ROOT for includes. I hope it is good idea
Isnt that so? Just reply though I have marked it solved

Yes, there is no harm using $_SERVER as you can be sure it will be correct on all PHP servers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.