Saving to static page


After reading through this forum you are probably going to think I’m thick, I probably am but I can make an excellant cuppa tea.

I am trying to save the contents from a form on one page into a seperate page ie



<? somename = ""; // Contents of text box on Page_1.php stored here ?>

Can somebody please help because its driving me crazy!!!


Have you taken a look at Remote Files in the PHP Manual yet?


If by store you mean use the value of somename on the next page you will need to use $_POST[’’] this is an array of all the form elements along with their respective values.

– PAGE1.php –

<form action="page2.php" method="POST">
<input type="text" name="somename" size="20">
<input type="submit" name="send" value="Go">

– PAGE2.php –
$somename = $_POST[‘somename’];

echo $somename;




Thanks for your replies but I think my post may have been a bit vague. I am trying to make a static config file which will hold information. ie email addresses, sql info, contact name, link information. This is in fact page 2 in my example. I do not want this page to load.

Page1 is the actual page where the information is entered and viewed. So pasically in all other pages all i have to do is

<?php require(config.php); ?>

then for an example the page title will be

<?php echo $title?>

IPage 2 is just to store the information, page 1 is to view and edit it.


Ok, so you actually want to physically store the data into the file…

I will advise the use of fopen(), fread(), and fwrite().

You will probably have to some serious string manipulation, but it can be done.


Thanks for your reply

I am now getting somewhere but not quite where i want to be.

I am inserting Page 2 at the start of page1.php

<?php require("2.php"); ?>

I have then added the following between the body tags

<?php $file = fopen ("2.php", "w"); ?> <?php

fclose ($file);

It is actuall writing data to page2.php but it seems to be creating page2.php and adding the information i put in the text fileds.

This is how page2.php is set up

<? $siteName = ""; $siteURL=""; ?>

I need the data adding between the “”. any suggestions how this can be done


I have

fclose ($file);


fclose ($file);

above ?>

I tried

fclose ($file);

but returned an error


Someone may have a better way, but how I would do is completely rewrite the file everytime. So basically once you hit submit on the form come up with exactly how the file should look:

$contents = “<?php n”;
$contents .= “$siteName=”" . $siteName . ““n”;
$contents .= “$siteUrl=”” . $siteURL . "“n”;
$contents .= “?>”;

You may know already, but the n is a new line when view in notepad or some other text editor and the " is to escape out the double quote so it will actually put the double quotes in the file.


Tried that, it didnt work. Return on page2.php

<? text = "text"; ="test"; ?>

which resulted in a parce error


Ok, first off… as you can see in my sig… Code isn’t meant to be working code… Just a guide and a starting point. I guess I half expected that you would have came back with… This didn’t work so I tried this and still didn’t work… Lets just try and put some effort next time into a problem.

here is the working one that I worked on before you even posted and it took me a couple of tries to get it right… A little something called trial and error…


$contents = “<?php n”;
$contents .= ‘$siteName="’ . $siteName . ‘"’ . “n”;
$contents .= ‘$siteUrl="’ . $siteUrl . ‘"’ . “n”;
$contents .= “?>”;


This is a little cleaner and with the use of single and double quotes there is no need for the escaping of the double quote. When you place something in single quotes it will use exactly what is put in there. Where as in double quotes php will still use the value of the variable.


Hi thanks for your reply. I didnt mean anything by my last post, I just put down my thoughts. I am gett somewhere now, here is where I am


<?php $file = fopen ("2.php", "w"); $contents = "<" . "? $siteName = "siteName"; $siteURL = "siteURL"; ?".">"; ?> <? fwrite($file, $contents, strlen($contents));

fclose ($file);



<? $siteName = "siteName"; $siteURL = "siteURL"; ?>

When I add the $ to siteName and siteURL page2.php reads

<? $siteName = ""; $siteURL = ""; ?>

I am nearly there but then again :evil:


I shouldn’t do this, but I bored and normally would have just put what you need to do and not exactly is to be done, but I am in a good mood and getting keg today so here goes… remember the SIG! :)

Of course that is what you are getting as you are writting the content before you actually have the variables set with the correct value…

– Page 1 –
if(!isset($_POST[‘Submit’])) // If the submit variable has not been checked do this
?> // Remember its easier to jump in and out of php rather than running html through the php parser.

<? } else { $siteName = $_POST['siteName']; $siteURL = $_POST['siteURL']; //---- I know this will write correctly as long as the variables are being passed and set correctly. $contents = "<?php n"; $contents .= '$siteName="' . $siteName . '"' . "n"; $contents .= '$siteURL="' . $siteURL . '"' . "n"; $contents .= "?>";

$file = “testWrite.php”;
$handler = fopen($file, “w”);
fwrite($handler, $contents);



You may also want some sort of notification that your file has been succesfully created (or not, in case of failure). Dollarsigns have to be escaped within doublequotes too, if you don’t want them parsed.
On a sidenote, jumping in and out of php blocks vs. having html parsed/echoed/printed is a matter of taste :wink:


A big thank you to everyone who contributed to this thread especially Ragstar00. I have managed to get it sorted thanks to your help. I realise the code isn’t the same as yours but your contributuins gave me the ideas to get this far. Well here it is

<?php if($_POST['siteName']) $siteName = $_POST['siteName']; if($_POST['siteURL']) $siteURL = $_POST['siteURL']; $file = fopen ("2.php", "w"); $contents = "<" . "? $siteName = "$siteName"; $siteURL = "$siteURL"; ?".">"; ?> <?php

fwrite($file, $contents, strlen($contents));

fclose ($file);


Once again thanks



Since you’re using user input as though it were actual PHP code, you might want to pay special attention to WHAT is being input. You wouldn’t want someone to find out about this script and input harmful code (which would then be executed).


I am not actually using the file like this, this is an example/help of how to get in working. Users will not have access to it only administrators But thanks for your comments they’re appreciated