Results 1 to 9 of 9

Thread: Simple php skinning

  1. #1
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Simple php skinning

    I figured it would be easy to find a tutorial for this, but since I'm not sure exactly what to search for, I haven't found what I need.

    Normally when I make a new site layout, I make one php page for each section.

    index.php, news.php, aboutus.php, etc

    Each file contains it's own content, and uses includes to pull in the menu, header, footer, etc.

    But what I want to do instead, is pretty much the opposite. I want to make a single index.php file, with my layout, meunu, header, footer, etc, and have an html file be called for the contents. So going to

    index.php?about.html

    will pull the content from about.html into the index file, and

    index.php?folder/somefile.html

    would do the same.

    I know this would seem very simple to most people, but I am extremely new to php, and really have no idea how this is done. Thanks in advance for any help given.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Like this?:
    Code:
    <title>Page!</title>
    <?php
      if(file_exists($_GET['ext'])){
        include($_GET['ext']);
      } else {
       die ("You were directed to a non existing page!");
      }
    ?>
    Then:
    index.php?ext=about.html
    Last edited by Nile; 03-15-2009 at 09:03 PM.
    Jeremy | jfein.net

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Like that, yes, but without the gaping security hole: index.php?ext=/etc/passwd

    Code:
    <?php
      $page = urldecode($_SERVER['QUERY_STRING']);
    
      if ($page[0] === '/'
          || $pagestrpos($page, '../') !== false
          || !file_exists($page)) {
        header('404 File Not Found');
        die('This page does not exist!');
      }
    
      require_once sprintf('templates/%s.inc.php', $page);
    ?>
    Going to index.php?about will load includes/about.inc.php.

    Note that this code is secure for *nix operating systems only. If you're running on something weirder, say so: it probably needs different checks.

    URLs like this are a prime candidate for rewriting with mod_rewrite to get /yourapp/about instead of /yourapp/index.php?about or /yourapp/?about.
    Last edited by Twey; 03-15-2009 at 09:00 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. The Following User Says Thank You to Twey For This Useful Post:

    zhono (03-16-2009)

  5. #4
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    This allows you to include anything in the current and sub directorys, excluding the current file (no infinate includes)
    PHP Code:
    <?php
    $request 
    = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING']:'';
    if(
    $request == ''){
        
    $request 'default.html'//The page to include
    }
    $include_path realpath($request);
    $self_dir dirname(__FILE__);
    if(
    substr($include_path0strlen($self_dir)) == $self_dir){
        if(
    file_exists($request) && $include_path != __FILE__){
            include 
    $request;
            die;
        }
    }
    echo 
    'Error in including file';
    (I only tested this on Windows. Let me know if you have problems on a non-Windows OS)

  6. #5
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Ah, yes. Exactly what I needed. Thanks guys.

    Now one other question. How would I simply assign the value to a variable to use on the page. So if the url is

    index.php?image=pic1

    then have it create a variable $image and set it's value to pic1 so I can do

    Code:
    <img src="images/<? $image ?>.jpg">

  7. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Code:
    <?php
    echo "<img src=\"images/{$_GET['image']}.jpg\">;
    ?>
    Jeremy | jfein.net

  8. #7
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    With that, all I get is:

    Parse error: syntax error, unexpected $end in /path/to/file/index.php on line 3

  9. #8
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    He meant:
    PHP Code:
    <?php
    echo '<img src="images/' $_GET['image'] . '.jpg">';
    ?>

  10. The Following User Says Thank You to techietim For This Useful Post:

    zhono (03-16-2009)

  11. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    But he should have meant:
    Code:
    <img src="images/<?php echo $_GET['image']; ?>.jpg">
    If you ever find yourself echoing HTML, you're probably doing it wrong. It seriously harms readability and performance.

    Code:
    $request = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING']:'';
    That's unnecessary — $_SERVER['QUERY_STRING'] will always be set, although it may be empty.

    I like what you've done with realpath(), though.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •