SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Errr...why do that? Just pull content from the DB depending on the URL the user supplies...
    PHP Code:
    ...header...

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

    ...footer... 
    You only need one file that way.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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...

  4. #4
    SitePoint Enthusiast Belsnickle's Avatar
    Join Date
    Feb 2012
    Location
    Chico, CA
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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?)

  6. #6
    SitePoint Enthusiast Belsnickle's Avatar
    Join Date
    Feb 2012
    Location
    Chico, CA
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Look into URL rewriting.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hey,
    i read this article: http://www.cyberdesignz.com/blog/web...url-rewriting/
    But i don't excactly know what apache is, and how to work with it on my hosting..?

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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..

  11. #11
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    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.

  12. #12
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'll ask my hosting what i got, i dont see either of these when running phpinfo() function...

  13. #13
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    663
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    // 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:
    PHP Code:
    // Here I am with my generator

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

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

    // And store it with a php tag
    file_put_contents('generated.php',"<?\n" . $body);
    This is also handy for generating caches file of one sort or another.

  14. #14
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well i checked with my hosting and they said they do have url rewritting with a .htaccess file
    When i tested something like this:
    Code:
    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..:
    Code:
    RewriteEngine on
    RewriteRule ^([0-9]+)$ getpets.php?id=$1 [L]
    What i wanna do is something like, changing getpets.php?id=11111 to example.com/pets/11111

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

  15. #15
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    You want something like:
    Code:
    RewriteEngine on
    RewriteRule ^pets/([0-9]+)/?$ getpets.php?id=$1 [L]

  16. #16
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank you so much it works

    And thanks everyone for the suggestions & help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •