Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi all, I wasn't sure where to post this so I figured I would try here. I am a big fan of the PHP6 MySQL 5 Visual QuickPro Guide, I learned PHP and MySQL exclusive through it, and when I had questions the associated forum members were always very helpful, so I thought I would try posting the question here. If this posting is inappropriate I apologize.

 

Having come to web design and programming later in life, some of the basics caused me real problems. For example, when I develop sites I use XAMPP, per the QuickPro Guide. In the beginning I had trouble understanding how it worked but with help initial from forum members and by trial and error I can get by now. The recent trouble I have had is with relative directory addressing. In the past I used absolute addresses and had no problems, but for the latest project I am working on I wanted to use only relative addresses to make the site more flexible and so I could use parts of it for future projects.

 

Here's the problem. When in the root folder, when on a live Host can be referenced by: "/", but XAMPP seems to need "./", a "../" or a "/" both won't work. Also, when I include an includes file into another includes file (this was a work around for an additional requirement) when on XAMPP the relative path for the second includes was did not require changing folders. But when the site went live that wasn't the case. When on the live Host The second included file had to be relatively addressed as if it was being included from the file the first file was included in (sorry for the confusion here).

 

Can anyone give me a link, or otherwise explain if XAMPP is different from live hosts? I have only used one hosting company so far and am wondering if their set up is unique and possibly not standard.

 

Sorry for the long winded post, and thanks for any help in advance.

 

Tony

Link to comment
Share on other sites

I do not fully follow this part

Here's the problem. When in the root folder, when on a live Host can be referenced by: "/", but XAMPP seems to need "./", a "../" or a "/" both won't work. Also, when I include an includes file into another includes file (this was a work around for an additional requirement) when on XAMPP the relative path for the second includes was did not require changing folders. But when the site went live that wasn't the case. When on the live Host The second included file had to be relatively addressed as if it was being included from the file the first file was included in (sorry for the confusion here).

 

But I have had similar problems with xampp. It wouldn't include a file using:

<?php
include("includes/file.php");
?>

 

But it would if i used:

<?php
include("./includes/file.php");
?>

 

However I think that may of been because I was including a file within a file and it was in a sub directory. I had a look at the include path. It may be of some use to you. I'm not 100%.

 

I've actually just changed my approah from totally relative paths to absolute as long as you keep a CONSTANT like DEFINE ('BASE_URL', 'http://www.example.com/'); then you can just echo this out in your pages + filename.php.

 

Then if you want to use your files for the basis of a site just change the CONSTANT.

 

I'm sorry I can't be more help. But no your not the only one, I had a similar problem and when I put the site up I had to change all the paths of images etc because the server wasn't liking how they were structured.

 

Let me know how you get on

  • Upvote 1
Link to comment
Share on other sites

Tony, your confusion is well known. One of the most common issues we see on this forum in concerned with include paths, and rightly so, I think. It is rather confusing at times.

 

With that said, (as far as I know) there are not any differences in how XAMPP handles paths from that of a live web host. Obviously, with XAMPP, localhost is the htdocs directory by default, so you have to base everything off of that, whereas a live host would most likely use something like public_html as your web root.

 

I think the best solution is to do something like Jonathon suggested; create constants in your config file, and base everything off of that.

 

Lastly, here's a link that might be helpful:

 

http://php.net/manual/en/function.include.php

 

Edit: One more link that may be of use:

 

MS explanation

 

As a note to Larry, as you will surely agree, a lot of people have issues with absolute and relative paths, and ever more when going from, for example, XAMPP to a live site. You really might want to consider talking about this more in-depth in the next version of your book.

  • Upvote 1
Link to comment
Share on other sites

Jonathon thank you for the idea, I'll try that and HartleySan thanks for the links, I haven't checked them out yet but I will later today.

 

Thank you both for your replies.

 

For what it's worth I feel better knowing it isn't just me and that there are some differences between live hosting and XAMPP.

 

Jonathan, just as you showed I to had to use a "./" to go one level back and get into the includes files when using XAMPP when I though I would have had to use:

 

<?php
include("../includes/file.php");
?>

 

 

Do either of you know of any other cases where you had to use

 

<?php
include("./folder/file.php");
?>

 

to relatively address a file anywhere else other than in XAMPP?

 

 

The part of my question that was a little confusing was that I have a file in the includes folder (header.php) that contains

