Jump to content

Questions about Constants


doubledee

Recommended Posts

;)  Most of us experience these types of things at one time or another.  I've said this many times over the years, but I don't mind admitting that despite having been a professional programmer at numerous software companies, and having worked on the development of hundreds of websites used by millions of people I still learn something new about web development on weekly if not daily basis.  For me it's part of the fun that it continues to offer a challenge, but people regularly underestimate how complicated it is thanks to books that claim it's "easy".

 

I never thought it was easy.  I just never knew it could be so hard.  (And doing this with a broken arm doesn't help my patience or comfort.)

 

 

If you go back in the thread and look at my earlier reply, I did point out that the includes deal with files and are relative to the filesystem of the server, while url's inside html are relative to the webroot.  Maybe that is not as clear as I think it is, but getting clarity on that will save you a lot of banging your head against the wall.

 

I got that, but don't think you follow m confusion...

 

index.php includes components/header.php which references "images/logo.jpeg".

 

All is logical.

 

Now...

 

secure/checkout.php includes components/header.php which references "images/logo.jpeg".

 

But in the infinite wisdom of PHP's architects, now header.php conceptually move from "components" to "secure" and so when header.php references "images/logo.jpeg" it doesn't work.

 

That is NOT intuitive (even if I get it now)!!!

 

That's what cost me 30 hours of my life...

 

 

The webroot is webserver magic.  I often call this "webspace".  If something is in webspace you can type in a url  like "http://www.mysite.com/something" and it will return that to you, whether it be an image, .css, .js, .html or .php.  Deconstructing that, the webroot is "/".

 

I got that, but it gets tricker in NetBeans as to what the true "web root" is.  That took me a day in itself.

 

 

So when you're dealing with things that are sent in html, it's always relative to the webroot. It doesn't matter that the real path on the server (or your workstation with WAMP) is /user/me/documents/00_site/  because the webserver takes care of that, so inside your html the only thing that is ever important is where that exists BELOW the webroot.

 

Right.

 

 

Since php includes are files from the filesystem, those functions, along with any of the file reading or writing functions, ALWAYS are relative to the filesystem.  They don't know or care about the webroot or webspace.  They need to know where is that file inside the filesystem on this machine that is running apache/php.

 

Right.

 

 

If you can keep those two separate systems clear in your mind, you'll be breezing through includes and urls.

 

I'm trying.

 

 

 

Debbie

 

 

Link to comment
Share on other sites

Here is my "config.inc.php" file...

<?php
define('ENVIRONMENT', 'development');			// Change to 'production' when ready.

define('ROOT', ENVIRONMENT === 'development'
				? '/Users/user1/Documents/DEV/++htdocs/00_MyWebsite/'
				: '/var/www/vhosts/MyWebsite.com/httpdocs/');

define('WEB_ROOT', ENVIRONMENT === 'development'
				? 'http://localhost/00_MyWebsite/'
				: 'http://www.MyWebsite.com/');
?>

 

Comments??

 

 

So I changed all of my includes to look like this in "checkout.php"...

 

<!-- Include BODY HEADER -->
<?php	require_once(ROOT . 'components/body_header.inc.php');	?>
<?php	//require_once('../components/body_header.inc.php');	?>

 

 

Should I do this in a files that are in the "Web Root" (e.g. "index.php") as well?  (Technically I don't need my constant there, but maybe there is some value to doing that...)  :confused:

 

Comments?

 

 

To fix my Logo, I did this in "body_header.inc.php"...

 

<div id="header">
<a href="<?php echo WEB_ROOT ?>index.php">
	<img class="logo" src="<?php echo WEB_ROOT ?>images/my_logo.jpeg" width="220"	 alt="My Logo" />

 

Does that look okay??

 

Comments??

 

 

And lastly, to fix my Tab Links, I did this in "body_header.inc.php"...

<ul id="topMenu">
<li class="current"><a href="<?php echo WEB_ROOT ?>index.php">Home</a></li>
<li><a href="<?php echo WEB_ROOT ?>products.php">Products</a></li>
and so on...

 

Thanks,

 

 

 

Debbie

 

 

 

Link to comment
Share on other sites

In terms of the webroot, i personally wouldn't use that nor set it, because it's easier to specify the webroot path.  The problem you had with the image is that:

 

images/logo.jpeg

 

Is a relative url, because you did not specify the leading '/' for the webroot. Change that to:

 

/images/logo.jpeg

 

And you've made it an absolute path.  The webserver will automatically append on the hostname so you can omit the use of the WEBROOT constant if you'd prefer to apply KISS. 

Link to comment
Share on other sites

In terms of the webroot, i personally wouldn't use that nor set it, because it's easier to specify the webroot path.

 

Huh?

 

My two constants ARE paths.

 

 

The problem you had with the image is that:

 

images/logo.jpeg

 

Is a relative url, because you did not specify the leading '/' for the webroot. Change that to:

 

/images/logo.jpeg

 

As I explained several times, that doesn't work.  And that is why I've wasted all weekend on this...  Things that even I would try didn't work?!

 

 

 

And you've made it an absolute path.  The webserver will automatically append on the hostname so you can omit the use of the WEBROOT constant if you'd prefer to apply KISS.

 

We're not following each other.

 

What I have now is the only thing that's worked.

 

And your suggestion of simply adding "/" didn't work on Friday.

 

 

 

Debbie

 

 

Link to comment
Share on other sites

It does work.  The problem is that you are not sure what your webroot is. On your development machine, which is where you are doing your development your webroot is / but you are running all your scripts from /00_MyWebsite/.  You can either set the webroot to reflect this (although I would not) or you can adjust your httpd.conf for apache so that the webroot reflects that you're running from /00_MyWebsite/. 

 

 

Link to comment
Share on other sites

It does work.  The problem is that you are not sure what your webroot is. On your development machine, which is where you are doing your development your webroot is / but you are running all your scripts from /00_MyWebsite/.  You can either set the webroot to reflect this (although I would not) or you can adjust your httpd.conf for apache so that the webroot reflects that you're running from /00_MyWebsite/.

 

It's all blurry after the last few days, but okay.

 

So, based on this and an earlier comment you made, should I change my Config file from this...

 

define('WEB_ROOT', ENVIRONMENT === 'development'
				? 'http://localhost/00_MyWebsite/'
				: 'http://www.MyWebsite.com/');

 

 

To this...

 

define('WEB_ROOT', ENVIRONMENT === 'development'
				? '/00_MyWebsite/'
				: '/');

 

Thanks,

 

 

Debbie

 

 

 

Link to comment
Share on other sites

I think gizmola is referring to the configuration of Apache. Apache is an http server which MAMP installs (MAMP stands for Mac, Apache, MySQL and PHP).

 

He was suggesting changing Apaches default configuration of DOCUMENT_ROOT from Users/user1/Documents/DEV/++htdocs to Users/user1/Documents/DEV/++htdocs/00_MYyWebsite

That way your WEB_ROOT paths will be the same on your Mac and website. Therefore there will be no need for the constant WEB_ROOT

 

Another way would be to setup your project as a virtualhost, so rather than going to ttp://localhost/00_MyWebsite/ you'll instead have a url say http://localhost.mywebsite/ or http://local.mysite.com/ or whatever url you'll want to use. This is the approach I'd take when dealing with multiple projects.

 

Sorry, If I have just confused you again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.