View Full Version : undefined variable problem

09-05-2007, 12:58 AM
i've came up with the following script for an 'easy' write to file from form page.

i've done it a few times before with success, but for some strange reason this time i'm having a bit of trouble with the variable 'page' coming up undefined. It's probably something really simple but i can't seem to be able to spot it.

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

$file = fopen('db/pages/home.php', 'a', 1);
fwrite($file, $page);

else {
$lines = file('db/pages/home.php');
$l_count = count($lines);
for($x = 0; $x< $l_count; $x++)
echo "<form method=\"post\" name=\"update\" enctype=\"multipart/form-data\" action=\"?page=admin&mode=edit\">";
echo "<textarea name=\"page\" cols=\"80\" rows=\"8\">".$lines[5]."</textarea>";
echo "<input type=\"Submit\" name=\"update\" value=\"Update\"></form>";}


Any help would be greatly appreciated, thanks.

09-05-2007, 01:12 AM
If I had to guess I would say the "register_globals" PHP flag has been disabled on your PHP server. This "register_globals", when it's turned on, automatically assigns all the $_POST array elements (and $_GET elements) to non-array variables (i.e. the value in $_POST['page'] is assigned to $page). It's been an infamous security hole in PHP and because of that it defaults to "off" in PHP versions 4.3+.

To be safe and cross-server operational you should just use the $_POST['page'] variable, or manually assign $_POST['page'] to $page (and other form variables) at the beginning of your script.

09-05-2007, 03:43 AM
register_globals, agreed. A form input or url name/value pair called 'page' is registering as the var $page when register_globals = On.

If it's a post, then:

$page = (!empty($_POST['page'])) ? $_POST['page'] : NULL;

will fix it, else:

$page = (!empty($_GET['page'])) ? $_GET['page'] : NULL;

09-05-2007, 03:59 PM
thanks guy, that's done the trick, now just another thing...

would i be able to get '$page' to only write to a specific line in '$file'?

tried using the same kinda way from the read script but can't quite get it, it comes up with 'fwrite(): supplied argument is not a valid stream resource in...'

again, help would be greatly appreciated.


09-05-2007, 04:42 PM
You have to open the file in read/write mode and read each line in until you get to the line you want to overwrite and then write it.