[php] - howto update variable in external php file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • martinsc
    Too lazy to set a custom title
    • Jun 2005
    • 27047

    #1

    [php] - howto update variable in external php file

    i need some quick php help...
    let's say i have a file called x.php
    with a few variables in it
    Code:
    $a = "2";
    $b = "ddd";
    and a file called y.php with
    Code:
    include 'x.php';
    how could i get y.php to update the variable value in x.php?

    thanks
    Make Money
  • gornyhuy
    Chafed.
    • May 2002
    • 18041

    #2
    format c:

    icq:159548293

    Comment

    • Dirty F
      Too lazy to set a custom title
      • Jul 2001
      • 59204

      #3

      Comment

      • martinsc
        Too lazy to set a custom title
        • Jun 2005
        • 27047

        #4
        thanks for the great help...
        Make Money

        Comment

        • RobbieRye
          Registered User
          • Oct 2005
          • 616

          #5
          If you are declaring constants such as "$a = "2";" then the only way to change it from another script would be to either redeclare it in y.php or re-write x.php using a file write.

          There may be other options, but without a more in depth look at your complete script, it's difficult to form them.

          Comment

          • martinsc
            Too lazy to set a custom title
            • Jun 2005
            • 27047

            #6
            Thanks Robbie.
            i'll look into file write.
            Make Money

            Comment

            • grumpy
              Too lazy to set a custom title
              • Jan 2002
              • 9870

              #7
              do you want to update the var in the file?
              Don't let greediness blur your vision | You gotta let some shit slide
              icq - 441-456-888

              Comment

              • who
                So Fucking Banned
                • Aug 2003
                • 19593

                #8
                Originally posted by Franck

                Comment

                • martinsc
                  Too lazy to set a custom title
                  • Jun 2005
                  • 27047

                  #9
                  Originally posted by grumpy
                  do you want to update the var in the file?

                  yes.......
                  Make Money

                  Comment

                  • grumpy
                    Too lazy to set a custom title
                    • Jan 2002
                    • 9870

                    #10
                    Originally posted by martinsc
                    yes.......

                    then the only thing you can do is rewrite file

                    easiest thing is every var on a line like
                    2
                    3
                    4

                    then
                    $filename = "vars.txt";
                    $my_vars = file($filename);

                    $my_vars[0] will be 2

                    if you have changed the vars

                    just save the array.
                    Don't let greediness blur your vision | You gotta let some shit slide
                    icq - 441-456-888

                    Comment

                    • broke
                      Confirmed User
                      • Aug 2003
                      • 4501

                      #11
                      You need to open and read the file and then rewrite the contents. Something like this (has not been tested)...

                      PHP Code:
                      $new_value = "6";
                      $new_file = "";
                      
                      $handle = @fopen("y.php", "r");
                      if ($handle) {
                         while (!feof($handle)) {
                             $buffer = fgets($handle, 4096);
                             
                             if(stristr($buffer,'$a' !hahahaha FALSE)){
                                $new_file .= "$a = ".$new_value."\"";
                             } else {
                                $new_file .= $buffer;
                      
                         }
                         fclose($handle);
                      }
                      
                      $handle = @fopen("y.php", "w");
                      if($handle){
                         fwrite($handle,$new_file);
                      } 
                      
                      Perfect Gonzo

                      Comment

                      • martinsc
                        Too lazy to set a custom title
                        • Jun 2005
                        • 27047

                        #12
                        thanks a lot guys.
                        i think i'll go with grumpy's idea...
                        Make Money

                        Comment

                        Working...