I have a problem with my links getting added a extra subdirectory found to it when that link doesnt even exist I have tried everything possible, such as ../ to specifying to exact folder location within the links

Here what are the correct links are:
http://localhost/xampp/website/newsletter.php
http://localhost/xampp/website/admin/login.php

When I click on the login link to goto "http://localhost/xampp/website/admin/login.php"
the links are wrong by adding /admin to newsletter.php and an extra /admin to login.php:
http://localhost/xampp/website/admin/newsletter.php
http://localhost/xampp/website/admin/admin/login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
  session_start();

  $connect = mysql_connect('localhost', 'root', '') or die(mysql_error()); 
  $db = mysql_select_db('blog', $connect); 
   
  $session_name = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0; 
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home - </title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<link href="../css/styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <table border="0" cellspacing="5" cellpadding="3" align="center" width="1024">
    <tr>
	  <td id="header" align="center" valign="top">
	    <h1>Header here</h1>
	  </td>
	</tr>
	
	<tr>
	  <td id="navigation" align="center">
        <ul>    
          <li><a href="">Home</a></li> |  
          <li><a href="">Newsletter</a></li> |
          <li><a href="">Deans Column</a></li> |
          <li><a href="">Photos</a></li> |
          <li><a href="">Jobs</a></li> |
          <li><a href="">Interships</a></li> |
	      <li><a href="">scholarships</a></li> |
          <li><a href="">About Us</a></li> |
          <li><a href="">Contact Us</a></li>
        <ul>
	  </td>
	</tr>
	
	<tr>
	  <td id="admin" align="center">
	    <?php
		if ($session_name) {
		  echo "<a href=\"newsletter.php\">Newsletter </a>\n";
		  echo "<a href=\"admin/add.user.php\">Add Admin </a>\n";
		  echo "<a href=\"admin/logout.php\">Logut </a><br>\n";
		} else {
		  echo "<a href=\"newsletter.php\">Newsletter </a>\n ";
		  echo "<a href=\"admin/login.php\">Login </a>\n";
		}  
	    ?>           
	  </td>
	</tr>
	
	<tr>
	  <td id="content">

Dani AI

Generated

The behaviour is not a bug in PHP — it’s how browsers resolve relative URLs. A link like href="newsletter.php" is resolved relative to the current page’s URL, not the file that emitted the HTML. When you load a page under /admin/ the browser will turn newsletter.php into /admin/newsletter.php. That explains why your header links suddenly gain an extra /admin once you open the admin page.

Three practical fixes (pick one):

  • Make all navigation links root‑relative to your project (so they don’t depend on the current folder). If your project lives at /xampp/website use links that start there: href="/xampp/website/path".

  • Put a single base URL in the document head so every relative link resolves from the same place:

    <!-- put this once in <head> -->
    <base href="http://localhost/xampp/website/">

    Note: <base> affects every relative URL (images, forms, scripts) — test carefully.

  • Generate URLs from one central PHP value so included headers always emit absolute paths. Example config + helper:

    // config.php (include everywhere)
    define('PROJECT_ROOT', '/xampp/website'); // root-relative path from localhost
    function site_url($path = '') {
      return rtrim(PROJECT_ROOT, '/') . '/' . ltrim($path, '/');
    }
    
    // usage in header
    echo '<a href="' . site_url('newsletter.php') . '">Newsletter</a>';
    echo '<a href="' . site_url('admin/login.php') . '">Login</a>';

Quick troubleshooting steps: view-source / Inspect Element to see the computed href; right‑click → copy link address; temporarily hardcode a full URL to confirm it works; check for an accidental <base> tag or rewrite rules. As suggested, centralising the site URL avoids this class of bugs; and ’s virtual‑host suggestion is worth doing long‑term so your site can live at / instead of /xampp/website.

Recommended Answers

All 12 Replies

Member Avatar for Member #120589

either use the full url or use an aboslute (/ at the beginning).

yea i tried full url as well and / that doesnt work as well

is this page outside of admin folder?

the only pages outside from the admin folder is newsletter.php, header, and footer.php

pages that are in the admin folder are index.php, login.php, and add.user.php

Member Avatar for Member #120589

SO you're saying that:

echo "<a href=\"/newsletter.php\">Newsletter </a>\n";
 echo "<a href=\"/admin/add.user.php\">Add Admin </a>\n";
 echo "<a href=\"/admin/logout.php\">Logut </a><br>\n";
} else {
 echo "<a href=\"/newsletter.php\">Newsletter </a>\n ";
 echo "<a href=\"/admin/login.php\">Login </a>\n";

AND

echo "<a href=\"">Newsletter </a>\n";
 echo "<a href=\"">Add Admin </a>\n";
 echo "<a href=\"">Logut </a><br>\n";
} else {
 echo "<a href=\"">Newsletter </a>\n ";
 echo "<a href=\"">Login </a>\n";

Don't work?

href=\"./admin/login.php\">Login </a>\n"; ??

yes that is correct and i have even tried almostbob way as well

Have you tried as ardav said?

echo "<a href=\"">Newsletter </a>\n";
 echo "<a href=\"">Add Admin </a>\n";
 echo "<a href=\"">Logut </a><br>\n";
} else {
 echo "<a href=\"">Newsletter </a>\n ";
 echo "<a href=\"">Login </a>\n";

thats definitely not going to work cause its a website address, i am using a local server

definitely was just for example.

$siteurl = "http://localhost/xampp/website/";

if ($session_name) {
echo '<a href="'.$siteurl.'/newsletter.php">Newsletter </a>\n';
 echo '<a href="'.$siteurl.'/admin/add.user.php">Add Admin </a>\n';
 echo '<a href="'.$siteurl.'/admin/logout.php">Logut </a><br>\n';
} else {
 echo '<a href="'.$siteurl.'/newsletter.php">Newsletter </a>\n ';
 echo '<a href="'.$siteurl.'/admin/login.php">Login </a>\n';

Try above code.
and whenever you upload files to live don't forget to change $siteurl.

Member Avatar for Member #120589

Have you set up a virtual host in C:\xampp\apache\conf\extra\httpd-vhosts.conf and C:\Windows\System32\drivers\etc\hosts ?

perhaps dumb, but what is the root setting on the wamp install?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.