包括显示的文件或包含的文件夹

When using php do you set your path in reference to the files directory or from the page it is displayed from.

For example:

index.php is the home page of course

Directory structure.

index.php

includes > footer.php, header.php

product > product.php [blueproduct] > blueproduct.php

storescripts > connect_to_mysql.php, more.php

=================================================

Inside of footer I have a script that is not working on every page. Its a newsletter script to collect info. This is the code I'm using within my included footer.php:

</div>
<div class="footer">
<div class="wideNewsletter">
<div class="wrapNewsletter">
<div class="newsletterIntro"><b>NEWSLETTER SIGN UP</b></div>
<div class="newsletterForm">
<?php
$name = "";
$email = "";
$msg_to_user = "";
if ($_POST['name'] != "") {

    include "../storescripts/connect_to_mysql.php"; 

    // Be sure to filter this data to deter SQL injection, filter before querying database
    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['email']);


    $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
    $numRows = mysql_num_rows($sql);



    if (!$email) {

        $msg_to_user = '<div class="warning"><ul><li>Please type an email address ' . $name . '.</li></ul></div><br /><br />';

    } else if ($numRows > 0) {

        $msg_to_user = '<div class="warning"><ul><li>' . $email . ' is already in the system.</li></ul></div><br /><br />';

    } else {

        $sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime) 
                                                    VALUES('$name','$email',now() )")  or die (mysql_error());

        $msg_to_user = '<div class="success"><ul><li>Thanks ' . $name . ', hope you find what you want!</li></ul></div><br /><br />';
        $name = "";
        $email = "";
    }
    $message = 'Name: ' . $_POST['name'] . ' Email: ' . $_POST['email']; 
mail('newproducts@moniquetrinidadjewelry.com', 'New Newsletter Sign Up at Monique Trinidad Jewelry',  $message);
}
?>
<form style="width:430px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset style="text-align:left;padding:0px;border:0px;"> 
Name:
<input name="name" type="text" maxlength="36" value="<?php echo $name; ?>" />
Email:
<input name="email" type="text" maxlength="36" value="<?php echo $email; ?>" />
<input type="image" src="https://www.moniquetrinidadjewelry.com/images/new-images/green-bullet.png" border="0" name="mySubmitBtn" type="submit" value="Submit">

</fieldset> 
</form></div>
<div style="position:absolute;top:120px;"><?php echo $msg_to_user; ?></div>
<div class="newsletterExplain">Receive product updates. Remember only one of each!</div>
</div>
</div><!--wide newletter end-->
<div class="wrapFooter">


<div class="tearOneFooter">
    <div class="footerColumnList">
        <div class="footerTitles">Connect With Us</div>
        <div class="footerLists">
            <ul>
                <li><a href="https://www.moniquetrinidadjewelry.com/contact/contact-pop.php">Connect With Monique!</a></li>
            </ul>
        </div>

    </div>
<!--Seperate Connect With us Column from Information Column-->
    <div class="footerColumnList">
        <div class="footerTitles">Information</div>
        <div class="footerLists">
            <ul>
                <li><a href="https://www.moniquetrinidadjewelry.com/about-us.php">About Us</a></li>
                <li><a href="https://www.moniquetrinidadjewelry.com/packaging.php">Packaging</a></li>
                <li><a href="https://www.moniquetrinidadjewelry.com/terms-and-conditions.php">Terms & Conditions</a></li>
            </ul>
        </div>
    </div>
<!--Seperate Information Column from Shipping and Returns Column-->
    <div class="footerColumnList">
        <div class="footerTitles">Shipping and Returns</div>
        <div class="footerLists">
            <ul>
                <li><a href="https://www.moniquetrinidadjewelry.com/return-policy.php">Orders and Returns<a/></li>
                <li><a href="https://www.moniquetrinidadjewelry.com/secure-shopping.php">Secure Shopping</a></li>
                <li></li>
            </ul>
        </div>
    </div>
<!--Seperate Shipping and Returns Column from Services & Support Column-->
    <div class="footerColumnList">
        <div class="footerTitles">Hours Of Operation</div>
        <div class="footerLists">
            <ul>
                <li>We are a 24/7 <br />Online Establishment!<br />(US Based)</li>
            </ul>
        </div>
    </div>
<!--Seperate Connect With us Column from Information Column-->
</div>
<!--Beging SecondTearFooterArea-->
<div class="tearTwoFooter">

    <!--<div class="signUpNewsLetter"><img src="https://www.moniquetrinidadjewelry.com/images/news_letter_temp_IMG.png" alt="newsletter" /></div>-->
    <div class="paymentOptions"><img src="https://www.moniquetrinidadjewelry.com/images/payment_options_temp.png" alt="payment options" /></div>
    <div class="twitter"><a href="https://twitter.com/MoniqueJewelry"><img src="https://www.moniquetrinidadjewelry.com/images/twitter_temp.png" alt="twitterLink" /></a></div>


</div>
</div>
</div>
</div>

This is working within the [blueproduct] directory from the product directory, but not the index.php.

I have another issue as well, but I believe better practice would be to open another question after I've done research on the issue correct? If not let me know and I'll edit this original message.

================================================================================

Edits and Additions Below

I have everything configured and I believe I can elminiate this as the issue. I'm really stumped on this one. The link to give you a better idea of what I mean is http://www.moniquetrinidadjewerly.com . If you go there and try the form it doesn't process, but that same form if you select 'necklace' within the navigation you can see works fine and runs correctly. Here is the updated footer.php file below to include changes for abs path.

