Refer to Index

In this code…


<ul id="topMenu">
	<li><a href="index.php">Home</a></li>

…is there a way to replace “index.php” with a shorthand so the link always ends up at w w w.MyWebSite.com/index.php (i.e. Web Root)??

Debbie

An absolute link would do that though since it hasn’t been specified where all your links are in relation to the root. Unless of course ALL the pages are at root level in which case you could use forward slashes.

How do I get the same effect in my IDE?

I have a folder called 01_MyWebsite where all of my files sit locally. I suppose it is like the web root, but it must not be, because if I do this…


<ul id="topMenu">
		<li><a href="/">Home</a></li>

…then I get…

[b]http://localhost/[/b] in the Address Bar with a listing of all my files?!

(I believe that would work on my web host though.)

And, yes, all files are in root for now.

Follow me?

Debbie

<base href=“http://www.mywebsite.com/”>

But there are a few pitfalls to beware of - have a look at href (HTML attribute) for more info.

You’re missing what I am saying…

On my web host, root is root.

On my laptop in NetBeans, root is “00_MyWebsite”.

This doesn’t matter, except when I need to refer to paths, it messes things up.

Debbie

Ah, I see.

In that case, your choices are largely limited to either using relative links only, using local links and then doing a global search and replace before uploading, or setting up a development server and always work off that rather than locally off the laptop.

Well, relative links are the problem, because what they are relative to varies.

Here is my directory structure locally…


00_MyWebsite
	Source Files
		components
			body_header.inc.php
			body_footer.inc.php
	
		secure
			checkout.php

		images
			my_logo.png

		index.php
		product_details.php

Here is an example of where I’m going crazy…

“checkout.php” includes “body_header.inc.php”.

“body_header.inc.php” has this code…


<img class="logo" src="images/myLogo.png" width="220" alt="My Logo" />

The problem is that when “checkout.php” includes “body_header.inc.php”, it makes it like the included code is now where “checkout.php” is located (i.e. in “secure”) and so the link to the logo above no longer works!!! (And if I used Absolute Paths instead, it break when I go between my Laptop and Web Host.) :frowning:

However, I was just thinking… :lightbulb

[b]If I could define two Variables (or Constants) called:

  • Production_Root
  • Development_Root

then I could build all of my paths off of “root”, and I think that would fix my problem, right?![/b] (Then when I upload my code to the server, i’d just switch which “root” I needed to use!)

Does that make sense?

Would it work?

If so, how do I do that?! :scratch:

Thanks,

Debbie

Thats because the included code is included IN the calling script. In essence, it becomes part of that script and so it will behave like it is in the same directory because it has been included INTO it. That is how it is supposed to work.

Right, but that also means I cannot use relative paths like I was when everything was in Root.

When I am in “checkout.php” and use this code…

<?php	require_once('../components/body_header.inc.php');	?>

…my Header appears.

But when I try to require the header file using my new DEV_ROOT constant, I can’t figure out what the constant and path should be to get it working?! :frowning: :frowning:

I’ve tried every combination, but clearly I don’t understand how paths work and where really are in NetBeans?!

Please help me!!

Debbie

Define a constant within the index.php file (entry point).


define('WWW',str_replace('//','/',dirname(__FILE__)));

I don’t follow you…

That just gives me the parent directory. Not the root.

The essence of my questions/problems starts in Post #7 above…

Debbie

As I mentioned on CF, you want to look at $_SERVER[‘DOCUMENT_ROOT’] in your script and also $_SERVER[‘HTTP_HOST’] which will give you the hostname (eg localhost, mysite.com) which you can then use in a switch() to then make use of the doc_root.

Can’t beat NP++ :wink:

Except it isn’t that easy, because I still have issues with the relative links getting messed up in my incudes…

Can’t beat NP++ :wink:

Secret codes?

Debbie

Notepad++ :wink:

I never used netbeans, i know you’re fixed on it but i just use a basic wamp setup with notepad++ and don’t have any file path issues. My only gripe was the SQL databases being different (different logon) so i had to create SQL profiles in my code to deal with that.

What i’ve said above was what you asked for - a way to operate code differently on each system. You check the $_SERVER[‘HTTP_HOST’] in a switch to see if it is ‘localhost’ or ‘mydomain.com’ and act accordingly. This will basically allow you to automatically switch which root constant you use - as you were saying above.

I understood that part.

Guess I have too many questions going on.

My bigger issue is how including the same file - which has a relative reference in it - by several files all in different locations blows up the relative paths.

Debbie

If checkout.php is inside the secure directory then images/logo.jpg relatively becomes secure/images/logo.jpg. It’s basically the way the browser will interpret the relative link in your html. Because the browser has browsed into the secure directory it assumes that anything in the html relatively linked, is also relative to the secure directory not the root.

Same for everything else in subdirectories which uses the header file. Your index.php and product_details.php files will be the only ones which will correctly link to the image.

This is why i always use my template system and then work out all links, content etc dynamically and then merge it into a template because the method you are using which is static html will not work in your current scenario. Your only option is to put in a php variable in that header and set its value in the calling script.

Your best bet would be to use something like this:


<img class="logo" src="<?=$Path ?>images/myLogo.png" width="220" alt="My Logo" />

In each script you would then need to set the value of $Path - EG ‘’ (blank - for index.php), ‘secure/’, ‘images/’ etc.

Yep, a tried and tested (by many of the popular frameworks) solution - good call.

Just create something like the following, and make sure it’s included at the start of your application.


<?php
define('ENVIRONMENT', 'development');

define(
  'ROOT',
  ENVIRONMENT === 'development' ? '/local/file/path/www/' : '/var/www/vhosts/example.org/www/'
);

define(
  'WEB_ROOT',
  ENVIRONMENT === 'development' ? 'http://localhost/example.org/' : 'http://example.org/'
);