Jump to content

[SOLVED] fwriting through input fields


bloodgoat

Recommended Posts

Excuse me, I'm extremely drowsy as it is (was at work all night, haven't slept yet). I'm trying to recreate an old script based purely off of foggy memory (it was from... oh, I don't know, 5-6 years ago, before I stopped scripting). As you can imagine, it's proving quite difficult for me to remember. I still understand the theory behind it but I'm getting boned when executing it.

 

Basically: You have an index file chalk full of input fields. This fields all pull variables from a central variable file. You can freely edit these fields and fwrite to change their value. Simple enough, right? Well, shed some help. Just something I jotted up in the last 10-15 minutes... here are my three files:

 

INDEX.PHP

<?php

include("vars.php");

$template = <<< html
<form action="write.php" method="post">
<input type="text" value="$varone" name="input1"><br>
<input type="text" value="$vartwo" name="input2">
<input type="submit" value="Make Changes">
html;

echo $template;

?>

 

VARS.PHP

<?php 

$varone = "variable one";
$vartwo = "variable two";

?>

 

WRITE.PHP

<?php

include("index.php");

$array_vars = array(
			"$varone = ".$_POST['input1']."",
			"$vartwo = ".$_POST['input2'].""
);

foreach($array_vars as $var){
$write .= $var.";\n";
}

$handle = fopen("vars.php", "w+");
fwrite($handle, "<?php\n\n");
fwrite($handle, "$write");
fwrite($handle, "\n?>");
fclose($handle);

echo "<meta http-equiv=\"refresh\" content=\"0;index.php\">";

?>

My problem: let's say I change the value in the first input field to "num one" and the value in the second input field to "num two", and then hit submit. My vars.php turns to this:

<?php

variable one = num one;
variable two = num two;

?>

Nevermind the fact the variables don't maintain their quotations (which is a huge problem in itself), but the previous value of the variable is then made the new value of the variable (except it's not even technically a variable because it doesn't have a $). Halp plz

Link to comment
https://forums.phpfreaks.com/topic/150618-solved-fwriting-through-input-fields/
Share on other sites

<?php

include("index.php");

$write = "\$varone = '{$_POST['input1']}\n\$vartwo = '{$_POST['input2']}'";

$handle = fopen("vars.php", "w+");
fwrite($handle, "<?php\n\n");
fwrite($handle, $write);
fwrite($handle, "\n?>");
fclose($handle);

echo "<meta http-equiv=\"refresh\" content=\"0;index.php\">";

?>

I'll try this, and thank you for the new code, but for the benefit of my own learning experience, can you explain what you did and why?

 

I can tell I was putting my backslashes in the wrong spot, you totally refreshed my memory in that regard, but this time around I was taking a new approach to the actual writing. Is it not possible with a foreach() loop through an array? Or is it just not as feasible?

Its pretty simple. There was no need for any loop.

 

$write = "\$varone = '{$_POST['input1']}\n\$vartwo = '{$_POST['input2']}'";

 

Backslashes can be used to escape chars. So we use a backslash before the $ so that php know its part of the string (and won't try to interpolate it as a variable. As for the rest, complext variables (ie arrays) should be within {} when being interpolated in a string, and I simply wrapped single quotes around the parts that need it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.