Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unhappy Help! PHP code to display .HTML pages..

    Hey, I haven't worked on PHP and HTML for a while, so I am trying to get back into it and make a website..

    What PHP code do I have to use to make it so that I can make links on my index.php page that people can click, and the content on those .html pages.. will be displayed in index.php layout page..

    I have done this before, but I totally forgot.. the links were similar to '?id=aboutme.html' .... that would basically put the content of .html in my index.php page.. however.. when you first visit the index.php page... it will have a default page (such as news or other content already being displayed.. How do I do that?

    I want my news to be displayed by default.. and then.. have links to the .html pages.. my news is located at '/public_html/new/news/news.php' ... so how will I implement that by default.. and then also have the functionality of having '?id=' pages.. ?

    Thanks in advance!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    PHP Code:

    <?php

    // set the default page
    $page="/public_html/new/news/news.php";

    // if there is an incoming URL variable, let's use that instead ...
    if(isset($_GET['id'])){
    $page=$_GET['id'];
    }

    // You have to be careful about this ...
    // Basically, you are allowing the user to put in any file (or script) they choose.

    // Later on, if you "include" that file, you would be allowing the user to display/execute any file,
    // including such things as "config.php" ... that's a bad breach of security.
    // But, I wanted to answer your question as you stated.
    ?>

    <html>
    <body>
    The rest of your page here.<br />
    <br />
    Now, we can include the content you wanted:<br />
    <?php include($page)?>
    <br />
    </body>
    </html>

  • Users who have thanked mlseim for this post:

    arslan89 (12-07-2010)

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That was awesome! Yeah it worked great. I see that you mentioned a security flaw in that code... is there any way to prevent that? I mean, how else would I be able to have ONE layout page (index.php) and have multiple pages be displayed within that layout page? I thought everyone followed this method ?

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    You put your content either in a MySQL database, with id numbers for "articles and content",
    or put them into their own text files with names like p12.html, and p23.html

    You then use an id code like: id=23

    Check to make sure id is an integer only,
    and then put it together to form your filename:
    PHP Code:
    <?php

    // set the default page
    $page="/public_html/new/news/news.php";

    // if there is an incoming URL variable, let's use that instead ...
    if(isset($_GET['id'])){
    $id=$_GET['id'];
       if(
    is_numeric($id)){
       
    $page="p".$id.".html";
       }
    }
    ?>

    <html>
    <body>
    The rest of your page here.<br />
    <br />
    Now, we can include the content you wanted:<br />
    <?php include($page)?>
    <br />
    </body>
    </html>


    .

  • #5
    New Coder
    Join Date
    Aug 2009
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hey, thanks so much for your help. I did that and it worked great. Thank you.


  •  

    Posting Permissions

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