dynamically generating webpages with php

  • dhoyin
  • Graduate
  • Graduate
  • dhoyin
  • Posts: 126
  • Loc: west africa

Post 3+ Months Ago

hi,
please can anyone show me how i can generate webpages dynamically with php.when a user on my site sends in a post, instead of it going to the database, it want a webpage to be generated to that content.can anybody help me with this.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

That is easy. I do this all the time when I create an installer for someone, so you should understand this pretty easily.

Here is the basics for it.

You will need to give permissions to the folder that would store all that created pages. I'm not sure what the permission thing would look like, but I'm sure you would/could figure it out.

Here is an example of such thing in action.

PHP Code: [ Select ]
<?php
  function create_page() {
    // page name
    $filename = "main/{$id}-{$name}.php";
    // Creating the file (If doesn't exist...)
    if($handle = fopen($filename, 'w+'))
    {
      // The contents that would be writtin to the file
      $contents = "<?php
\$name = $name;
?>
<html>
<head>
<title>{$id} - {$name}</title>
</head>
<body>
User ID: {$id}
Name: {$name}
Other info: {$other}
</body>
</html>
";
      // Writing the contents into the file and checking if it succeeded
      if(file_put_contents($filename, $contents))
      {
        // The contents were successfully written into the file...
        echo "Page created";
        $this->finish;
      } else {
        // There was a problem writing the contents into the file
        // Give the appropriate text and kill the script
        die("<p>There was an error while creating the user file.</p>\n");
      }
      // Closing the file
      fclose($handle);
    }
  }
?>
  1. <?php
  2.   function create_page() {
  3.     // page name
  4.     $filename = "main/{$id}-{$name}.php";
  5.     // Creating the file (If doesn't exist...)
  6.     if($handle = fopen($filename, 'w+'))
  7.     {
  8.       // The contents that would be writtin to the file
  9.       $contents = "<?php
  10. \$name = $name;
  11. ?>
  12. <html>
  13. <head>
  14. <title>{$id} - {$name}</title>
  15. </head>
  16. <body>
  17. User ID: {$id}
  18. Name: {$name}
  19. Other info: {$other}
  20. </body>
  21. </html>
  22. ";
  23.       // Writing the contents into the file and checking if it succeeded
  24.       if(file_put_contents($filename, $contents))
  25.       {
  26.         // The contents were successfully written into the file...
  27.         echo "Page created";
  28.         $this->finish;
  29.       } else {
  30.         // There was a problem writing the contents into the file
  31.         // Give the appropriate text and kill the script
  32.         die("<p>There was an error while creating the user file.</p>\n");
  33.       }
  34.       // Closing the file
  35.       fclose($handle);
  36.     }
  37.   }
  38. ?>


I didn't test that piece of code, but I'm sure that it should work. All I did was copy this from a working example, changed the $contents variable and the results. I also changed the file name to be (or to seem to be) dynamic.

I think that code is pretty much self-explanatory. If you don't understand something ask and I will try to help :)

I put that php code in the example to show that you can do that. At least I do that to store values like in the example...
  • dhoyin
  • Graduate
  • Graduate
  • dhoyin
  • Posts: 126
  • Loc: west africa

Post 3+ Months Ago

guy thanks, you are just too much !!!!!!!!!!!!!!!!!!!!!!!!!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Your welcome. Just post here if you have any questions regarding that code or this topic :)
  • dhoyin
  • Graduate
  • Graduate
  • dhoyin
  • Posts: 126
  • Loc: west africa

Post 3+ Months Ago

well the code just worked fine with no error. but i still have a question. how can i run a block of php code on a .htm page. i tried it but the php section did not display the result. though normally the i know i should save it as .php but for some reason i need the pages in .htm yet i wish to run php scripts on them. so can that be done.
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

Either you need the files to be .php or you need to have access to .htaccess or httpd.conf.

Truce wrote:
You can set files with a .htm or .html extension to be interpreted by PHP in your htaccess file.

Code: [ Select ]
AddType application/x-httpd-php .html .htm

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 77 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.