1

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

mastermind93
  • 11
  • 1
  • 3

2 Answers2

1
<?php $docRoot= "http://" . $_SERVER['HTTP_HOST'].  $_SERVER['REQUEST_URI'];  ?>

check this about $_SERVER variables

Elijan
  • 1,406
  • 1
  • 8
  • 11
  • Thanks for your reply! But I tried this also before. The result is an error: URL file-access is disabled in the server configuration in.... – mastermind93 Dec 01 '12 at 05:32
  • @mastermind93 that error relates to the different functions in php, what exactly are you tryign to do? are you including something? – Elijan Dec 01 '12 at 05:38
  • I will try to explain it as good as I can. The Directory structure looks like this: mywebsite (root directory of my website, which is in xampp/htdocs), mywebsite/includes (sub-directory for includes) and mywebsite/faq/index.php What I would like to do: 1.Include the functions.php from the subdirectory "includes" in the file "mywebsite/faq/index.php" without using everytime "../". 2. I have some pictures, which are in the directory "mywebsite/images". So I would like to show the image also in the file "mywebsite/faq/index.php" without using "../" only something like: $docRoot/images. – mastermind93 Dec 01 '12 at 06:05
  • @mastermind93 The error you are having is because includes can;t includes files from url (security issue to prevennt cross domain includes) that;s why you have an error (you can disable it in php.ini. google fro error) Use relative paths when you do your includes. e.g `include('includes/function.php')` – Elijan Dec 01 '12 at 10:31
1

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>