Reputation: 11
I have created a website using Xampp as local server. My problem is at the moment, that I would like to include files and set links everytime begining from the root directory.
I tried a lot of methods but still not anyone is working. :(
Have tried following in the config.php already:
$docRoot = dirname(__FILE__).'/mywebsite/';
and also $docRoot = $_SERVER["DOCUMENT_ROOT"].'/mywebsite/';
For includes it is working like this:
But it isn't working for links in HTML.
If i try something using $docRoot like:
<a href="<?=$docRoot?>index.php"></a>
the result:
C:/xampp/htdocs/mywebsite/index.php
so it doesn't redirect to the link which is set...
but it should be just: localhost/mywebsite/index.php
Sorry for my english, I'm learning it. :D
Thanks and best regards
Upvotes: 1
Views: 3347
Reputation: 31
What you are trying to do is to include a local file from your folders, you don't have to use a URL to do that, you have to use the local file path to that file, why don't you try something like this
<?php
// inside a config file
define('DS' , '/' );
define('LOCAL_DIR' , 'mysite' );
define('ROOT_PATH' , $_SERVER["DOCUMENT_ROOT"] . DS . LOCAL_DIR . DS );
define('BASEURL' , $_SERVER["HTTP_HOST"] . DS . LOCAL_DIR . DS);
?>
So whenever you want to include a file inside of any other script, you use:
<?php
include( ROOT_PATH . 'your_includes_directory/filename.php' );
?>
And whenever you want to print a link you use something like this:
<a href="<?=BASEURL;?>filename.php?var1=asd>Link to file</a>
Upvotes: 1
Reputation: 1416
<?php $docRoot= "http://" . $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI']; ?>
check this about $_SERVER variables
Upvotes: 1