PHP: Web path

Can anyone please try to explain to me why one and the same thing is working on my local machine and not on my hosted machine

using this to get the WEB ROOT on the hosted machine:

defined('WEB_ROOT') ? null : define('WEB_ROOT', str_replace(array($docRoot, 'library/initialize.inc.php'), 'public_html', $thisFile))

Result:
web root:  /home/photogr1/public_html    //This is the public_html into which to place displayable files but it shows the whole absolute path - Wrong

This on my local machine:

defined('WEB_ROOT') ? null : define('WEB_ROOT', str_replace(array($docRoot, 'library/initialize.inc.php'), '', $thisFile));

Result:

web root: /PHOTOGRAPHERSCONNECT.com/    //this is a root folder in htdocs for this site - correct

Thanks a lot for your patience and support!!
czechmate1976Asked:
Who is Participating?
 
Mark BradyConnect With a Mentor Principal Data EngineerCommented:
I see your dilemma. As for how you would reference the one directly in the public folder? Not sure what you mean but in your coding for eg: if you have a page in the 'www/products/' directory and you wanted a link to return the user back to the home page your link would be:

<a href="../">Home</a>

I have started using a mysql database to hold my links and created a menu.php script to load them all in each page I needed a navigation menu. that way you only need to create the menu once and 'include("menu.php") for each page required. If you need to make changes to the links you can then do it once through a script or directly in MySql
0
 
czechmate1976Author Commented:
http://www.photographersconnect.com/home/photogr1/public_html/article/

The link above should look as follows:

http://www.photographersconnect.com/article/

I promise it was all fine on my local machine.. but I am burned spending too much time on this and miss a lot of obvious errors... Thanks for everyones help
0
 
Mark BradyConnect With a Mentor Principal Data EngineerCommented:
If you can't get it sorted out, you can achieve that URL another way. Just name the main page in the articles folder 'index.php or .html' ect and do the same for each other folder. Then the URL http://www.photographersconnect.com/article      will load the index page. Just my two cents worth!
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
czechmate1976Author Commented:
Thanks, Elvin. I thought about this before. I have them all in folders by subject and named index, there is actually only one main file in each folder... so this would work but I'd have to go and rewrite all the links in all scripts, if I only change the definition of the web root so it works correctly as when on my local machine, I can avoid re-writing all the links or references- and there are many :-)

what's more, how do I then refer to the one directly in the public folder i.e www.site.com/  ?

Really any advice much appreciated, even a two cent worth one :-)
0
 
rjdownConnect With a Mentor Commented:
How are you defining the variable called $docRoot? I've used servers before where $docRoot is predefined by the server configuration. Try echo'ing its value on your remote server. If it doesn't have a value, then you should set it like so:

$docRoot = $_SERVER['DOCUMENT_ROOT'];
0
 
czechmate1976Author Commented:
This is how it is set exactly -

$docRoot = $_SERVER['DOCUMENT_ROOT'];

the only difference between the folder setup on my machine and the host server is that the public folder (web root) on my machine was 'htdocs' into which I placed the folder with my site (photographersconnect.com) and the public pages were placed in folder 'public'. Something like
/xampplight/htdocs/PHOTOGRAPHERSCONNECT.COM/PUBLIC

while on the hosted one the public folder is 'public_html'  (=htdocs on my machine), into which I directly placed the files from my PUBLIC folder and everything else was placed outside the public_html in order to disable access to it.

home/photogr1/public_html/(my displayable pages)
home/photogr1/(script library and includes)

displaying the webroot on my machine will show /photographersconnect.com (correct)
but on the server 'home/photogr1/public_html/' none of which should be part of it. It should be like: /index.php or /article/

I don't know where I am going wrong, went through it so many times, echoing it all out but always get the whole absolute path instead of the one leading only to the webroot folder.

Please guys I really appreciate your tips on this This stops me from progressing onto more serious stuff and time is an issue. Thanks a lot to everyone for your input on this
0
 
czechmate1976Author Commented:
this is what I get echoing it out

docroot: /home/photogr1/public_html
file:         /home/photogr1/library/initialize.inc.php
web root: /home/photogr1/public_html
0
 
czechmate1976Author Commented:
Through various readings I now realized that my home machine is running on Windows while the hosted server is Unix machine.. would that be the cause of the problem if you look back at my question, the part how the webroot is put together. Thanks a lot
0
 
czechmate1976Author Commented:
thanks to everyone for participation on my question... I have finally found the solution to my problem.. and it works wonders :-)

Thanks a lot to everyone. The solution is below.
// setting up the web root for this application
$localpath=getenv("SCRIPT_NAME");
$absolutepath=realpath($localPath);
// a fix for Windows slashes
$absolutepath=str_replace("\\","/",$absolutepath);
$docRoot=substr($absolutepath,0,strpos($absolutepath,$localpath));

Open in new window

0
 
czechmate1976Author Commented:
thanks for your help
0
 
Mark BradyPrincipal Data EngineerCommented:
Hey I'm glad you got it sorted. It looks like you should have accepted your own answer for this one. This is because of future reference for other people having the same problem. Sure, they will see my answer and think it is the correct one but I believe your solution is the correct one. Mine will work as well but for future reference, I would ask a moderator what they think before accepting an answer if it is not what you are really looking for. I do however appreciate the points!

On another note, I to am a photographer. What is your site about?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.