php中的路径无法正常工作

php中的路径无法正常工作

问题描述:

I have a navigation bar I include in my pages, which worked fine as long as all the php files were in the same dir. Once I moved them into folders the navigation bar would only allow one link to be clicked then once in a lower dir the pathing to files were incorrect.

Dir structure:

php-->

  • navpanel.php

  • search.php

  • user folder

    • checkusername.php

    • etc php files

  • database folder

    • createdatabase.php

js-->

  • js files

php code im using which is included in the other files.

<?php
$path = $_SERVER["DOCUMENT_ROOT"];
echo $path;
if ($_SESSION['admin'] == 1){
    echo '
        <body>          
            <ul id="menu">
                <li><a href="search.php">Search</a></li>
                <li><a href="#"></span> User</a>
                    <ul>
                        <li><a href="user/createuser.php">Create User</a></li>
                        <li><a href="user/edituser.php">Edit User</a></li>
                        <li><a href="user/deleteuser.php">Delete User</a></li>
                        <li><a href="user/usergroup.php">User Groups</a></li>
                    </ul>
                </li>
                <li><a href="#">Databases</a>
                    <ul>
                        <li><a href="database/createdatabase.php">Create Database</a></li>
                        <li><a href="database/editdatabase">Edit Database</a></li>
                        <li><a href="database/deletedatabase">Delete Database</a></li>
                        <li><a href="database/databasegroup">Database Groups</a></li>
                    </ul>
                </li>
                <li><a href="../connect/logout.php">Log Out</a></li>
            </ul>         
        </body>';
}
else {
    echo '
        <body>

            <ul id="menu">
                <li><a href="search.php">Search</a></li>
                <li><a href="connect/logout.php">Log Out</a></li>
            </ul>

        </body>
    ';
}

The problem is in the HTML output. If you want the links to be relative from the root (http://www.example.com/), then you should use /user/createuser.php, /user/edituser.php, etc.

Although unrelated to your links, you are referencing the session variable $_SESSION['admin'] without calling session_start(); at the top of your script. Session variables return null if you don't call session_start();.

<?php
session_start(); // u forgot to place this
$path = $_SERVER["DOCUMENT_ROOT"];
echo $path;
if ($_SESSION['admin'] == 1){....

Try changing your links to something like:

echo '<li><a href="'.$path.'user/createuser.php">Create User</a></li>';