Jump to content

PHP include in HTML href?


aarontbode

Recommended Posts

I'm a newby to PHP. Let's get that out of the way first. Here's what I'm looking to do:

 

<a href="<?php include("../file.php"); ?>">Text here</a>

 

My goal is to have a URL placed on the file.php document, so instead of having to update each instance of that link individually I can just update the document and streamline the whole process.

 

How can I achieve this? Is this possible, and what method should I be using? Thanks!

Link to comment
Share on other sites

You could. For example, you could put an echo statement in file.php which displays the URL. Then when you include() file.php, as you have in the code above, the URL would be added to the href attribute in the source code.

 

For what it's worth, you would probably be better off storing the common URLs in a database. Having them scattered in a bunch of PHP files seems like a potential maintenance nightmare.  :unsure:

Link to comment
Share on other sites

Thank you both for your replies. What would you recommend (code snippets would be highly appreciated)?

 

The URL will need to be updated frequently across multiple files, so my goal is to be able to hop in, update the link, and have it reflect across multiple files. Currently, I have to go in and update each link manually, which isn't the end of the world but I'm trying to determine a more efficient way of updating the links.

Edited by aarontbode
Link to comment
Share on other sites

It might help to know more information on the project. As requinix mentioned, "What is the problem that you think having the URL in a PHP script will solve?"

 

Are these links primarily internal links (links to the same website / links you control) or external links?

 

Also could you clarify what you mean by frequently changing URLs. I guess I haven't had any issues with links changing that often. It can feel that way, since there always seems to be a website that changes their domain name or decides to restructure their content. But I don't think I've needed to update the same link tag within the same year before.

 

Are you hosting a library of links? If so, is there other data associated with the links, such as page / website name? I would lean towards using a database for storing connected data like this.

Link to comment
Share on other sites

Creating a script to produce a single URL is nonsense, so that's out of question.

 

Surely you have an application-wide configuration. If you don't, now is the time to create one. Put the URL into the configuration, then simply reference it whenever you need the URL.

<a href="<?= html_escape(config_get('foo_url'), 'UTF-8') ?>">text</a>
Edited by Jacques1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.