0

I'm teaching myself PHP from tutorials, and am trying to save some object properties to a php file, as persistant data, so I can later retrieve them in another user session. (Yesi, I know how to save to a database, but for this simple case I want to learn how to save &retrieve from a webform, writing it to another php file.) I'd like the form pre-populated with whatever the value of the data object’s property was when last saved. My code so far:

<?php require "objects.php";  ?>

$firstname = $id1->firstname

<form name="EditObject" method="post" action="objects.php" >
 <input type="text" name="firstname" id="firstname" value = "<?php echo $firstname; ?> ">
<br>
    <input type="submit" name="submit" value="Submit">
     </form>

AND the objects.php file:

class Users {   public $firstname = ''; }
$id1 = new Users();  
if ( isset( $_POST['submit'] ) ) {
      $firstname = $_REQUEST['firstname'];
      $id1->firstname = $firstname;  
          echo 'data updated';        echo  '<br>';
      echo  $id1->firstname;      echo  '<br>';
}
  • https://www.php.net/manual/en/function.file-put-contents.php should help you on that path. – Gavin Simpson Mar 21 '21 at 06:26
  • Implement the [serializable interface](https://www.php.net/manual/en/class.serializable). – Markus Zeller Mar 21 '21 at 08:54
  • @ Markus Zeller: I think the Serializable is for writing to other types of files, like txt, json, xml, etc. I'm wanting to write just to other .php files. Or am I wrong about serializable? – user2671645 Mar 21 '21 at 22:03
  • @Gavin Simpson: this looks interesting, but it overwrites the entire file, or appends. I want to just update a property of an existing object in that file. – user2671645 Mar 21 '21 at 22:08
  • UPDATE: There are a couple of old CMS that save all their content data to php files as arrays. And they are fast and secure. Modern Object->properties should be faster & better, but if no one knows how to do this, then I'd accept saving content in arrays in php files. But I'm still looking for some tutorials or links or code direction on how to do that. – user2671645 Mar 21 '21 at 22:18

0 Answers0