PHP require_once directories issue - php

I have a website with the following directory structure:
index.php
connections/database.php
contacts/details.php
inc/functions.php
I would like the database.php page to use require_once to include the functions.php page e.g:
require_once 'inc/functions.php';
I would like to then use require_once for the both the index.php page and the details.php page to just require the database.php page which in turn will also include the functions.php page. I can get this working for either the index.php page or the details.php page but not both as I have to keep changing the syntax, e.g.
require_once 'inc/functions.php';
require_once '../inc/functions.php';
Otherwise I get errors that the functions.php page cannot be found. Is there a way to include the database.php page which also includes the functions.php page that will work from the different directories for both the index.php page and the contacts/details.php page?
thanks

Your best bet is to set a $basepath variable in your config file pointing to the absolute root path of your app. Then you can go with require_once $baseapth."/inc/functions.php"

You can use dirname(__FILE__)!

Related

PHP How to exclude a file which is included in the file I am including?

Suppose I want to include header.php file and header.php already contains another file included functions.php. If in my index.php I am including header.php then naturally functions.php will also get included. But what if for some specific reason I want to break the joint and exclude functions.php while including header.php? Is this possible?
Since after a lot of searching I could not find a native PHP function the workaround I came up with is:
if(basename($_SERVER['PHP_SELF']) != 'page-name.php'){
include('functions.php');
}
With this code functions.php will be included in all pages from header.php except only on page-name.php
You can just change for:
include_once 'functions.php';
so it only will include once even if Header.php and Index.php has it.

Dynamic linking in PHP

I am creating a custom CMS for learning. I have the plan to have the following pages,
Login page
All Posts page
Edit Post page
index page
header.php (the website's header)
footer.php (the website's footer)
sidebar.php (the website's sidebar)
I am confused how would the index page link header, footer and sidebar. Please guide me how can I link these php files in index.php.
You can simply add an Array of files you want to include:
$array = ('header.php', 'footer.php', 'sidebar.php');
Then add some HTML Code structure...
and then you can access the Array and load files.
include_once($array[0]);
.. to include the header.php
include_once($array[1]);
.. to include the footer.php
....
you can use the require_once function to make your site not loading other content if the file does not exists.
if you want to add these files automaticly just add a loop.
foreach($array as $file){
if(file_exists($file)){
require_once($file);
}
else{
die($file.' does not exist!');
}
}
You can use either require_once or include.
I personally use require once option cause it only needs to be include once and not again on later stage.
Inside your body tags:
require_once('/path/to/header.php');
require_once('/path/to/breadcrumb.php');
require_once('/path/to/content.php');
require_once('/path/to/footer.php');
require_once('/path/to/copyright.php');

Why doesn't wordpress need to include anything before using a function

When I use get_header(); in index.php, it automatically load the header.php file and and need not to include anything in the page.
Generally index.php is the main page. everything should be include there before it was called. Am I wrong?
It seems you are checking index.php inside the theme. But that is not the entry point to the site, but the index.php file in the wordpress root. There you can see it has included some files and inside those they have included some other files. Like wise file including functions are written in the wordpress core codes. So, you don't want to worry about including files in your theme. if you call get_header() function, the function itself has coded so that the relevant file to be included. If you are going to use a separate file to code functionality other than standard wordpress theme files, you'll have to include it at the top of functions.php

Add php page in wordpress outside the theme

I have added a php page (test.php) in my root directory (WWW) where wordpress is installed. I don't want to use the custom template method (add this page in my wordpress theme directory)
I have added in this php file this:
<?php
require('wp-blog-header.php');
get_header();
?>
<h1>Test</h1>
<?php
get_footer();
?>
When I do: www.example.com/test.php this page is correctly loading but the title of this page display "error 404"
I don't understand how can I resolve this problem.
you need to include wp-load.php to use header, in your case replace require('wp-blog-header.php'); with
<?php require_once('wp-load.php'); ?>
I have placed this code in my root directory in test.php file. This code is working fine if you replace require('wp-blog-header.php'); with require_once('wp-load.php');

get_header wordpress function invocation

The first line of index.php is get_header();
There is no include / require in my index.php. yet the wordpress site continues to render the head section of home page by referring get_header() from general-template.php
How is wordpress framework achieving this?
get_header() is a function located in wp-includes/general-template.php.
This function includes the header.php template file from your current theme's directory.
If your theme contains no header.php file then this function includes the header file located in wp-includes/theme-compat/header.php.
You can find more information about this function in codex of Wordpress.org
The index.php you're referring to is the index off your theme. Within the root folder of your WP installation also an index.php is located. This is the first one called and starts the whole 'trip'.
http://theme.fm/wp-content/uploads/2011/09/wordpress-internals-how-wordpress-boots-up-382x600.png shows an image of the complete file load-sequence.

Categories