General PHP Variable Questions

CrazyImpling

New Member
Reaction score
3
Okay, I'm a php newbie, and I want to make my own website for my map I'm making. I was wondering, is there a way to make a text box or something and then save it to a file in the root directory and then print variables out from the file in the root directory?
 

mase

____ ___ ____ __
Reaction score
154
Depends on how you want to save it. There is flat filing systems or database systems.

Flat File is what you're describing the most with the saving to a root directory and all so here is something that may help you.
http://www.albinoblacksheep.com/tutorial/flat
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
Okay, I'm a php newbie, and I want to make my own website for my map I'm making. I was wondering, is there a way to make a text box or something and then save it to a file in the root directory and then print variables out from the file in the root directory?

Something like this?

Depending on what you really want to save, you can serialize() / deserialize() something into the file, rather than just a form field. ALWAYS VALIDATE USER INPUT!

PHP:
<?php

# validate user input
function validateInput()
{
        if (empty($_POST['myText']))
        {
                return false;
        }

        # make thorough checks here!

        return true;
}

function saveToFile()
{
        # I'm lazy ... PHP5 only.
        return file_put_contents('save.txt', $_POST['myText']);
}

function loadFromFile()
{
        # I'm lazy ... PHP5 only.
        return file_get_contents('save.txt');
}

function displayForm()
{
?>
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
<textarea name="myText">your text goes here</textarea>
<input type="submit" />
</form>
<?php
}

# --------------------
# SCRIPT BEGINS HERE
# --------------------

$displayError = false;
$displayForm = false;

if ($_SERVER['REQUEST_METHOD'] != 'POST')
{
        $displayForm = true;
}
else
{
        if (!validateInput())
        {
                $displayError = true;
                $displayForm = true;
        }
        else if (!saveToFile())
        {
                print '<p>Input could not be saved.</p>';
        }
}

if ($displayError)
{
        print '<p>input validation failed.</p>';
}

if ($displayForm)
{
        displayForm();
}
else
{
        print '<p>' . htmlspecialchars(loadFromFile()) . '</p>';
}

?>
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top