0

I'm a bit lost for I'm "green" in PHP.

Please, may you teach me how to fix this:

on 'POST' --> Replace a specified array key from a file:

(WRONG:)

<?php
    $newData = $_POST["sendData"]; 

    if(isset($_POST['sendData'])){

        $file = fopen('fileToOpen.php', 'a');

        foreach($file as $key => $val) 
        {
            $data[$key] = explode("|", $val);
        }

        for($k = 0; $k < sizeof($file); $k++)
        {
            unset($data[$k][3]);
        }

        $data[$k][3] = "$newData";
        fwrite($file, $data[$k][3]);
        fclose ($file);

    }
?>

That's wrong as it continues to write:

data1|data2|data3|oldDatanewData

instead of rewrite:

data1|data2|data3|newData

Is there any other technique to achieve something similar? Perhaps with file_put_contents? Am I missing implode?

Thanks!

13
  • 2
    First thought: Writing user-submitted data to a .php file is just ASKING to get hacked. Commented Feb 16, 2011 at 22:08
  • 1
    fopen returns a resource and resources cannot be used with foreach. Raise your error_reporting! Commented Feb 16, 2011 at 22:10
  • @jnpcl Thanks for your concern, That IS an Admin option for I've written a secured login with md5 and so on... This is an admin choice that has to be written to a fl.fi. database, to be lately reused. :) Commented Feb 16, 2011 at 22:13
  • @roXon: Even so, it's probably smarter to use a different file extension, and block the file from non-script access. Scenario: Your Admin login cookie gets hijacked, and even though your server/hosting/ftp login is different, the attacker now has a way to write to a .php file on your server. Commented Feb 16, 2011 at 22:16
  • @jnpcl Interesting. I'm low about hijacking. But I have created a temporary file on the server. On admin LOGIN (if the entered username and password are correct) the admin IP is stored (md5) into a file. So no cookies are set. Is that a good way? Commented Feb 16, 2011 at 22:25

2 Answers 2

2

Dunno what are you asking for but perhaps you only need to serialize and unserialize the array.

$data_array = unserialize(file_get_contents('fileToOpen.php'));
$data_array[$key_you_want_to_change] = $new_data;
file_put_contents('fileToOpen.php', serialize($data_array));
Sign up to request clarification or add additional context in comments.

5 Comments

I don't think the submitter's file is in serialize() format, but the file_get_contents/file_put_contents part is right.
@aaz & @marines Correct, I want to explode the line by '|' into keys. Now found the 3th key : replace it (OVERWRITE IT) with posted data!
I assume roXon will save serialized array in the file before reading it. :)
Good point. @roXon, serialize()/unserialize() is more robust if you can pick the format. If not, just replace that with implode('|', ...)/explode('|', ...).
@aaz @marines It works!!! with explode('|') / implode('|', ...) like you said! Thanks you all! I was doing it wrong: $data_array[3] = explode( file_get_contents('|', $fileName)); CORRECT: $data_array[3] = explode('|', file_get_contents($fileName)); !!! :)
0

$newData = $_POST['sendData'];

 if(isset($_POST['sendData'])){

 $file = "fileToOpen.php";

$oldData = file_get_contents($file);

$oldData = eregi_replace("\n","",$oldData);

$FullDataArray = explode("?",$oldData); $oldDataArray = explode("|",$FullDataArray[1]);

$oldDataArray[3] = $newData;

$newDataString .= "

foreach($oldDataArray as $key=>$val) {

$newDataString .= $val;

if($key!="3") {

$newDataString .= "|";
}

}

$fh = fopen($file, 'w');

fwrite($fh,$newDataString);

fclose($fh);

}

?>

1 Comment

This erase the whole line from the file. :( I need only the 3th arr to overwrite. :| I'm trYing to adapt the code above but still unsuccessfully.

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.