PHP:如果用户在特定页面上,则编写指向不同链接的函数

I'm fairly new to PHP. I have a small issue with an application I'm working on. I have a very simple header for the application that displays on all the pages. Heres's my code for it:

Header

function writeHeader($profile) {
    <a href="update_profile">Update Profile</a>
}

The purpose of the header is to direct the user to their "Update Profile" page. So the header functions just fine if the user is on the Index page (because the route is just "update_profile"). But I have several sub-pages in my app, they are structured kind of like this:

-index.phtml
-characters/create.phtml
-characters/edit.phtml
-worlds/create.phtml
-worlds/edit.phtml
-etc.

Whenever a user navigates to a page other than "Index", the link stops working because the route has changed.

I considered writing a whole new functions just to write headers specifically for these pages, but I'm wondering if there's a way to just update the function I have now to change the route if the user is navigating to the "Update Profile" page from anywhere other than Index. Can anyone give any suggestions for how to do this?

You have to be the closest possible to what you'll get in production. It means that you don't have to target the localhost, but the domain instead. To do so, you have to create a virtual host in your http.conf file, and target it. This way, you should be able to use the absolute path, as Nick said, and it will be the same once your app will be deployed.

You could do something like:

    $base_url = 'http://localhost.com/';

    function writeHeader($profile){ 
       echo '<a href="'.$base_url.'update_profile">Update Profile</a>';
    }