Best way of creating php files with php?

Hey guys, whats the best way of creating PHP files using php?
I want to create a new file for each article for example, how to do that?
I want the file to be created using PHP because the content in the file might change later on .

The file is something like that…

static header
Changeable content (taken from a database)
static footer

thanks for the help.

Errr…why do that? Just pull content from the DB depending on the URL the user supplies…


$pageID = $_GET['pageid'];
# get page content from DB.


You only need one file that way.

I had it working that way but i’m using many APIs such as facebook send button which doesnt accept page parameters, eg if u got a page called getarticles.php?ID=5 it will ignore everything after the ‘?’, making it useless…

You can’t do this with Post?

I mean, what you ask certainly is possible, it’s just generally probably not a good idea if it can be avoided through any other means.

I know its horrible (facebook’s fault) but i dont have any other solution to do that…
get is not a solution because they dont accept page parameters
post is not a solution either because then all articles will be under 1 page that will have the same name always, like articles.php … (correct me if im wrong?)

You can still send post parameters to that assuming at any point you’re submitting a form, you could always make your form action submit to itself and the form submission be an <a> link.

that will still not make my API work as the send button (for example) accepts a link only, so it will still get articles.php and have no clue to which article to point.

Look into URL rewriting.

i read this article:
But i don’t excactly know what apache is, and how to work with it on my hosting…?

dont think that my hosting has apache…not listed on phpinfo() at least…

I think ill have to stick with my suggestion of creating pages, whats the negetivites it has besides of webspace wasting?

and can some1 guide me on the best way to do that ? thanks…

You’re probably running Apache. If you aren’t, you’re likely running nginx. If you are running anything else, I recommend changing hosts. =p

Both of those are web servers, which are what make it possible for your pages to be a website. It runs under PHP.

With Apache, you just create a file named .htaccess and put it in the root of your site. With nginx, you have to put it somewhere a little different, but it’s still pretty similar.

I’ll ask my hosting what i got, i dont see either of these when running phpinfo() function…

To answer your original question (even though getting the urls to work is probably better), do the same thing you would to generate an html page. Have a template with something like:

// some_demo.php.tremplate
// Here is a php function that will end up in my generated php file
function some_demo()
    $a = "<?php echo $aPassed; ?>";
    echo $a;

And then use a basic template processing approach:

// Here I am with my generator

// Want to pass this to the generated file
$aPassed = 'some demo info';

// Pull in the template
include 'demo.php.template';
$body = ob_end_clean();

// And store it with a php tag
" . $body);

This is also handy for generating caches file of one sort or another.

Well i checked with my hosting and they said they do have url rewritting with a .htaccess file
When i tested something like this:

RewriteEngine on
RewriteRule ^test\\.html$ test.php [L]

It worked

But when i’m trying something like the following (taken from an example) it doesn’t work…:

RewriteEngine on
RewriteRule ^([0-9]+)$ getpets.php?id=$1 [L]

What i wanna do is something like, changing getpets.php?id=11111 to

Can someone give me the correct code to do such a thing?

You want something like:

RewriteEngine on
RewriteRule ^pets/([0-9]+)/?$ getpets.php?id=$1 [L]

thank you so much it works :slight_smile:

And thanks everyone for the suggestions & help