Forum Moderators: coopster

Message Too Old, No Replies

PHP flat file --> write to variables?

a.php's input saved to b.php's variables?

         

JAB Creations

7:56 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd like to have edit.php write the form data to file.php. Specifically I'd like the values relative to the for the element names (stuff1, stuff2, stuff3, stuff4) written to the PHP variables on file.php ($stuff1, $stuff2, $stuff3, $stuff4).

I've seen plenty of tutorials online that only go so far as to dump text on to a file, that isn't helpful at all. My question is how do we target variables on the file we want to write to and then write to those variables?

- John

edit.php

<form>
<fieldset>
<input name="stuff1" value="" />
<input name="stuff2" value="" />
<input name="stuff3" value="" />
<input name="stuff4" value="" />
</fieldset>
</form>

file.php

<?php
$stuff1 = '';
$stuff2 = '';
$stuff3 = '';
$page = <<<EOD

EOD;
?>

penders

11:51 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've seen plenty of tutorials online that only go so far as to dump text on to a file, that isn't helpful at all.

IMHO this is exactly what you need to - if I have understood you correctly. You want to dump PHP code (which is just text) to a file, rather than simply changing the values in memory. As far as I'm aware there are no functions to do this for you, as there might be with an XML file for instance, to maintain the structure of your file.

Presumably you later want to include() this file to access the values?

The danger with this method is that if invalid PHP code is written then you'll get an E_PARSE error when you try and include() it and your script breaks, so be sure to be extra careful in your form validation. Consider using var_export() [uk.php.net] to get the value of your variables in PHP form - that can be written straight to a file.

So, in short, I think you need to step through your form variables ($_GET[] or $_POST[]) to actually build a string that contains your PHP code, using var_export() to get the values. And then dump this string/text to file.php in the same way you'd write any text file (fopen() / fwrite() OR file_put_contents() if PHP5).

JAB Creations

3:53 am on Apr 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've constructed most of what is needed however I am still not seeing anything useful online as far as having a POST being written to a PHP variable in a flat file database. Here is what I have thus far...

- John

index.tpl

<?php
// $_POST info should be assigned as the values to the respective variables below ($_POST['title'] goes to $title).
$title = '';
$meta_description = '';
$meta_keywords = '';
$page = <<<EOD

EOD;
?>

index.php

<?php
// User clicks anchor 'edit this page', sends referer...
if (isset($_SERVER['HTTP_REFERER'])) {$edit_page = basename($_SERVER['HTTP_REFERER']);}
else {$edit_page = basename($_SERVER['PHP_SELF']);}

// Non-Structural content is saved to TPL extension equivelent...
// Content = $title, $meta, $page (between open/close body element)
// Structure = everything else in and around.
$template = str_replace('.php','.tpl',$edit_page);
global $template;


// If POST then let's write to that template! <!-- s:) --><img src=\"{SMILIES_PATH}/icon_smile.gif\" alt=\":)\" title=\"Smile\" /><!-- s:) -->
if ($_SERVER['REQUEST_METHOD'] == "POST") {

$title = $_POST['title'];
$meta_description = $_POST['meta_description'];
$meta_keywords = $_POST['meta_keywords'];
$page = $_POST['page'];

$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($template)) {

// In our example we're opening $template in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($template, 'a')) {
echo "Cannot open file ($template)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($template)";
exit;
}

echo "Success, wrote ($somecontent) to file ($template)";

fclose($handle);

} else {
echo "The file $template is not writable";
}



} // End if request method = post

function get_title() {
// Temp echo...
echo 'we need to import the title from the variable on the flatfile';
}

function get_meta_description() {
// Temp echo...
echo 'we need to import the description from the variable on the flatfile';
}

function get_meta_keywords() {
// Temp echo...
echo 'we need to import the keywords from the variable on the flatfile';
}
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><?php get_title();?></title>
<meta name="Description" content="<?php get_meta_description();?>" />
<meta name="keywords" content="<?php get_meta_keywords();?>" />
<style type="text/css">
form fieldset {
border: #99c solid 1px;
}
h1 {
font-size: 18px;
}
input.button {
background-color: #fff;
border: #000 solid 1px;
clear: left;
cursor: pointer;
display: block;
float: left;
height: 18px;
margin: 1px;
width: 65%;
}
input.button:hover, input.button:focus, input.text:hover, input.text:focus {
background-color: #99c;
}
input.text {
border: #000 solid 1px;
display: block;
float: left;
height: 18px;
margin: 1px;
width: 50%;
}
label {
border: #000 dotted 1px;
clear: left;
display: block;
float: left;
height: 18px;
margin: 1px;
padding-left: 2px;
width: 15%;
}
textarea {
border: #000 solid 1px;
clear: left;
display: block;
float: left;
padding: 4px;
width: 65%;
}
textarea:hover, textarea:focus {
background-color: #99c;
}
</style>
</head>

<body>

<h1>Page Editor</h1>
<div>
<p>The file to be edited below is <b><?php echo $edit_page;?></b> which has it's content saved to the template file <b><?php echo $template;?></b>.</p>

<p>This page was requested via a <i><?php echo $_SERVER['REQUEST_METHOD'];?></i> request.</p>
</div>

<form action="<?php echo basename($_SERVER['PHP_SELF']);?>" method="post">
<fieldset>
<label for="title">Title</label><input class="text" id="title" name="title" tabindex="1" type="text" value="" />
<br />
<label for="meta_description">Meta Description</label><input class="text" id="meta_description" name="meta_description" tabindex="2" type="text" value="" />
<br />
<label for="meta_keywords">Meta Keywords</label><input class="text" id="meta_keywords" name="meta_keywords" tabindex="3" type="text" value="" />
<br />
<textarea id="page" name="page"></textarea>
<br />
<input class="button" type="submit" />
</fieldset>
</form>

</body>
</html>

Dave75

4:52 am on Apr 3, 2008 (gmt 0)

10+ Year Member



You might want to check out the parse_ini_file [au.php.net] function. There are some great community contributions for more involved configuration file management on the PHP website.

From there you can customise how you organise one or more ini files to your liking.

JAB Creations

6:48 am on Apr 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Dave but but some one pointed out the lack of support for an equal sign? I'd like to save XHTML content page to a single variable. Many posts there don't clarify what they actually do. That is one of the reasons I posted here. There is just too great of a disconnect between what is posted and anything that seems to lead towards my goal on that page.

- John

Dave75

3:54 pm on Apr 3, 2008 (gmt 0)

10+ Year Member



Thanks Dave but but some one pointed out the lack of support for an equal sign?

Yes, sorry I didn't look at your problem hard enough. I see you are trying to keep everything on the one file. Looking at your code, you might want to put some logic like this:

define("META_DESCRITION", "");

function get_meta_description()
{
//If post description isset
//$Description is $_POST['meta_description']
//Open this file
//Set META_DESCRITION value with preg_replace
//Write this file
//Close this file
//Otherwise
//$Description is META_DESCRITION

//Return $Description
}

But, I'm not sure about opening and writing to a file that is being executed. I don't regard it as safe coding.

My question is, are you anticipating an equals sign in the description? And, if so, why don't you convert it to an char entity if one is found?