PHP - Include/require Problem
Hello,
I have a small and annoying problem. Imagine that I have a main folder on my FTP server which has index.php and I then have a php folder for all of my small scripts and an includes folder for misc files. So, for instance http://www.somedomain.com/index.php http://www.somedomain.com/php/somescript.php http://www.somedomain.com/includes/somefile.html Inside somescript.php, I need to use one of the files from the includes folder, like this for example: require('../includes/somefile.html'); This is all fine, but the problem is when I include the PHP file on my index.php page. Then I am telling PHP to go one step back to find the includes folder, but the problem is that it only needs to go one step back if it PHP script is not included on some page outside of the php folder. If I run php/somescript.php, there is no problem, but if I include php/somescript.php on my index.php, then there is trouble! I hope you guys understand what I mean. Thanks in advance! PS - I would appreciate if the solution is not about moving php/somescript.php or using full URLs in my includes. Similar TutorialsWhy would I want to use include() vs. require() ?? (It seems like most people use "include()", but I would think "require()" would be better/safer...) TomTees 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. is there a way to do an include or require to a whole directory? I have only done includes and requires with single files in the past, but have a situation where this could save me a lot of time. I am very confused in between these three terms INCLUDE, REQUIRE, HEADER... Can anyone tell me the difference between these three terms... I just want to run a php file in the if condition.. Basically i do not want to open i just want to run ... for ex. Code: [Select] <?php if(condition) { // run mail.php file } ?> Php Folks, As you know, typing the same code over and over again on all files is daunting. I was wondering, if I can have an error_reporting.php file and then put:
include('error_reporting.php');
at the top of all my php files as header, where the error_reporting.php would have this content: <?php error_reporting(E_ALL); ini_set('error_reporting','E_ALL'); ini_set('display_errors','1'); ini_set('display_startup_errors','1'); ?> Q1. Is that ok or not ?
Q2. Usually, I have a conn.php with content like this: <?php $conn = mysqli_connect("localhost","root","","db_database"); $db_server = 'localhost'; $db_user = 'root'; $db_password = ''; $db_database = 'test'; $conn->set_charset('utf8mb4');//Always use Charset. if (!$conn) { //Error Message to show user in technical/development mode to see errors. die("Database Error : " . mysqli_error($conn)); //Error Message to show User in Layman's mode to see errors. die("Database error."); exit(); } ?>
And then, on all my php files, I just reference to the conn.php by putting the following line on the header: include('conn.php');
Or:
require('conn.php');
And on each php file, just before dealing with mysql, I have a line like this: mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT); $conn->set_charset("utf8mb4"); Now, I am wondering, why should I write the above 2 lines on all my php files that deal with mysql ? To keep things short, why don;t I just add those 2 lines in the error_reporting.php ? So, it looks like this:
error_reporting.php
<?php ini_set('error_reporting','E_ALL');//error_reporting(E_ALL); ini_set('display_errors','1'); ini_set('display_startup_errors','1'); mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT); $conn->set_charset("utf8mb4"); ?>
I am using php require to input a page of code. I am including (require) that file several times within the page. I am guessing that it executes the file every time, and does not save any page loading time, and in fact probably slowing it down because it has to make the request every time.
Just wanted an expert to confirm or deny my educated guess. Would it be the same if it were including an html file instead of a php file?
<?php require ("$base_path/main_page_rotation/content4.php"); ?> Hello all, i just joined today, because am going nuts with php and the include or require statement/function I want to use them to link to a header. Nothing hard about that , I would say. But my bit of code below just does not work. I am running them in a test area on my server. Just to be safe The file L2.php is in the same directory/folder ( showing my age).
I should say that am have been programming a while but only using php for about 4 weeks and this normal simple bit of code has me stumped. I am building the site in visual studio and atom. any pointer would be received with joy
The content of L" is also below . Danler
<?php include_once('L2.php'); ?> centents of l2.php below <h3> line 2 <br><H2>Hello world AM TRYING 6</h2> </h3>
I need to include a pagination file in multiple pages or create a custom function but not sure which one to use. For example: Code: [Select] <?php function test() { echo "Hello PHP Freaks!"; } ?> Or I can create a separate file that echos this statement and include it. When do you use include/require and when do you create your own custom function? 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?? . trying to use a require but not working properly Fatal error: require() [function.require]: Failed opening required 'get_extra.php?id=1' (include_path='.') first thing set on page is.. page is profile.php?id=1 $userid = $_GET['id']; code is:require "get_extra.php?id=$userid"; what can i do to fix this? thanks I get this error when trying to include header.php and sidebar.php Code: [Select] hParse error: syntax error, unexpected '/' in /home/ucherllo/public_html/index.php on line 26 Here are is index.php and includes code. INDEX.PHP Code: [Select] <html> <head> <title>Cher LLoyd - UltimateCherLloyd.net - Your newest fan site for everything Cher Lloyd. News, media, photos, and more. </title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta name="description" content="Newest fan site for everything Cher Lloyd" /> <meta name="keywords" content="cher lloyd, cher lloyd fansite, cher lloyd x-factor, fansite, fan site, swagger jagger, playa boi, dub on the track, 2010, 2011, cher lloyd photos, cher lloyd news, cher lloyd media, cher lloyd fan site" /> <meta name="author" content="D" <meta name="robots" content="index, follow" /> <!-- PUT THIS TAG IN THE head SECTION --> <script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"> </script> <script type="text/javascript"> GS_googleAddAdSenseService("ca-pub-7892222271864999"); GS_googleEnableAllServices(); </script> <script type="text/javascript"> GA_googleAddSlot("ca-pub-7892222271864999", "FlauntDarkLeader"); </script> <script type="text/javascript"> GA_googleFetchAds(); </script> <!-- END OF TAG FOR head SECTION --> <link href="style.css" rel="stylesheet" type="text/css"> </head> <?PHP include(/home/ucherllo/public_html/header.php); ?> <td height="265" align="left" valign="top" bgcolor="#FFFFFF"> <?PHP $number=10; include("/home/ucherllo/public_html/news/show_news.php"); ?> </td> <?PHP include(/home/ucherllo/public_html/sidebar.php); ?> SIDEBAR.PHP Code: [Select] <td width="247" align="left" valign="top" bgcolor="#FFFFFF">testing</td> </tr> </table> <!-- End Save for Web Slices --> </body> </html> HEADER.PHP Code: [Select] <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <table width="800" align="center" border="0" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="#FFFFFF"><center><!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT FlauntDarkLeader --> <script type="text/javascript"> GA_googleFillSlot("FlauntDarkLeader"); </script> <!-- END OF TAG FOR SLOT FlauntDarkLeader --></center></td> </tr> </table> <!-- Save for Web Slices (ucherlloyd.psd) --> <table width="801" height="359" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01"> <tr> <td colspan="11"> <img src="images/index_01.png" width="800" height="46" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="46" alt=""></td> </tr> <tr> <td colspan="7"> <img src="images/index_02.png" width="509" height="3" alt=""></td> <td rowspan="2"> <img src="images/index_03.gif" width="24" height="28" alt=""></td> <td colspan="3" rowspan="3"> <img src="images/index_04.png" width="267" height="179" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="3" alt=""></td> </tr> <tr> <td colspan="6" rowspan="3"> <img src="images/index_05.png" width="485" height="271" alt=""></td> <td> <img src="images/index_06.gif" width="24" height="25" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="25" alt=""></td> </tr> <tr> <td colspan="2" rowspan="4"> <img src="images/index_07.png" width="48" height="285" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="151" alt=""></td> </tr> <tr> <td rowspan="3"> <img src="images/index_08.png" width="13" height="134" alt=""></td> <td> <img src="images/index_09.gif" width="241" height="95" alt=""></td> <td rowspan="3"> <img src="images/index_10.png" width="13" height="134" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="95" alt=""></td> </tr> <tr> <td rowspan="2"> <img src="images/index_11.png" width="23" height="39" alt=""></td> <td><a href="index.php"><img src="images/index_12.gif" alt="" width="51" height="22" border="0"></a></td> <td> <img src="images/index_13.gif" width="88" height="22" alt=""></td> <td> <img src="images/index_14.gif" width="71" height="22" alt=""></td> <td> <img src="images/index_15.gif" width="93" height="22" alt=""></td> <td rowspan="2"> <img src="images/index_16.png" width="159" height="39" alt=""></td> <td rowspan="2"> <img src="images/index_17.png" width="241" height="39" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="22" alt=""></td> </tr> <tr> <td colspan="4"> <img src="images/index_18.png" width="303" height="17" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="17" alt=""></td> </tr> </table> <table width="799" border="0" align="center" cellspacing="0" cellpadding="5"> <tr> This is my website http://ucherlloyd.net/ PS: on Index.php there is also a Cutenews include that worked before i included sidebar.php & header.php HELP? Thanks in advance! Ok. So I.m trying to learn the language. I have this program: Code: [Select] <?php require_once("fillarray.inc"); ?> <html> <head> <title>PHP Class Lesson 2 - AJ</title> </head> <body> <h1>The Circumference of a Circle</h1> <br> <table border="1"> <thead> <tr> Circle Number:</tr> <tr> Diamater</tr> <tr> Circumference</tr> <tr> radius</tr> <tr> Area</tr> </thead> <?php foreach($arr as $diameter){ echo ("<td>"); echo("<tr align=\"center\"> 1 </tr>"); echo ("<tr> $diamater </tr>"); $circumference = $diameter * M_PI; echo ("<tr> $circumference </tr>"); $radius = $diameter/2; echo ("<tr> $radius </tr>"); $area = pow($radius,2)*M_PI; echo ("<tr> $area </tr>"); echo ("</td>"); } ?> </table> </body> </html> and in fillarray.inc I have: Code: [Select] $totalnum = rand(0,25); $arr = array(); for($i=0;i<$totalnum;i++){ $arr[$i] = rand(1,100); } But when I run it, this is the display i get: Quote $totalnum = rand(0,25); $arr = array(); for($i=0;i<$totalnum;i++){ $arr[$i] = rand(1,100); } The Circumference of a Circle Circle Number: Diamater Circumference radius Area Any help on whats going on? Im having a scope problem when creating a library. This is the test file: Code: [Select] <?php include_once 'lib/lib_inventario.php'; session_start(); hacerFormulario('Memoria Ram'); ?> And this the library importer: Code: [Select] <?php $lib_usada = 'Inventario'; include_once 'registroglobal.php'; $reg = RegistroGlobal::GetInstance(); include 'errores.php'; include_once 'test.php'; include 'sesiones.php'; include 'bd.php'; include 'seguridad.php'; include 'usuarios.php'; include 'web_inventario.php'; $reg->setConf('Libreria usada', 'Basica'); ?> Well, i explain what the second code does. It imports a few modules that have function or object definitions in they. So the idea is that just importing the lib_whatever file you import all the functions you need, and also set some configuration settings. My problem with the first code is that it says that the function is undefined, when it sure exist in web_inventario file... 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 code where i include file (on submit). if isset(... include "example.php"; This example.php contains a variable $message which i later print in original code. How do i prevent getting undefined variable errors? (before submit button is pressed) Thanks I am simple using include to include a file within the webpage. include $file; $file does exist and is a string. This works fine locally but when testing on my live server it isnt included and nothing below the include is executed, not even plain html. The error log shows nothing, and there are no errors on screen even when using E_ALL. So i decided to make $file point to the file on the server to see if it was picking it up and it does. So why doesnt the live server want to include this file? The file consists of php and html but there arent any errors. It wouldnt work locally if there were. Any ideas? Hey guys! Well this is actually kind of a weird problem I have been having, and not too sure why I'm getting this problem. I've tried a lot of stuff, and it doesnt seem like I've been able to find a solution. I've been trying for about a month. Anyways, I have a website created with some OOP. Well, once I write an include statement to include a file, which then includes all my classes, it messes up my font, moves my website content down, and well.. it just does weird stuff. However, once I comment the include out, the website looks normal again. so basically, I can comment out the file itself and everything is just fine. But it only happens when I include my class files. Any ideas or suggestions would be amazing! Thank you! Hi, I am trying to include my menu and header on my website. It seems to be picking up the pictures but not showing them. My code is: <?php include ("..\head.html"); ?> <table border="0" width="100%" cellspacing="0" cellpadding="2"> <tr> <td valign="top" align="left" width="90"> <?php include ("..\menu.html"); ?> My file structure is like this: FILE menu.php FILE head.php FOLDER blog - FILE index.php(where the code came from) FOLDER Images The menu and head files all link to the images folder, it is showing me the name of the image but just a little red cross. I hope that explains it ok. I don't knwo if i'm just missing something simple. Can anyone help? Haven't posted here for quite a while but I'm working on my first php driven website and am having a few problems with some includes that I'm sure you'll find to be fairly basic and obvious. What I'm trying to do is get my "home" page to load into a division when you initially visit my site. The header and footer had an include that contains various links which include this home page. The navigation links to the start page work perfectly and as expected, however when the site is visited initially all I get is a blank space in the main content division. The site in question is linked here... http://valvetech.byethost14.com/ The code that I'm using is as follows and I'm at a complete loss as to what I'm doing wrong. Code: [Select] <div id="center_column"> <?php $url = ''; if (!empty($_GET['category'])) { $url .= $_GET['category'] . '/'; } if (!empty($_GET['page'])) { $url .= $_GET['page'] . '.php'; } include $url; ?> </div> Thanks in advance for any assistance anybody can give me because I'm at the end of my rope on what should be a fairly easy problem to fix. -Carl |