Simple PHP Question

Discussion in 'OT Technology' started by doggfather, Feb 3, 2006.

  1. doggfather

    doggfather New Member

    Joined:
    Aug 6, 2005
    Messages:
    224
    Likes Received:
    0
    I want it so when someone enters a < or >, it converts it to &lt; and &gt; so people can't post html or php in the form



    <?php



    $filename = 'test.txt';
    $somecontent = $_POST[somecontent];
    $somecontent2 = "$somecontent <br />";

    if ($somecontent=="") {

    }

    else {

    if (is_writable($filename)) {


    if (!$handle = fopen($filename, 'a')) {
    echo "Cannot open file ($filename)";
    exit;
    }

    if (fwrite($handle, $somecontent2) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
    }

    echo "\"($somecontent)\" was added.";

    fclose($handle);

    } else {
    echo "The file $filename is not writable";
    }
    }
    ?>


    <form action=test.php method=post>
    <textarea rows="13" name="somecontent" cols="78"></textarea>
    <input type="submit">
    </form>
    <br /><br />
    <hr />
    <br /><br />
    <?php

    include('test.txt');

    ?>
     
  2. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    use:
    PHP:
    $string htmlspecialchars($string); //replaces special chars
     

Share This Page