PHP - Can This Go In An Include
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? Similar TutorialsHi 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 I have a script that is going to be on several urls. There is a master site that will have certain information that will need to be present on all urls that host this script. I am planning on using RSS to do this. What is a good method to dynamically create the xml file for the master site so that the feed can be broadcast to the other urls? Thanks in advance for the input. 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. 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, I'm so new to PHP. Kindly help me figure out on how to use include function.. situation is.. I am going to include a head.php file /includes/head.php this is the path where i want to include head.php /menu/menulist/list in "list" folder that is where I want to include head.php include works on folder "menulist" but not on 3rd folder "list" any advise on how tot solve this problem of mine? Thanks 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 have to store my Authorize.net Payment Gateway values somewhere safe. (i.e. Outside of the Web Root) I tried this code but my checkout page that formerly worked with local values in Test Mode is crashing. // Include Authorize.net Values. require_once('/var/www/vhosts/MyWebsite.com/private/auth_config.php'); I think the issue is that private maybe can't be read or included by other files? Would using another Linux directory solve my problem? Debbie 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 Hello, I created a script at my server, but would like to upload files to my other server. The website runs on server 1 and the upload form on server 2. I want to include the file of server 2 at the page at server 1; I've tried: include("http://ipaddress/upload.php"); Sadly, this doesn't work. I get the error: Quote URL file-access is disabled in the server configuration in Is there another way to do it? Or should I change the php.ini and allow this (why isn't it allowed by default? What are the risks?). iFrames aren't what I need here. Thanks, NLCJ I have an include file and a php file in different directories and I can't figure out what I'm doing wrong trying to get them to relate to one another. webhost/website/includes/include.inc webhost/website/modules/module/page.php How do I get the page.php to use the include.inc file? I'd think it would be "../../include.inc", but that isn't working 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. Hello. Include is identical to require, only require stops any further script execution. Just wondering if anyone could give me a good example, where 'require' should be used instead 'include'. Thanks. Hi Ive only just started using PHP so go easy I have a PHP include for a menu on a site I run. <body> <html> <ul class="avmenu"> <li><a class="current" href="index.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="services.php">Services</a></li> <li><a href="projects.php">Projects</a> <ul> <li><a href="highcroft_house.php">Highcroft House</a></li> <li><a href="barn_conversion.php">Barn Conversion</a></li> <li><a href="sunroom_extension.php">Sunroom Extension</a></li> <li><a href="dance_school.php">Dance School</a></li> </ul> </li> <li><a href="testimonials.php">Testimonials</a></li> <li><a href="offers.php">Special Offers</a></li> <li><a href="links.php">Links</a></li> <li><a href="contact.php">Contact</a></li> </ul> As you can see, on the first link, the index link, it has the class="current" property which displays a little tab next to the menu to show what link is currently selected. With using the include, obviously the class="current" is passed on to every page. Is there a way that I can tell the include file to change according to what page its on? Any help appreciated! Thanks Tom I have a php file with a ton of vars that i need to access in a number of controllers. How can i simply include this file into a controller in cakephp? ~John Hi! I would appreciate your help... How can i include files with get and post data in order to avoid sql injections? The including data has utf8 encoding... Any suggestions??? Thanks in advanced!!! |