</div>
<div class="footer">
<div class="wideNewsletter">
<div class="wrapNewsletter">
<div class="newsletterIntro"><b>NEWSLETTER SIGN UP</b></div>
<div class="newsletterForm">
<?php
$name = "";
$email = "";
$msg_to_user = "";
if ($_POST['name'] != "") {

    include_once(DOC-ROOT."/storescripts/connect_to_mysql.php");

    // Be sure to filter this data to deter SQL injection, filter before querying database
    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['email']);


    $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
    $numRows = mysql_num_rows($sql);



    if (!$email) {

        $msg_to_user = '<div class="warning"><ul><li>Please type an email address ' . $name . '.</li></ul></div><br /><br />';

    } else if ($numRows > 0) {

        $msg_to_user = '<div class="warning"><ul><li>' . $email . ' is already in the system.</li></ul></div><br /><br />';

    } else {

        $sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime) 
                                                    VALUES('$name','$email',now() )")  or die (mysql_error());

        $msg_to_user = '<div class="success"><ul><li>Thanks ' . $name . ', hope you find what you want!</li></ul></div><br /><br />';
        $name = "";
        $email = "";
    }
    $message = 'Name: ' . $_POST['name'] . ' Email: ' . $_POST['email']; 
mail('newproducts@moniquetrinidadjewelry.com', 'New Newsletter Sign Up at Monique Trinidad Jewelry',  $message);
}
?>
<form style="width:430px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset style="text-align:left;padding:0px;border:0px;"> 
Name:
<input name="name" type="text" maxlength="36" value="<?php echo $name; ?>" />
Email:
<input name="email" type="text" maxlength="36" value="<?php echo $email; ?>" />
<input type="image" src="https://www.moniquetrinidadjewelry.com/images/new-images/green-bullet.png" border="0" name="mySubmitBtn" type="submit" value="Submit">

</fieldset> 
</form></div>
<div style="position:absolute;top:120px;"><?php echo $msg_to_user; ?></div>
<div class="newsletterExplain">Receive product updates. Remember only one of each!</div>
</div>
</div><!--wide newletter end-->
<div class="wrapFooter">


<div class="tearOneFooter">
    <div class="footerColumnList">
        <div class="footerTitles">Connect With Us</div>
        <div class="footerLists">
            <ul>
                <li><a href="https://www.moniquetrinidadjewelry.com/contact/contact-pop.php">Connect With Monique!</a></li>
            </ul>
        </div>

    </div>
<!--Seperate Connect With us Column from Information Column-->
    <div class="footerColumnList">
        <div class="footerTitles">Information</div>
        <div class="footerLists">
            <ul>
                <li><a href="https://www.moniquetrinidadjewelry.com/about-us.php">About Us</a></li>
                <li><a href="https://www.moniquetrinidadjewelry.com/packaging.php">Packaging</a></li>
                <li><a href="https://www.moniquetrinidadjewelry.com/terms-and-conditions.php">Terms & Conditions</a></li>
            </ul>
        </div>
    </div>
<!--Seperate Information Column from Shipping and Returns Column-->
    <div class="footerColumnList">
        <div class="footerTitles">Shipping and Returns</div>
        <div class="footerLists">
            <ul>
                <li><a href="https://www.moniquetrinidadjewelry.com/return-policy.php">Orders and Returns<a/></li>
                <li><a href="https://www.moniquetrinidadjewelry.com/secure-shopping.php">Secure Shopping</a></li>
                <li></li>
            </ul>
        </div>
    </div>
<!--Seperate Shipping and Returns Column from Services & Support Column-->
    <div class="footerColumnList">
        <div class="footerTitles">Hours Of Operation</div>
        <div class="footerLists">
            <ul>
                <li>We are a 24/7 <br />Online Establishment!<br />(US Based)</li>
            </ul>
        </div>
    </div>
<!--Seperate Connect With us Column from Information Column-->
</div>
<!--Beging SecondTearFooterArea-->
<div class="tearTwoFooter">

    <!--<div class="signUpNewsLetter"><img src="https://www.moniquetrinidadjewelry.com/images/news_letter_temp_IMG.png" alt="newsletter" /></div>-->
    <div class="paymentOptions"><img src="https://www.moniquetrinidadjewelry.com/images/payment_options_temp.png" alt="payment options" /></div>
    <div class="twitter"><a href="https://twitter.com/MoniqueJewelry"><img src="https://www.moniquetrinidadjewelry.com/images/twitter_temp.png" alt="twitterLink" /></a></div>


</div>
</div>
</div>
</div>

The config.inc.php file is located within the main directory and it reads :

<?php
define("Monique trinidad Jewelry","My Website");
define("DOC_ROOT","/home3/onlinfr7/public_html");
define("URL","https://www.moniquetrinidadjewelry.com");
?>

I'm not sure where the issue is occurring or what exactly is happening with the homepage(index.php) newsletter form in the footer. Why it works in one page, but not the other. It seems that path may not be the issue as I first thought. Any advice?

there's the current working directory, which you can get with getcwd(). THAT'S the path that everything will be relative to in any file operations you perform. By default it will be the directory that your main script is executed from.

Whether you go relative to that, or relative to something else, or just absolute on everything is up to you. There's no right/wrong answer - just whatever's easiest for YOU to maintain.

What I like to do that makes things easier when including other files is create a config file and include that in the main file or header like index.php

So this might be my config file called config.inc.php

<?php
define("SITENAME","My Website");
define("DOC_ROOT","/home/username/webroot");
define("URL","http://www.example.com");
?>

I include this config file in my index.php like include("/home/username/webroot/config.inc.php");

Then I can use DOC_ROOT whenever I want to include another file somewhere and it will always have the full absolute path so that you know it's included.

e.g. include_once(DOC_ROOT."/storescripts/connect_to_mysql.php");