Web Analytics Made Easy -
StatCounter Simple Question - CodingForum

Announcement

Collapse
No announcement yet.

Simple Question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Question

    I forgot what code to use for a dynamic page (I guess you would call it). What I'm referring to is the code that lets you have one page and have something like index.php?showpage=1a and index.php?showpage=2b show two different pages.

    Thanks in advance.

  • #2
    Your pages will have the extension .php

    At the very top of each page you'll have something like this:

    PHP Code:
    <?php
    $page
    =$_REQUEST['showpage'];
    ?>
    Then, you'll have some other pages created, or parts of them.
    You'll use PHP includes to display them based on the name.
    You could name then the same as you call them, or use some
    if statements ... whatever .... something like this.

    PHP Code:
    <?php
    if($page === "1a"){$p="firstpage.php";}
    if(
    $page === "2b"){$p="secondspage.php";}
    include 
    $p;
    ?>
    So the whole thing would look like:

    PHP Code:
    <?php
    $page
    =$_REQUEST['showpage'];
    if(
    $page === "1a"){$p="firstpage.php";}
    if(
    $page === "2b"){$p="secondpage.php";}
    include 
    $p;
    ?>

    Comment


    • #3
      Thanks. That works, but would it be possible to define it to show one page if the user goes to just index.php without a page defined?

      Comment


      • #4
        something to consider to make your life easier if you're going to be doing this with a lot of pages....

        PHP Code:
        $pages = array(
                                 
        'default'=>'index.php',
                                 
        '1a'=>'page1a.php',
                                 
        '2b'=>'page2b.php',
                                 
        '3c'=>'page3c.php'
                       
        );

        $page = isset($pages[$_GET['showpage']) ? $pages[$_GET['showpage']] : $pages['default'];

        require_once(
        $page); 
        Last edited by syosoft; Oct 2, 2006, 02:51 AM.
        Active PHP/MySQL application developer available for immediate work.
        syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

        Comment


        • #5
          cool,
          what syosoft said ...

          In the "long" method I showed,
          it would be like this:
          PHP Code:
          <?php
          $page
          =$_REQUEST['showpage'];
          $p="index.php";
          if(
          $page === "1a"){$p="firstpage.php";}
          if(
          $page === "2b"){$p="secondpage.php";}
          include 
          $p;
          ?>
          EDIT (A note for novices) ...
          You (and I) should be coding PHP like syosoft's example ...
          that is the correct method. But I sense you're sort of new
          at this? I figured I would give an example that's a bit easier for
          a novice (like myself). Whenever someone posts a snippet, like I did,
          and someone else posts a "better programming practices" snippet,
          keep a copy of both and learn from those snippets. I learn the most by
          seeing examples such as the one syosoft gave.

          Comment


          • #6
            Thanks for the compliments - I aim to help
            Active PHP/MySQL application developer available for immediate work.
            syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

            Comment


            • #7
              it's better to not use shorthand because that's harder to read, especially for novices + the code should be able to handle manipulated querystrings.
              something like
              PHP Code:
              $pages = array('1a'=>'page1a.php'
                                       
              '2b'=>'page2b.php'
                                       
              '3c'=>'page3c.php' 
                             
              ); 

              if (isset(
              $pages[$_GET['showpage']]) and array_key_exists($_GET['showpage'],  $pages)){
                require(
              $pages[$_GET['showpage']]);
              }else{
                require(
              'index.php');  

              Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

              Comment


              • #8
                When do you know if you should use "require" or use "include"?
                I'm never sure about that.

                Comment


                • #9
                  or:

                  your link page.php?page=settings.php

                  then php code
                  PHP Code:
                  $page $_GET['page'];

                  include (
                  $page); 

                  Comment


                  • #10
                    Lee ...

                    Wouldn't that allow someone to hi-jack your site with any URL
                    they wanted? They could use your site's URL to display a porn page?

                    Comment


                    • #11
                      In my opinion "include" would only be used for files that you don't care if they get included or not. "require" will cause the page to error and stop, include issues a warning.

                      Comment


                      • #12
                        Lee,

                        best remove your code because it simplyu should never be done like that. with the right settings, this would alow any page to be included over http, including PHP pages that can contain whatever code the attacker wants to run on your server.
                        Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                        Comment


                        • #13
                          More descriptive subject

                          In the future, please use a more descriptive subject when posting a question. See posting guidelines.
                          Spookster
                          CodingForum Supreme Overlord
                          All Hail Spookster

                          Comment

                          Working...
                          X