Jump to content

PHP CODE (simple online text editor that now doesn't work)


drfate

Recommended Posts

Hi all,
being a user at webfreaks for the last 2 years, their hard drive crashed a few weeks ago and now my simple PHP text editor will not work, I know they have done something on their end because it worked for the last two years. Here is the code that once worked:

It basically showed a list of text which I could edit online, nothing special but nice and quick, anyone know why this won't work anymore? The error I'm getting is:

Notice: Undefined variable: submit in /home2/impulse/public_html/admin/edit.php on line 3

Notice: Undefined variable: data in /home2/impulse/public_html/admin/edit.php on line 11

However, I never use to get this error until the server fiddled with something :( I'm at my wits end, any help would be GREATLY appreciated.

FILENAME: edit.php

<?php

if ($submit) {
$fp = fopen("data.txt", "w");
fwrite($fp, stripslashes($newdata));
fclose($fp);
}

$fp = fopen("data.txt", "r");
while (!feof($fp)) {
$data .= fgets($fp, 4096);
}
fclose($fp);

?>

<html>

<head>
<title>simple text editor</title>
</head>

<?php
$filemod = filemtime('data.txt');
$filemodtime = date("F j Y h:i:s A", $filemod);
echo "<center>This File was last updated $filemodtime</center>";
?>

</font>

<CENTER>
<form action="<? print $PHP_SELF; ?>" method="post"> 
<textarea name="newdata" rows="26" cols="40">
<?
print $data;
?>
</textarea><br>

<input type="submit" name="submit" value="Submit"></form>
These are notices and not errors per say. However, they usually indicate serious problems that should be addressed.

If register_globals are set in the php.ini file, then you can use $submit because PHP will automatically create it for you (assuming that's the name specified in your HTML form). It's best to keep register_globals off for security reasons and program accordingly. This means using $_GET or $_POST depending on your forms method (i.e. $submit = $_GET['submit']).

The $submit notice is saying that variable is not defined prior to its use on line 3. So, this can be ignored if register_globals is on since PHP will create and populate it with a value.

For the second notice, you should always initialize a variable before using the period concatenation character. Example:

$data = '';  // Initialize
$fp = fopen("data.txt", "r");
while (!feof($fp)) {
$data .= fgets($fp, 4096);  // initialize before using .=
}
fclose($fp);

Take a look at error_reporting() and display_errors php.ini settings for more info on how to stop notices.

Use isset rather using the variable on its own, ie change
if($submit)
to
if(isset($submit))

Now PHP will check whether this variable actually exits before using this variable, whereas before it wasnt and through up a notice, saying it cannot find the submit variable.

Also you'll want to use $_POST['submit'] instead of $submit if register_globals has been turned off. You'll also want to use $_POST for your other form field variables, such $newdata will need to be $_POST['newdata']

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.