0

I have the following code in index.html:

<div class="button">
    <a href="ridiculously long string" title="title">Title</a>
</div>

I'd like to save "ridiculously long string" in a text file, referenced by index.html. Is this possible?

I tried replacing the string like so the following, but it doesn't work: php reference: file_get_contents()

<div class="button">
    <a href=<?php echo file_get_contents("text.txt"); ?> title="title">Title</a>
</div>

Errors symptoms: the button on my page now reads title="title">Title and clicking it takes me to a 404: The requested URL /~user/html_root/< was not found on this server.. index.html and text.txt are in the html_root directory.

Here's how one of the shorter text.txts read:

?autoplay=0&amp;trail=0&amp;grid=1&amp;colors=1&amp;zoom=1&amp;s=%5B{%228%22:%5B60,61,98,103,109,115%5D},{%229%22:%5B60,61,77,78,97,99,102,104,108,110,114,116%5D},{%2210%22:%5B76,79,98,103,105,109,111,115,117%5D},{%2211%22:%5B76,79,104,110,112,116,118%5D},{%2212%22:%5B60,61,63,64,77,78,111,117%5D},{%2213%22:%5B60,61,63,64%5D},{%2219%22:%5B76,77,79,97,98,102,103,108,109,114,115%5D},{%2220%22:%5B76,78,79,97,99,102,104,108,110,114,116%5D},{%2221%22:%5B98,103,105,109,111,115,117%5D},{%2222%22:%5B104,110,112,116,118%5D},{%2223%22:%5B61,111,117%5D},{%2224%22:%5B60,62,76,77%5D},{%2225%22:%5B60,62,75,78%5D},{%2226%22:%5B61,76,79%5D},{%2227%22:%5B77,78,96,97,102,103,109,110,115,116%5D},{%2228%22:%5B96,98,102,104,109,111,115,117%5D},{%2229%22:%5B61,65,97,98,103,105,110,112,116,118%5D},{%2230%22:%5B60,62,64,66,104,105,111,113,117,119%5D},{%2231%22:%5B60,62,64,66,75,76,112,113,118,120%5D},{%2232%22:%5B61,65,75,78,119,120%5D},{%2233%22:%5B77,78%5D},{%2237%22:%5B78,79%5D},{%2238%22:%5B77,79%5D},{%2239%22:%5B77%5D},{%2240%22:%5B60,61,63,64,75,77%5D},{%2241%22:%5B61,63,75,76%5D},{%2242%22:%5B61,63%5D},{%2243%22:%5B60,61,63,64,114%5D},{%2244%22:%5B78,79,84,85,92,93,95,113,115%5D},{%2245%22:%5B79,84,86,92,93,95,96,97,104,112,115%5D},{%2246%22:%5B78,86,98,103,105,111,113,114%5D},{%2247%22:%5B75,77,86,87,92,93,95,96,97,102,105,110,112%5D},{%2248%22:%5B75,76,93,95,103,104,109,112%5D},{%2249%22:%5B93,95,110,111%5D},{%2250%22:%5B94%5D}%5D

I thought changing text.txt to a more benign URL might help debugging. I changed text.txt to https://www.google.com/ and get the same 404.

