Reputation: 149
So lets say I have this given directory
public_html
index.php
Now lets say I want to include page2.php inside page1.php i do this:
include "/home/page2.php";
But it doesn't work, the / is supposed to be the root directory (public_html).
or I want to redirect from reg.php to index.php
"/index.php" doesnt work.
Note: this is all on a hosting server.
Upvotes: 0
Views: 756
Reputation: 72865
/
is not the site root directory, it is the root directory of your OS's filesystem.
You can use a relative path modifier like so: ../../yourincludefile.php
to navigate upwards through the filesystem
Or you can use $_SERVER['DOCUMENT_ROOT']
to call the full path from the OS. DOCUMENT_ROOT
will offer you more consistent results and lets you include the same file no matter where in the directory hierarchy you're working.
For example, if you have includes/functions.php
, you can use this anywhere, on any script and it will always include the correct file path:
include_once($_SERVER[DOCUMENT_ROOT] . "/includes/functions.php");
or using the reference inside the string:
include_once("$_SERVER[DOCUMENT_ROOT]/includes/functions.php");
Upvotes: 4
Reputation: 1008
Option 1 - file-relative:
include("../path/to/file.php");
Option 2 - root relative
$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/path/to/file.php");
Upvotes: 0
Reputation: 2350
You should be able to use relative paths, ie:
I want to include page2.php inside page1.php i do this:
include "../page2.php";
I want to redirect from reg.php to index.php
"../../index.php"
Or if you are talking about index.php inside public_html,
"../../../index.php"
Upvotes: 0