PHP Script If Statement Pages

Discussion in 'PHP' started by Jake-Johnson, Aug 16, 2009.

  1. #1
    Easily create pages like /index.php?action=home, /index.php?action=about, or any other pages you'd like.

    
    <?php
    
       // set up php get action
       $page = $_GET['action'];
    
          // set up php home on index.php?action=home
          if($page == 'home'){
    
               // if page is home content displays
               echo 'This is home page';
    
          // set up php about on index.php?action=about
          } elseif($page == 'about'){
    
               // if page is about content displays
               echo 'This is about page';
    
          // if page is normal.. on index.php
          } else {
    
              // display following content displays
              echo 'Some random content on main page';
    
          // end if statement..
          }
    
    ?>
    
    Code (markup):
    To change the /?action=home, change the first string I set up... like so:

    
    $page = $_GET['[color=red]action[/color]'];
    
    Code (markup):
    for all of the pages, just change each one of these:

    
    } elseif($page == '[color=red]about[/color]'){
    
    Code (markup):
    I recommend including files instead of echoing text.. like this:

    
    } elseif($page == 'about'){
    // if page is about content displays
    include('[color=red]page.php[/color]');
    
    Code (markup):
     
    Last edited: Aug 16, 2009
    Jake-Johnson, Aug 16, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Or simply:

    
    $function = 'display_' . $_GET['action'];
    
    if (function_exists($function)) {
        $function();
    }
    
    function display_home() {
    
    }
    
    function display_about() {
       
    }
    
    PHP:
    This allows you to simply add a simple function instead of having to do the if else structure.
     
    premiumscripts, Aug 16, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    To iterate that (and what is an ifelse?) Something like this tends to work better.

    
    $page = isset($_REQUEST["page"])?str_replace(".", "", $_REQUEST["page"]):"home";
    
    //the reason for the replacement of . is to prevent hack attemps like page=../../ etc
    
    if(!file_exists(realpath("pages/".$page.".php"))) { echo "Unable to load requested page"; }
    else { include('pages/'.$page.'.php'); }
    
    PHP:
    including a file is fine, but make sure you secure the input then. (and I've always prefered switch() { } statements over large blocks of if-then-else)
     
    kblessinggr, Aug 16, 2009 IP
  4. Jake-Johnson

    Jake-Johnson Peon

    Messages:
    839
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    My mistakes. Else if. :)

    I am fairly new to PHP. A function would indeed work better/easier. :D
     
    Jake-Johnson, Aug 16, 2009 IP
  5. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Regardless if you use his or my method, make sure you sanitize your inputs.
     
    kblessinggr, Aug 16, 2009 IP