开发者

how to write hyperlinks in include files

I have a website on my local machine (Windows 7, XAMPP). My site uses some PHP includes. The includes will appear on many pages on the site, possibly nested in many levels of directories. This makes it impossible to use relative links. The problem is solved by using absolute links, but absolute links will be diff开发者_C百科erent on my local machine versus the remote server. For example, I would write an include file for a web server like:

<nav id="main">
  <ul>
    <li><a href="http://domainname.com/topic1/">Topic1</a></li>
    <li><a href="http://domainname.com/topic2/">Topic2</a></li>
  </ul>
</nav>

But on my local machine, I'd have to write:

<nav id="main">
  <ul>
    <li><a href="C:\xampp\htdocs\website\topic1\index.php">Topic1</a></li>
    <li><a href="C:\xampp\htdocs\website\topic2\index.php">Topic2</a></li>
  </ul>
</nav>

Does anyone know how this problem is generally handled?


Most frameworks handle this by using a config value($base_url = 'http://www.domain.com';) and then calling that when creating links.

<nav id="main">
  <ul>
    <li><a href="<?php echo $base_url;?>/topic1/">Topic1</a></li>
    <li><a href="<?php echo $base_url;?>/topic2/">Topic2</a></li>
  </ul>
</nav>

This should solve the problem as long as you remember to change your base URL when moving between servers.

However, you still shouldn't be linking to something like C:\xampp\htdocs\website\topic1\index.php, it should be http://localhost/website/topic1/index.php.


There's no need to store the document root in a config and have to change it each time you relocate the app.

Use the DOCUMENT_ROOT server variable:

<a href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/path/to/file.php">Link</a>


You can define a constant in a configuration .php file that you include everywhere:

define('ROOT_URL', '/');

If you're on your Windows matchine (where you'll likely be running the web app at http://localhost/..., not requesting PHP files from C:\), change that to:

define('ROOT_URL', '/website');

Then use this constant when generating links:

<li><a href="<?php echo ROOT_URL ?>/topic1/index.php">Topic1</a></li>


You can either use relative links like (forward slashes are good option):

<a href="../topic2/index.php">Link to Topic 2</a>

or use absolute links with PHP variable:

<?php
    $topUrl = 'c:/xampp/htdocs/website';
?>
<a href="<?=$topUrl?>/topic2/index.php">Link to Topic2</a>

But my preffered solution is using some template engine - I preffer Smarty.


"/" is equivalent to 'http://www.mydomain.com' or 'http://localhost' (the root of your website) and preserves your http or https protocol why right what is already there? If you're nesting your website in folders, that's not a great idea for testing. I drop the application I want to test into my local server root folder to test (just like a real webserver) and remove it when I'm not testing it's a very simple solution.

<nav id="main">
  <ul>
    <li><a href="/topic1/index.php">Topic1</a></li>
    <li><a href="/topic2/index.php">Topic2</a></li>
  </ul>
</nav>


Personaly I would do it the following way

config.php

if ($_SERVER['REMOTE_ADDR'] == 'your pc ip here') {
   // Its for the local site
   $rootUrl = 'http://localhost/'; // The url needed for the local website
} else {
   // The url for the online version
   $rootUrl = 'http://mydomain.com/';
 }
 // Set the url ready
define('ROOT_URL', $rootUrl);

The files where the url is needed

<?php include('config.php'); // Path to the config file ?>

<nav id="main">
  <ul>
    <li><a href="<?php echo ROOT_URLl;?>topic1/">Topic1</a></li>
    <li><a href="<?php echo ROOT_URLl;?>topic2/">Topic2</a></li>
  </ul>
</nav>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