I could implement a javascript solution. There's already js on this webpage. But it's controlled by a colleague and I'd prefer to try a stand alone solution first. Many thanks to anyone who can help!

  • 3
    It might help us if you could explain why "ridiculously long string" was being used as the value of an `href`. – Andrew Morton Nov 30 '19 at 19:58
  • 1
    How does `file_get_contents()` fail? – symlink Nov 30 '19 at 19:59
  • @AndrewMorton I don't understand the existing code, but it reads something like "?autoplay=0&trail=0&other_variables;s=%5B{%228%22:%5B60,61,98,103,109,115%5D},{ tons },{ and },{ tons },{ of },{ data }%5B" – John HaTrick Nov 30 '19 at 20:12
  • @symlink I added symptoms to my question – John HaTrick Nov 30 '19 at 20:12
  • 1
    @JohnHaTrick I don't understand, the href attribute gets completely removed? – symlink Nov 30 '19 at 20:16
  • I guess so. I don't understand either! Pretty green on html and brand new to php. Maybe I need an include statement or library to run php? – John HaTrick Nov 30 '19 at 20:22
  • 1
    @JohnHaTrick Why not explain further what it is exactly that you want to do and why? – Brad Nov 30 '19 at 20:22
  • @Brad Thanks for chiming in. I'm borrowing this MIT-licensed html to adapt for a class project. It has ~10 of these "ridiculously long strings", which I'd like to distribute out to files to make the html easier to read, understand, and adapt for my project. Appreciate all your questions! – John HaTrick Nov 30 '19 at 20:28
  • 1
    @JohnHaTrick That's really not helpful to us... what's in the text file? Moving all this to external text files really sounds much less easier to read. If you could actually show us what's in the file, we can probably suggest a way to format your code. (For example, the HTML tag doesn't need to be all on one line.) – Brad Nov 30 '19 at 20:46
  • Added excerpt from txt below your answer – John HaTrick Nov 30 '19 at 20:50

1 Answers1

2

Anytime you want to inject arbitrary data into HTML, you need to wrap it with htmlspecialchars() so that any reserved characters are escaped. Additionally, you actually need to surround attribute values with quotes or you're going to be generating invalid HTML.

<a href="<?php echo htmlspecialchars(file_get_contents('text.txt')); ?>" title="title">Title</a>

Really though, "ridiculously long string" is questionable anyway. I assume you're using some huge data URI? If so, consider not doing that, as there are limits you'll run into and it's not efficient to base64-encode things.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Ok one of my symptoms is now abated. The button now bears its correct label. The 404 error remains. – John HaTrick Nov 30 '19 at 20:34
  • To be more specific, the 404 reads: "/~user/html_root/< was not found". html_root is the directory of my index.html and text.txt. I would expect something more like "text.txt was not found"... – John HaTrick Nov 30 '19 at 20:42
  • 1
    @JohnHaTrick Surely that path is what's in your text file? – Brad Nov 30 '19 at 20:45
  • No path in the text file. It reads: ?autoplay=0&trail=0&other_variables=0&...;s=%5B{%228%22:%5B60,61,98,103,109,115%5D},{ tons },{ and },{ tons },{ of },{ data }%5B – John HaTrick Nov 30 '19 at 20:48
  • 1
    @JohnHaTrick Use backticks to format code on Stack Overflow, or your HTML can get eaten and it isn't clear what is in what. And you say tons-and-tons-of-data... can you show literally what's in it? Can you edit your question to show literally what's in that file, and what the actual HTML output is? – Brad Nov 30 '19 at 20:51
  • I tried a substitute text.txt that just reads: https://www.google.com/ and I get the same 404 error. – John HaTrick Nov 30 '19 at 21:04
  • I'm also not sure where you recommend using the backtick(`), but I really appreciate the advice on how to get my question formatted correctly. It's not a character I've really ever used. Is there a particular section you find ambiguous? – John HaTrick Nov 30 '19 at 21:10
  • @JohnHaTrick Again... what's the actual HTML output? Right click on the page, view source. Paste that into your question as well. Also, now that we can see your text file, it looks like it's not actually text but is already formatted HTML. Therefore you don't need/want to use `htmlspecialchars()` in this case. – Brad Nov 30 '19 at 21:16
  • Sorry I didn't understand what you meant by HTML output. I tried to insert a relevant section of the code because the entirety of the index.html is humongous (hence why I'm trying to distribute some of its contents). – John HaTrick Nov 30 '19 at 22:10
  • Changed filename from index.html to index.php and it worked! – John HaTrick Nov 30 '19 at 22:36