0

I'm new to PHP and seem to have ran into a problem I can't seem to get around.

I have a form on a secure page that creates a PHP file to store a text value. I named this variable $text.

The Form HTML Code:

<form action="upload_title.php" method="post">
  <label for="text">Title 1:</label>
  <input type="text" name="text" id="text"><br>
  <input type="submit" name="submit" value="Submit">
</form>

The upload_title.php then seems to store the text input as $text in filename.php:

<?php
  $var_str = var_export($_POST['text'], true);
  $var = "<?php\n\n\$text = $var_str;\n\n?>";
  file_put_contents('filename.php', $var);
?>

This seems to be functional as the form will generate filename.php, below is an example if I typed Store into the form input and submitted on the webpage.

<?php
  $text = 'Store';
?>

Now the issue I'm encountering is not being able to retrieve this stored as a attribute in separate html document, the index.html in my case.

This was my best approach to changing the title attribute of an image:

<a href="upload/1.jpg">
  <img src="upload/thumb.jpg" title="<?php include 'filename.php'; echo htmlspecialchars($text); ?>" alt="" class="image0">
</a>

This does not work, but I can see my JQuery detects that this is trying to be populated but does not extract the data from filename.php on the `index.htm' page.

Thank those in advance for your advice and insight, it is sincerely appreciated.

5
  • 1
    Instead of storing in a file store it in a SESSION variable that you have access to throughout your application. PHP Sessions Commented Sep 29, 2014 at 19:27
  • Why are you using PHP as storage?! That's not quite what it's for, and there are far better ways. Commented Sep 29, 2014 at 19:27
  • 1
    You will do yourself an enormous service by learning just the basics of AJAX and using that. Don't be intimidated -- it's much easier than you may think. See: stackoverflow.com/questions/26092158/… Commented Sep 29, 2014 at 19:28
  • Thanks for the quick response @JayBlanchard ! I'm not too sure that this would be applicable to what I'm going for as the changes made are not just in a user session, but actually being referenced by the public html of the index. I'm going for using the form input on a secure page to be loaded to a PHP file that will affect the public html. Commented Sep 29, 2014 at 19:35
  • Thanks @Biffen! I'm definitely not going for anything crazy technical, it's just to change an attribute that's being used by my JQuery that can be changed by a user using a a form input. Is there no way to get the $text value to be referenced from the filename.php in the index? Commented Sep 29, 2014 at 19:44

1 Answer 1

2

Your issue is probably the fact that you are using an html file instead of a php file, in this case index.html.

Your server is likely not set up by default to process .html files as php so the php does not get executed.

Apart from that it is not a very good way to store your value as when the php does get executed, you introduce a security risk and you use a lot more storage than necessary. You'd better store the value in a database or text file.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the feedback @jeroen ! The value is really not a huge risk to the site, nor would I care even if it was compromised or befell greater storage usage. Is there no way to amend a HTML page to run PHP and retrieve the $text value from the external PHP?
@NickolasBarakso Until someone sends in something like ''; exec('rm -fr /');
I do understand that i'm leaving a risk but it is just for a small site that I'm pretty certain hackers or the general public will more than likely never see. Excuse my ignorance but I would assume that bit of code clears my local site or something? Even if this happened, I can back it up. Would you say that there is another way to alter an html attribute coming from a data base, but wouldn't that use php too? Would changing the .htaccess allow me to run php within html?
@NickolasBarakso Just store the value and use file_get_contents() to get it back. No php needed for the file with the value.
Thanks @jeroen, I'm tinkering with it now to no avail, how, if it is possible to implement the attribute in the html with the file_get_contents() in the html without PHP? Isn't the file_get_contets() a PHP function? How would I mark the title attribute in the html to retrieve the value using this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.