PHP - Include Question
Just a quick question about including php files on a page...
Let's say I have my index page that is a fully functioning page with all the <html>, <body>, and <head> tags where they should be. Now I want to include another file, which also has <html>, <body>, and <head> tags. Would this cause any problems or is it ok to include the second file as is? Similar TutorialsHi I'm in a need of help here, I need to inluded this: "http://site.com/products.php?do=groups&groupid=2" in a a tpl file. Any way that this can be done? an example would be greatly appreciate it. cheers Hello person who reads this now I work a lot with <?php include("..."); ?> and I never thought beyond the html I put in there. In 1 of the include files I have the set-up of a simple vertical html/css menu and on the right next to it I have an image. However, not every page on my website needs the same image and I probably will change a lot of them frequently. Now I was wondering if (and how) I could include a piece of PHP code which does the following: If the web-url is "page1.php" show "image1.png" If the web-url is "page2.php" show "image2.png" If the web-url is "page3.php" show "image3.png" etc. This way I only have to change the "imageX.png" in 1 file. I hope you can help me. Best regards and thanks in advance! Ferdri3 Any help is much appreciated in advance... I have a master file located along the root path that needs to be included in subdomains. Inside the master file are includes like Code: [Select] <?php include("../bodyCopy/anyfile.php"); ?> that need to reference the /bodyCopy directory IN THE SUBDOMAIN and NOT along the root. The problem that I'm having is that the master file resides OUTSIDE of the subdomain on the root (in a '/bodyCopy' directory. How do I write the PHP include so that files included in a subdomain from outside of the subdomain (on root) search directories INSIDE the subdomain and NOT relative to the root path? Here's what I mean. I have this file sitting on the root: /home/xxxx/public_html/constructfiles/filetoinclude.php It needs to be included he /home/xxxx/public_html/SUBDOMAIN/publicfile.php Then, 'filetoinclude.php' needs to include other files that are he /home/xxxx/public_html/SUBDOMAIN/bodyCopy/otherfilestoinclude.php ...but, 'filetoinclude.php' is searching along the root path for '/home/xxxx/public_html/bodyCopy/otherfilestoinclude.php' (which does not exist), and NOT within the SUBDOMAIN for the '/home/xxxx/public_html/SUBDOMAIN/bodyCopy/otherfilestoinclude.php' like I need it to. It appears that PHP will only search along the root path of the included file and NOT the root path of the file in which an include is included. I'm confusing myself now, but I hope someone can help. Thanks, Jet - I'm a little confused with the use of absolute paths in PHP. I've always used relative paths but it's becoming an issue with some of the apps I'm working with and absolute would solve the problem. The thing is, I'm getting different results in different places and I'm a little confused. With regular HTML I seem to be able to use / as the root of my site. So if I do the following... Code: [Select] <img src="/images/name.jpg" /> That works fine no matter what page it's called from in the site and no matter what directory that page is in. I need to be able to do that same thing with PHP. Now, in my custom 404 page I have the following... Code: [Select] if($AddressURL == 'http://www.domain.com/blog/files/filename.zip') { header('HTTP/1.1 301 Moved Permanently'); header('Location: /download/filename.zip'); exit(); } That actually works just fine. If I go to the /blog/files/filename.zip (which doesn't exist) in a web browser it winds up downloading the file at www.domain.com/download/filename.zip as expected. Now where I get confused is that include files don't seem to work like that with PHP. If I do this... Code: [Select] require_once('/includes/config.php'); That winds up looking for an /includes folder all the way back at the local server root, not just the web root. so it's looking for /var/includes instead of /var/www/includes and of course that doesn't end up working. So, how come the include file paths work differently than regular HTML paths or header redirects in PHP? Is there any simple way I can always refer to the SITE root no matter what server it's running from? I've tried playing with $_SERVER['DOCUMENT_ROOT'] but it only seems to return the root of the current document, not the root of the whole site. So if I use it within a php file located in /var/www/includes/test.php it considers /includes the root which is not what I want. Any information would be greatly appreciated. Thanks! First off, I am new to PHP, and want to learn it, but it seems very complicated. Where do I begin? I found a script online for a contact form and I am having trouble making it work for my needs. I wanted to include a phone number field in the email message. This is what I have so far for the PHP, but it does not include the phone field in the body of the message. Everything else works fine. Apologize in advance if this message has been posted many times before. <?php $post = (!empty($_POST)) ? true : false; if($post) { include 'functions.php'; $name = stripslashes($_POST['contactname']); $email = trim($_POST['email']); $subject = "New Message from your website"; $message = stripslashes($_POST['message']); $phone = stripslashes($_POST['phone']); $error = ''; // Check name if(!$name) { $error .= 'Please enter your name.<br />'; } // Check email if(!$email) { $error .= 'Please enter an e-mail address.<br />'; } if($email && !ValidateEmail($email)) { $error .= 'Please enter a valid e-mail address.<br />'; } // Check message (length) if(!$message || strlen($message) < 15) { $error .= "Please enter your message. It should have at least 15 characters.<br />"; } if(!$error) { $mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { echo 'OK'; } } else { echo '<div class="notification_error">'.$error.'</div>'; } } ?> Hello, Is this a sound explanation of require and include.. Lets say i have "X" application with an index.php file. in a folder on my domain like so: www.xxx.com/Application X folder/index.php. // the index.php calls to multiple files with-in the "Application X folder" and database. What if i move the index.php to another location all together like so: http://www.xxx.com/notXsubfolder1/notXsubfolder2/notXsubfolder3/index.php //the X application index file.. and then edit the require and include links in the index.php file to paths like this (just example might not be accurate): require_once '../../../../application X Folder/include/required_file.php'; My question:?? What i want to know is: if the required_file.php has includes of its own, will it take the includes that it requires from its original folder where the Required_file.php is located? or will its paths change the the new index.php location?? . How can I use an include file that has a list of links in any directory at my website without breaking those links? You can see it in action here... The links in this directory work... https://www.billelgin.com/primary-directory/ The links in this directory (being created from the same include file) do not work... https://www.billelgin.com/primary-directory/subsection-one/ Obviously the problem is the include file is being called from different directories, so it's breaking the relative path links in the include file, but what can I do to fix it? You can download the example directory structure here... https://www.billelgin.com/EXAMPLE.zip I think this is a simple question, but I'm just starting out and can't figure out how to do this exactly. I'm making a demo site for a flower shop. The structure is as such (only listing the particular files in question - once this is done, the rest will be easy) index.php -> inc_show_colors.php -> showColors.php The index is to be standard through the site, loading include files into a main content area. Clicking on a link loads inc_by_color.php (index.php -> index.php?page=by_color ) From there, one of the four colors is selected. showColors.php contains switch statements for each color. I want to load showColors.php into the main content window when a color is selected, and I need to have it work for all four colors. I'm not sure how to do this, as you can't pass a variable to an include from what I understand. What's the easiest way to go about this? Keep in mind this is an intro class and "best practices" are not necessary yet, just the simplest solution to get it working. I want each of these colors to link to another include file, which will load in its place in index.php. index.php: Code: [Select] <div id="maincontent"> <?php if (isset($_GET['page'])) { switch ($_GET['page']) { case 'by_color': include('includes/inc_by_color.php'); break; case 'by_type': include('includes/inc_by_type.php'); break; case 'by_occasion': include('includes/inc_by_occasion.php'); break; case 'view_cart': include('includes/inc_view_cart.php'); break; case 'payment_options': include('includes/inc_payment_options.php'); break; case 'home_page': default: include('includes/inc_home.php'); break; } } else include('includes/inc_home.php'); ?> </div> inc_by_color.php: The first link I tried doing in the way I thought would work, but it doesn't. It can't find the include file. The second one works, kind of - but it just loads the page by itself, not into the maincontent div in index.php. Code: [Select] <h1>Flowers Categorized by Color</h1> <div class="picdivcontainer"> <div class="picdiv"><a href="index.php?page=red"><img src="images/image.png" width="90" height="90" alt="Red Flowers" /> <p>Red</a></p></div> <div class="picdiv"><a href="showColors.php?color=white"><img src="images/image.png" width="90" height="90" alt="White Flowers" /></a> <p><a href="showColors.php?color=white">White</a></p></div> </div> and showColors.php: Code: [Select] if (isset($_GET['color'])) { switch ($_GET['color']) { case 'red': $result = mysql_query("SELECT * FROM flowers WHERE color='red'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { // Set and trim strings and show the resulting flowers from query $flowername = $row['name']; $flowerimage = $row['image']; $image_path = "images/"; $flowername = str_replace('_', ' ', $flowername); $flowername = ucwords($flowername); ?> <div class="picdiv"><p><?php echo $flowername."<br />"; $flowerimage = str_replace(' ', '_', $flowerimage); echo "<img src='images/" . $row['image'] . "' />"; ?></p></div> <?php } break; I realize that showColors.php is probably a really messy way of doing this, but I just need to get it functioning ASAP for now. The PHP code sends me an email from the server, but the user input is empty or "blank". I only receive: "From: \ Email: \ Subject: \ Message: " and that's it. How do I fix my PHP and/or HTML code to receive user input from the form? Attached is my existing HTML and PHP code that doesn't send me any "user input" from the form. Thanks to anyone who can help!!
Hi Guys.. Merry Christmas Im kinda new to this fantastic world of PHP and i have a little problem i hope you can help me with. Im trying to build a website where I use include() to genereate my content. On my index.php i have a menu which includes content in a content div from external .php pages, my structure kinda goes like this.. (simplified) site/ index.php content/ fronpage.php products.php contact.php The HTML looks like this. <div id="menu"> <ul> <li><a href="index.php?page=frontpage">frontpage</a></li> <li><a href="index.php?page=products">frontpage</a></li> <li><a href="index.php?page=contact">frontpage</a></li> </ul> </div <div id="main"> <?php include('/content/'.$_GET['page'].'.php'); ?> </div> This all works very fine, but my problem is, can I have a include inside an already included page? I would like to have a menu on my products.php site, but that page is already included from above, and i would like the menu on the products.php site to stay as the content from the nested include changes with input from the /products folder. my idea was something like this. site/ index.php content/ frontpage.php products.php contact.php products/ product1.php product2.php the HTML on the index.php is the same as above and then i would add the include() on the products.php page, so its kinda the same thing, but one inside the other. HTML inside the products.php folder <div id="sub_menu"> <ul> <li><a href="#">frontpage</a></li> <li><a href="#">frontpage</a></li> <li><a href="#">frontpage</a></li> </ul> </div <div id="sub_main"> <?php include('/content/products'.$_GET['#'].'.php'); ?> </div> I dont know how to link to the new files so they will be included while the first include still stays on the page. Any of you know how and if this can be done? Or maybe at better way to do it? Hope this made sense, my first PHP question Thanks Hi Still a new comer when it comes to PHP. I have a situation where I want to use an include within an include and I am having trouble with my file paths. My main header include, includes everything for each page of my site, beyond the opening of the body to incorporate my navigation etc conditionally loading in css, and loading in titles and meta data etc depending on the page in question. This header needs to reference another include called the-pod.php which is required for every page, the only trouble is I want to use / to reference the root of the server and this is breaking my code. I can't use ../../ etc as its a different path depending on where the master file is located. So my question is how do I get around this? Can the root of the server or path to the root be stored in a variable? and if so how would I write this. Any tips / advice will be greatly appreciated. CanI put one INCLUDE statement INSIDE of another INCLUDE statement? When I put this above all else in my document and test it offline, all I see is code in the browser. But when I test it on my web server, I can view the markup and whatnot. Does anyone know why this is? Hi I have recently moved hosting providers; I have a php include on my website, which feeds latest posts from my forums to my homepage. The code I use is <?php include ('../mb/getposts/posts.php?id=80')?> Now, this code used to work on my old host, but no longer works on my new host, giving an error of Code: [Select] Warning: include(../mb/getposts/posts.php?id=80) [function.include]: failed to open stream: No such file or directory in /home/site/public_html/folder/page.php on line 41 Warning: include(../mb/getposts/posts.php?id=80) [function.include]: failed to open stream: No such file or directory in /home/site/public_html/folder/page.php on line 41 Warning: include() [function.include]: Failed opening '../mb/getposts/posts.php?id=80' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/site/public_html/folder/page.php on line 41 I can also include the script as <?php include ('../mb/getposts/posts.php')?> and when I do this the script works as it should do, but showing the default option. The ?id=xx bit allows me to choose what forum id I want the posts to be shown on my homepage as I have lots of individual homepages (long story) and to try and keep files small, I decided to wrap the latest posts php file into something where i could use one file for all the forums IDs that I need to show Does anyone know why this will not work? I am thinking it's a problem with the php config of my new server, and have asked my hosts, but they are telling me that this is out of their scope of support.. Any ideas would be appreciated Hi, I'm not really a php user however I'm having a dabble. I'm trying to display an include file such as <?php include("sponsor.php"); ?> However I'm trying to make it a bit more advanced by selecting the month so what I'm after doing is displaying <?php include("sponsor-october.php"); ?> The idea being that this updates by itself, I understand that <?php echo date("F"); ?> will display the month, it's just combining the two where I get lost, any help would be welcome ta. Hi, is there any way to clear the included file in php? for example, if i included a file like , include "file.php"; after using the functions in this file finally i want to exclude from the list. Dear All, I have just signed up to the forums so hello. I'm a complete novice to PHP and have a question. I've written a site using PHP but am stuck, how I have written is that certain pages use INCLUDE files that pull out information from .TXT files. The question I have is, is it possible to contain another INCLUDE link within the .TXT file? i.e. index.php includes companyname.txt but companyname.txt only has this line of text in it: <?php include("../includes/moreinformation.txt"); ?> I know it is quite a weird question/request but is there a way of doing this? So basically have an include within an include although the file extensions are .TXT. Thanks and looking forward to any help from you Pros. Kindest regards, James. James. Okay if I use an include to include a login script the login button doesn't work at all but it works on the page itself just not on the include http://store.crytell.com/ when I attempt to login nothing happens Im sure I have the script dir right Code: [Select] <body bgcolor="#100f14"> <? /** * Main.php * * This is an example of the main page of a website. Here * users will be able to login. However, like on most sites * the login form doesn't just have to be on the main page, * but re-appear on subsequent pages, depending on whether * the user has logged in or not. * * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC) * Last Updated: August 26, 2004 */ include("./storelogin/include/session.php"); ?> <html> <title>Jpmaster77's Login Script</title> <body> <table> <tr><td> <? /** * User has already logged in, so display relavent links, including * a link to the admin center if the user is an administrator. */ if($session->logged_in){ echo "<h1>Logged In</h1>"; echo "Welcome <b>$session->username</b>, you are logged in. <br><br>" ."[<a href=\"./storelogin/userinfo.php?user=$session->username\">My Account</a>] " ."[<a href=\"./storelogin/useredit.php\">Edit Account</a>] "; if($session->isAdmin()){ echo "[<a href=\"./storelogin/admin/admin.php\">Admin Center</a>] "; } echo "[<a href=\"./storelogin/process.php\">Logout</a>]"; } else{ ?> <h1>Login</h1> <? /** * User not logged in, display the login form. * If user has already tried to login, but errors were * found, display the total number of errors. * If errors occurred, they will be displayed. */ if($form->num_errors > 0){ echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>"; } ?> <form action="./storelogin/process.php" method="POST"> <table align="left" border="0" cellspacing="0" cellpadding="3"> <tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr> <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr> <tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>> <font size="2">Remember me next time <input type="hidden" name="sublogin" value="1"> <input type="submit" value="Login"></td></tr> <tr><td colspan="2" align="left"><br><font size="2">[<a href="./storelogin/forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr> <tr><td colspan="2" align="left"><br>Not registered? <a href="./storelogin/register.php">Sign-Up!</a></td></tr> </table> </form> <? } /** * Just a little page footer, tells how many registered members * there are, how many users currently logged in and viewing site, * and how many guests viewing site. Active users are displayed, * with link to their user information. */ echo "</td></tr><tr><td align=\"center\"><br><br>"; echo "<b>Member Total:</b> ".$database->getNumMembers()."<br>"; echo "There are $database->num_active_users registered members and "; echo "$database->num_active_guests guests viewing the site.<br><br>"; include("./storelogin/include/view_active.php"); ?> </td></tr> </table> </body> </html> Code: [Select] <?include_once "./storelogin/main.php"; // this will include a.php?> I am creating several files with the same connection information. I already REQUIRE_ONCE my connection data that has all the 'secret entry data', but am wondering, if this is my current code in the top of the files: $con = require_once('connection.php'); if (!$con) { die('Could not connect!' . mysql_error()); } mysql_select_db("$database") or die('Choose a database ' . mysql_error()); And then a $query/$result Can the lines BEFORE the $query go into an INCLUDE file ALSO if they are going to be constant and repeated, or do they have to be hardcoded into each file individually? I am fairly new to PHP. I tried searching for answers on Google and in this forum, but I found no solutions. Problem I used PHP include as a way of navigating through my website. I did this so if I changed the design of the website, I don't have to edit each page. The function is similar to iframes. Yesterday I developed a tip calculator (calculates how much tip you need to give to the waiter or waitress based on your bill). However I've noticed after I press "submit", it sends the visitor to a page that shows only the results. I want to show the results within my website design. Screen shots: I input the data. Notice the gray background and the navigation bar. After I press "Submit Order", it shows the results but the results aren't within my website's design. (Notice the white background). Question: How can I incorporate the results from the tip calculator in my design after pressing the "Submit Order" button? Here is the code on the main page of my website (HTML/CSS with PHP) P.S For links in the navigation section, I used : ?id=name Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>MV Book</title> <link href="style.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <div id="wrapper"> <div id="wrapper2"> <div id="header"></div> <!-- End of Header --> <div id="navi"> <ul> <li><a href="/">Home</a></li> <li><a href="?id=tip">Tip calculator</a></li> <li><a href="?id=orderform">Tip calculator</a></li> </ul> </div> <!-- End of Navi --> <div id="content"> <?php $id = $_GET['id'] ; switch($id) { default: include('cutenews/show_news.php'); break; case "orderform": include('orderform.html'); break; case "tip": include('tip.php'); } ?> </div> <!-- End of Content --> </div> <!-- End of Wrapper 2 --> </div> <!-- End of Wrapper --> </body> </html> And this is the code that is on the page where I input the code: Code: [Select] <form action="processtip.php" method="POST"> <table border="0"> <tr> <td>Bill Amount</td> <td align="center"><input type="text" name="billamount" size="10" maxlength="10" id="billamount"></td> </tr> <td>Tip Percent (%)</td> <td align="center"><input type="text" name="tippercent" size="10" maxlength="10" id="tippercent"></td> </tr> <tr> <td>Number Of People (Split)</td> <td align="center"><input type="text" name="people" size="10" maxlength="10" id="people" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Submit Order"></td> </tr> </table> </form> This is the PHP Code that process the input data and displays the results (processtip.php) : Code: [Select] <?PHP $billamount = $_POST['billamount']; $tipnumber = $_POST['tipnumber']; $people = $_POST['people']; ?> <html> <head> <title>Emaj's Tip Calculator</title> </head> <body> <h1>Emaj's Tip Calculator</h1> <h2>Thanks for using my Online Tip Calculator</h2> <?php if ($billamount < 0) { echo "<p> You can't have a negative number. Does the restuarant owe you money?"; exit; } else { if ($billamount == 0) { echo "What's the point of using a tip calculator if you didn't order anything?<br />"; exit; } if ($billamount > 0) { echo "<p> Your bill is $".$billamount; } } $tippercent = 0; $tippercent = $tipnumber/100; $tipamount = 0; $tipamount = $billamount * $tippercent; echo "<p> Total tip is $".$tipamount; $totalamount = 0; $totalamount = $billamount + $tipamount; echo "<p>Total bill (Tip included) is $".$totalamount; echo "<p>You are splitting the Total Bill with ".$people." people."; $indperson = 0; $indperson = $totalamount/$people; echo "<p>Each individual person pays $".$indperson; ?> If there is a thread that has a similar problem with a solution, can you please post a link to it? Thank you and I apologize in advance if there's a duplicate thread. -Emaj |