include("./includes/snippet.php)

 

when I built the site using XAMPP I had to include the snippet.php file relative to the header location, like I showed it above. But when I tried it on a live host that didn't work and I had to include the snippet file relative to the file that the header was being included into:

 

include("../../includes/snippet.php)

 

I don't know if many people put an include("includes/something") into another includes file. I did it as a work around to add a block of code to an existing header without having to work in the header and possibly ruin it.

 

One last question, when you go live with a project you have been working on, how do you handle changing the paths from the XAMPP version to the live host version? Do you have both versions in the code and have the code select which paths to use based on something like server information, or do you just hard code the changes to work on a host when you are ready to go live?

 

Thanks in advance for all the help.

 

Tony

Link to comment
Share on other sites

Yeah Tony, I had the same problem. I had to go through and manually add ../ to everything. I thought it was because I was using a mod_rewrite to alter my url structure. Which i think in part it was, but often it was just XAMPP.

 

I've actually just got a new system and XAMPP. I haven't however checked that xampp is calling files any differently to a liver server and it's my own fault, because I wrote in big fat letters on a list of things to do.

 

"configure XAMPP to work seamlessly" - I shall at some point today try and check that.

 

I normally just have a configuration file that has the settings for a live and local server so it knows what constants to use.

 

However my work around has been to use an absolute path. it's simpler to use in my opinion actually easier to alter.

 

I will try and find the answer to this for everyone using xampp

  • Upvote 1
Link to comment
Share on other sites

One last question, when you go live with a project you have been working on, how do you handle changing the paths from the XAMPP version to the live host version? Do you have both versions in the code and have the code select which paths to use based on something like server information…?

 

Yes. I base my configuration file on Larry's explanations in chapter 16 from PHP 6 and MySQL 5 for Dynamic Web Sites, with a "LIVE" constant based on server information and constants for the different base-URLs and base-URIs. That makes things very simple. You just have to think once about the different paths on your own computer and on the web server, and when you want to re-use files in another project, very few changes are needed for paths to work whatever the project.

Link to comment
Share on other sites

A bunch of responses...

 

Thanks to you guys for the thread and for helping each other out.

 

Thanks to Tony for the nice words on the book. And this is a fine place for such a question.

 

Jonathon, to be clear, you wouldn't want to use a BASE_URL constant to include files as that is a URL reference and you don't (generally) want to include files over HTTP. I use BASE_URI for an absolute path to files on the server.

 

HartleySan, yes, this seems to be a point of confusion, especially in the e-commerce book which requires absolute HTML paths because of mod_rewrite. And when people put one example in folder A and another example in folder B, both end up being in subfolders of the Web root directory. I'll try to cover this topic in more detail in the future.

 

I don't think there are differences between XAMPP and live hosting, or, at least, the actual differences are in the OSes involved and the directory structures and such. But XAMPP doesn't do anything that differently in this regard, as far as I know.

Link to comment
Share on other sites

Hi Tony and Larry,

 

Apologies I must be more clear, I have this horrible habit of rambling and not realizing that nobody bar me know what i'm actually on about :(

 

I have used the BASE_URL for links and images, css links in this kind of way.

a href="<?php echo BASE_URL; ?>a_page.php">A page</a>

is that wrong?

 

As for including files I just had a look over some recent work and I am not using absolute paths. Mainly because of the reason you said and I believe that the http wrapper is automatically disabled for XAMPP? So i didn't think it was possible

 

But I have in my includes recently had to alter the path in some examples so that it had the "./" first (but i think that was because it was an include within an include)

 

The main problem I had when i had a site that i tested on a live server was that i had to alter all the relative paths for CSS files, links, images, especially when using a mod re_rewrite that simulates a folder-esq structure like wordpress. Because they were just dynamic pages really. That's when i decided to use absolute where possible.

 

My answer i suppose is more about the paths for links + images as opposed to includes. So apologies Tony.

Link to comment
Share on other sites

Everyone, thank you very much, Larry I have just started getting into your PHP 5 Advanced: Visual QuickPro Guide, it's a little deep for me but it to is very clear. These discussion boards are very useful and everyone is very helpful. That's a huge help for someone like me who has come to web design and scripting a little later in life than I think most do.

 

I am still at a loss to understand what "./" does, and why XAMPP requires that relative path to get into the root of a site from one level out as opposed to the standard "../" that I normally have to use to go back one folder. I stumbled upon that by making a type as I was wracking my brain to understand why my relative addressing wasn't working. On a live host "./" just doesn't work for me. I'm not trying to "re-kill" the horse, so to speak, but I think if I can understand the logic behind XAMPP's requirement for "./" as opposed to the standard "../" will help me understand what is really going on.

 

Once again thank you for all the help.

 

Tony

Link to comment
Share on other sites

include("./file.php");

is the same as

include("file.php");

 

It means to work from the current directory.

 

../ 

 

is to go up one directory level as you rightly point out. I only assumed that I had to put './' in front of a particular include the other day because I was including a file from within an included file that was in another sub directory. However that is merely just an educated guess...

  • Upvote 1
Link to comment
Share on other sites

As a very minor point, there are rare cases in which ./ is different from specifying nothing (e.g., include("./file.php") vs. include("file.php")).

 

For example, if you load a resource from another server, then ./ will point to the current directory on the other server (where the resource is located), whereas nothing will point to the current directory of the local HTML/PHP file making the request for the resource.

 

I had this issue once with including a Flash SWF file where some of the resources used by the Flash file (ones that used ./) loaded fine, whereas other resources used by the Flash file (ones that used nothing) would not load, because it was trying to load them locally from my server. As a result, I had to separately download the resources that used nothing, and store those locally on my server in order to get them to load properly.

 

Anyway, hope that sheds some light on the situation. In all cases that I can think of in PHP with including files though, ./ vs. nothing is the same I think, although I always err on the side of safety by using ./ or an absolute path using a constant, which is what Larry generally seems to do.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...