Hey peoples,
On all my pages, I have a common header, sidebar, and footer. And for these pages, i use the php include command in order to include them into the appropriate pages.
A while back i inquired as to the best syntax to make my includes portable; meaning, i wanted them to work on my local machine AND on the live site. After several posts on the subject and some help from some long-time posters here, I decided to go with the following syntax:
<?php include ($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php'); ?>
This worked great on my local machine, as it resolved to http://localhost/includes/header.php
Well, i recently uploaded my local site to the live site and that option may not work. Because the $SERVER['DOCUMENT_ROOT'] is referencing the document root of Apache, on the server, and i have no control over where this root is assigned (i'm on shared hosting).
As such, again, in an effort to make portable code, i decided to go with another syntax, which is almost pefectly portable:
<?php include 'http://' . $domain . '/includes/header.php'; ?>
where $domain is a variable that is defined in a global.php file that gives the db host, username, password, etc, and this file is included on all pages.
So on my local machine, I set $domain to be "localhost" and on the live site, I set it to be "www.mydomainname.com". Simple enough right? And yes, it does work BOTH on the local and the live site. And it should work, as the syntax is straightfoward.
BUT...
I problem creeped in...
Because my site includes a custom build shopping cart made from SESSIONS (this is the key here), in the aforementioned header.php file, i have a line that prints either (a) Your cart is empty, or (b) You have X items in your cart. Thus, on the top of every page, in the header, you see that line.
The code that is used for this is pasted below, and it now includes a bunch of debugging echo statements.
<?php
if (count($_SESSION['cart']) == 0) {
echo '<img border="0" src="/images/cart.gif" align="absbottom" width="17" height="12"></a>';
echo '<span class="content7i_blue"> ';
echo "(Your cart is empty.)";
echo count($_SESSION['cart']); //for debugging
// the following if/else statement is ONLY for debugging purposes
if (isset($_SESSION['cart'])) {
echo "it is set";
}
else
echo "not set";
}
elseif (count($_SESSION['cart']) == 1){
echo '<img border="0" src="/images/cart_f.gif" align="absbottom" width="17" height="12"></a>';
echo '<span class="content7i_blue"> ';
echo "(You have 1 item in your cart.)";
}
else {
echo '<img border="0" src="/images/cart_f.gif" align="absbottom" width="17" height="12"></a>';
echo '<span class="content7i_blue"> ';
echo "(You have ";
echo count($_SESSION['cart']);
echo " items in your cart.)";
}
?>
The problem is that even when i add stuff to the cart, the line in the header still prints out "Your cart is empty." Further, I print out how many items are in the cart by using the following line: echo count($_SESSION['cart']). When i print this from the header, it gives 0, which is wrong.
So you may figure, okay, well, perhaps the cart is indeed empty. Nope. When i click on the mycart icon (takes me to mycart.php), I display the contents of the cart. Sure enough, if there are three items, ALL THREE items show. If there are five items, five items show, etc, etc.
And just for the heck of it, i put the same line of code from above, echo count($_SESSION['cart']); , directly above where the cart contents are displayed. And it DOES show the correct amount of items in the cart.
That is so crazy, let me repeat: if i put the SAME EXACT LINE OF CODE:
echo count($_SESSION['cart']);
directly above the tabular display of the cart contents, it DOES PRINT OUT THE CORRECT NUMBER OF ITEMS!
So I'm viewing one page, and the same line of code, echo count($_SESSION['cart']) , gives two different results. One line is used in the header.php file, which is included in mycart.php, and this line of code produces 0 as the result, and then when i use that same line later in the ACTUAL page, mycart.php, i get the real result.
Now, for those of you few still reading to this point, here's the kicker: when you access the header.php page DIRECTLY, meaning you go view that actual page (http://www.mydomain.com/includes/header.php), it now displays the CORRECT RESULT!
SO, something funky is going on when the page is included using the syntax I gave:
<?php include 'http://' . $domain . '/includes/header.php'; ?>
Apparently, based on the debugging statements i echoed on the page, the $_SESSION variables are not carrying through to the included portion of the code.
But when i change the syntax back to the first one shown:
<?php include ($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php'); ?>
it works fine with NO PROBLEMS. But as mentioned, this only works on my local machine, and I have to use the second syntax shown at the beginning. But the second one SHOULD WORK...right?
So what is going on?
Any ideas would be very much appreciated.
Thanks.
Jonathan