Creating webpage on form submit?
How is it possible to allow a user to create a webpage containing some html, based on their entries in a form? ie. I would want them to be able to input a nam开发者_如何学Goe and when the button is clicked, a webpage called that name would be created. I imagine that this must be possible in php, but what functions/code would I be using?
Thank you!
First of all, I would ask myself if this really what I need.
Why don't you just create a basic template page and pass the data as a parameter?
Something like:
user.php?name=joachim
If you still want to do that, you could use the PHP functions for opening, writing and closing files:
- http://php.net/manual/en/function.fopen.php
- http://php.net/manual/en/function.fwrite.php
- http://php.net/manual/en/function.fclose.php
Here's a tutorial on how to create a file in php:
http://www.tizag.com/phpT/filecreate.php
This can pose big security risks in the first place, anyways, suppose form is submitted with html text, you can go about something like this:
$handle = fopen('file_name.html', 'r+');
// write to file
fwrite($handle, $_POST['fieldname']);
close($handle);
Note: You got to consider XSS and other security issues because you are allowing users to create pages. Bad guys can exploit it easily.
Resoures:
PHP File Handling Functions
XSS
Finally, a must read PHP security guide:
http://phpsec.org/projects/guide/
Idea: Just allow them minimal of customization and use a pre-made page where you could insert this user data. Ofcourse as said, you need to sanitize the user-submitted data and consider security issues.
I would just have a page that acts as a template that populates based on the fields you mention, unless you actually need a physical file.
The template makes changes and security much easier.
UPDATE: Basically you can either have your form submit to a page that prints those POSTed fields to the appropriate spots or post to a database and then pull to the page.
You'd need to extract the entries of the form from the $_GET or $_POST arrays, and then use file writing functions such as fwrite to create a file. Theres an example on how to do a simple write here.
Excluding the fact that it is a security risk to allow users to create any files on the host...
You can create a file via fopen
(), fwrite()
and fclose()
or using file_put_contents()
. I prefer using some kind of template file which you load first, then add the user input values in it and then save the whole string via fwrite()
or file_put_contents()
. You get the user inputs from the $_GET
or $_POST
array, depending on the method you used in your form.
By saying "create a webpage containing some html, based on their entries in a form" I'm going to assume ( yeah yeah I know) that what you really mean is to determine the contents of a page by the entries from the form.
Gonna try and explain this as basically as possible, because I'm not completely sure I understand your intent.
When you write the form, you will have an tag that contains action="example.php". This means that it will perform the actions on example.php
In example.php, you can then retrieve the variables passed to it from the form by looking within your $_POST[] array variable. Then, you can change the structure of example.php or your result page by using the variables in php to determine what you print.
I don't think generating a physical file would be necessary, and since you're using a form to get the information in, $_GET is kind of pointless (I'd guess in most cases).
精彩评论