I have create my system of three folders
pages folder
style folder
images folder
and one index.php file.
My problem is how I can include index.php file in my file login.php which are found in pages folder.
I'm trying to header ("Location:index.php") don't work.
Not: this index.php is single file located outside of those folders
<?php
header ("Location:index.php");
?>
Just make the URL's relative from the document root (by adding an initial /):
header('Location: /index.php');
or if you want to redirect to a sub folder:
header('Location: /theFolder/index.php');
Then you don't need to care what folder you're in and add a bunch of ../../.
When traversing a folder tree, you can use ../ to denote a parent folder.
. is a shortcut to 'the current folder'.
.. is a shortcut to 'the folder containing the current folder'.
Assuming the index.php file is in the parent folder of the login.php page, you can access it like so:
header ("Location: '../index.php'");
Related
I've a web site made of some folders, one for each section (info, news, blog etc...).
In each of these folders there is an index.php file that should load a layout (common to all). These are stored in a different folder in the root where there is also the main index.php file (the homepage). So i have something like this:
root
-index.php (home)
-/layout
--layout files
-/info
--index.php`
The index file in the /info folder should include the page layout from /layout.
The problem is that the layout files should include other files from other folders.
In layout files I put this:
include 'contents/page-element.php';
But if I try to reach the same page-element.php file from the index.php file in the /info folder, I should do:
include '../contents/page-element.php';
to go to the root and then reach the /layout folder.
I don't want to create a copy of the layout for the folders so I've tried $_SERVER['DOCUMENT_ROOT'], but it does not work in some cases on the localhost and even on the web server.
Can someone help me or let me know how I can build a dynamic absolute path?
By using the built-in constant __DIR__ you can get your absolute path
by print it or save
echo __DIR__;
it will print something like this for you
C:\Users\user\Desktop\test
and there is also another constant that will get the absolute path for your file that executes the command __FILE__
echo __FILE__;
will give you a result like this
C:\Users\user\Desktop\test\index.php
I have an index.php in root and a 'includes' folder containing header.php and footer.php.I included header and footer in index.php and it works fine but in header and footer I include some different files in config folder in root.
I know that I should use "../config/config.php" but it works with 'config/config.php'.
I included header and footer in index.php and I should use path according to the index that I included to ???
what if I include one file in several folders ???
Try to use __DIR__ it will give you the path compared to where your current file exist, also try to use include_once instead of include
DIR : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to
dirname(FILE). This directory name does not have a trailing slash
unless it is the root directory.
Include your file like this from within your header.php and footer.php:
include_once DIR.'/../config/config.php';
do the same technique for other files
Yes your path should be according to index.php that's mean your root directory, not your includes folder.
At any time you can check such issues by
echo __DIR__;
to make sure from your path.
So my folder is called
leaguenotes and inside that folder i have a folder administration which is my subdomain folder so
leaguenotes
-index.php
-js
-files.js
-administration
-index.php
-otherfiles.php
And from administration index I'm trying to include in this example files.js with <?php echo '<script src="'.dirname($_SERVER['DOCUMENT_ROOT']).'/js/jquery.min.js"></script>'; ?>
$_SERVER['DOCUMENT_ROOT'] = /var/www/html/leaguenotes/administration which is the subdomain folder so I'm using dirname to get to parent folder /var/www/html/leaguenotes and add /js/jquery.min.js and still the file is not found
if you access your page, take a look at the html source-code, i think you will see the source produced from your php code is not the correct one. because a real subdomain is a different domain then your domain itself. It is treated by your browser as a different domain.
I am trying to clean up my website folder structure. I have too many processing scripts in the root directory. The reason is because they won't work if I move them to a sub folder and call them from there.
For eg.
root directory
- images folder
- css folder
- js folder
- includes folder
- admin folder
index.php
ajax.php
Say I move the ajax.php to includes folder, it will look like this.
root directory
- images folder
- css folder
- js folder
- includes folder
- ajax.php
- core folder
- template folder
index.php
Now on every page I have an init.php required. In ajax.php for eg.
<?php require_once 'core/init.php'; ?>
// rest of the code
The issue I am having is that the init.php file won't run as long as ajax.php is in includes folder. I get an error something like this.
Warning: require_once(core/init.php): failed to open stream:
If i move the ajax.php back to the root directory, it'll work fine.
On index.php, this is how I am calling the ajax.php
core/init.php works fine when requiring in the "templates" folder.
Perhaps someone can tell me the solution to this?
Try using PHP's chdir function. This will change the working directory that your PHP environment is working in, and therefore the relative paths that your include functions are looking in. Ideally, you'd want to call chdir with the root of your project, so that when you do require_once('core/init.php'); you don't end up calling from the core directory, but rather your . directory.
Sorry for asking again, but I really need help. I have header.php in the root/lib which is including header_sub.php in the same directory. Normally files in root can directly include them by this code:
include_once('lib/header.php');
but now i have example.php in a sub-directory /blog, if i use these
include_once(../'lib/header.php'); or
include_once($_SERVER['DOCUMENT_ROOT'].'/lib/header.php'); or
include_once(dirname(__FILE__).'/lib/header.php');
header_sub.php would not be included correctly.
Is there a way to include header.php and header_sub.php without modifying them?
Some body suggested to use these:
$oldcwd = getcwd(); // Save the old working directory
chdir("../"); // Moves up a folder, so from /blog to /
include("header.php"); // Include the file with the working directory as if the header file were being loaded directly, from it's folder
chdir($oldcwd); // Set the working directory back to before
However, even i can see the current url is root directory after chdir(), it still includes this root/blog/lib......
The path to the file you need depends on were you are calling the file.
Examples:
In /root calling file in /root -> include_once('header.php');
In /root calling file in /root/lib -> include_once('lib/header.php');
In /root/lib calling file in /root -> include_once('../header.php');
In /root/blog calling file in /root/lib -> include_once(../lib/'header.php');
In /root/blog/css calling file in /root/lib -> include_once(../../lib/'header.php');
If you do like this all path are relative and you can change the root folder and everything still works.
Another option is you have a file called "common.php" or "include.php" were you define path for some folders. This is useful if your site directory has many sub folders.