some subdirectories will display correctly and other wont...

Also these are the folders i have

website

  • admin
    • add.user.php
    • index.php
    • login.php
  • css
    • styles.css
  • footer.php
  • header.php
  • newsletter.php

1. http://localhost/xampp/website/newsletter.php
correct sub...http://localhost/xampp/website/newsletter.php
wrong sub...missing /admin after website http://localhost/xampp/website/add.user.php
wrong sub...missing /admin after website

2. http://localhost/xampp/website/admin/
wrong sub...http://localhost/xampp/website/admin/newsletter.php
correct sub...http://localhost/xampp/website/admin/login.php

3. http://localhost/xampp/website/admin/login.php
wrong sub...http://localhost/xampp/website/admin/newsletter.php
correct sub...http://localhost/xampp/website/admin/login.php

4. http://localhost/xampp/website/admin/login.php
wrong...subhttp://localhost/xampp/website/admin/newsletter.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>";
		  echo "<a href=\"./add.user.php\">Add Admin </a>";
		  echo "<a href=\"./logout.php\">Logut</a><br>\n";
		} else {
		  echo "<a href=\"./newsletter.php\">Newsletter </a> ";
		  echo "<a href=\"./login.php\">Login</a>";
		}  
	    ?>           
	  </td>
	</tr>
	
	<tr>
	  <td id="content">

Dani AI

Generated

This is the classic “relative link in a shared include” problem. A link like ./newsletter.php or news.html in a header file will be resolved by the browser against the current page’s URL, so the same header will produce /website/newsletter.php when included on a root page and /website/admin/newsletter.php when included from a page inside /admin. That explains why some links look correct and others suddenly have (or lack) the /admin/ segment. described the symptom and correctly flagged the ./ usage as the source.

Fixes you can apply (pick one and be consistent):

  • Use root‑relative URLs (start with a slash) so every page resolves the same target:

    <a href="/myproject/newsletter.php">Newsletter</a>
  • Define a single base URL in one config file and build links from it (keeps move/rename easier):

    // config.php (load once)
    define('BASE_URL', '/myproject');
    
    // in header
    <a href="<?php echo BASE_URL; ?>/newsletter.php">Newsletter</a>
  • Use the HTML <base> tag in the document head to control how relative links resolve. Be careful: base affects all relative URLs (images, CSS, scripts) so test thoroughly.

  • Or keep separate header/includes for different folder depths (admin/header.php vs site/header.php) so relative links can stay simple.

Quick troubleshooting tips:

  • Inspect the rendered anchor href with browser dev tools to see the final resolved URL.
  • Don’t put server filesystem paths (what PHP returns for DOCUMENT_ROOT) directly into an href — browsers need URL paths, not local file paths.
  • As noted, make sure session_start() and any header-affecting PHP run before any HTML output to avoid “headers already sent” issues.

Choose one of the above patterns (root‑relative or BASE_URL are easiest) and apply it across all includes; that will stop the link target from jumping between root and admin.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589
session_start();

should be above the DTD.

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

THis is the issue, I think (./)

You don't state which page you're outputting.

Use relative links where possible (../admin/etc.php or admin/etc.php or just etc.php)

Else use absolutes

$root = $_SERVER['DOCUMENT_ROOT'];

echo "<a href=\"$root/newsletter.php\">Newsletter </a>";

same result when i use ../ and $root...the pages that use the correct address becomes incorrect and the pages that uses the incorrect address becomes correct

Member Avatar for Member #120589

OK, perhaps your problem is localhost.

The root (html) is http://localhost
Your main folder is

http://localhost/xampp

To get the document_root version to work, do this:

$root = $_SERVER['DOCUMENT_ROOT'];
echo "<a href=\"$root/xampp/newsletter.php\">Newsletter </a>";

BTW, if you're on Windows, I'd suggest that you 'register' all your projects in the \windows\system32\drivers\etc\hosts and the \xampp\apache\conf\extra\httpd-vhosts.conf files.

I tend to use as a name for my local projects, like so:

httpd-vhosts.conf

<VirtualHost *:80>
  DocumentRoot c:/xampp/htdocs
  ServerName localhost
</VirtualHost>
 
<VirtualHost *:80>
  DocumentRoot c:/xampp/htdocs/diafol
  ServerName diafol.local
</VirtualHost>

hosts

127.0.0.1       localhost
::1             localhost
127.0.0.1 	diafol.local

After making changes restart Apache.

The html root is now

So in your case,

I'd make a directory under xampp\htdocs\myproject (or whatever)
Copy all your project files to it
Then do as above:

<VirtualHost *:80>
  DocumentRoot c:/xampp/htdocs/myproject
  ServerName myproject.local
</VirtualHost>

and

127.0.0.1 	myproject.local

Just access this then as

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.