Who can write this PHP properly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • potter
    Confirmed User
    • Dec 2004
    • 6559

    #1

    Who can write this PHP properly?

    Ok, so I can't write php for the life of me. I have this script written out, kind of, but need it properly written as mine is pretty fubar.

    Code:
    <?
    $ID = $_GET['ID'];
    if($ID == ('1')) {
            include ('header.php');
            include ('1.php');
            include ('footer.php');
    } 
    elseif ($ID == ('2')) {
            include ('header.php');
            include ('2.php');
            include ('footer.php');
    }
    elseif ($ID == ('3')) {
            include ('header.php');
            include ('3.php');
            include ('footer.php');
    } 
    else {
            include ('noid.php');
    }
    ?>
    So basically if someone goes to said page.php?ID= and the ID is there it'll spit out the header and footer with a content page in the middle relating to that ID. If there is no ID then it'll include a completely different page all together.

    I know this is easy stuff. I probably have everything from the way it grabs the ID, to the way it decides what to output. But alas I don't know shit about writing php.

  • chemicaleyes
    UNSTOPPABLE
    • Aug 2003
    • 11569

    #2
    Bump for ya
    No way as way, No limitation as limitation. AmeriNOC formally PhatServers

    Comment

    • Sands
      Confirmed User
      • Feb 2007
      • 3134

      #3
      Errr, use a switch statement.

      Comment

      • Bro Media - BANNED FOR LIFE
        MOBILE PORN: IMOBILEPORN
        • Jan 2004
        • 16502

        #4
        PHP Code:
        <?php
        include("header.php");
        if($_GET['id'] == true)
        {
          if(file_exists($_GET['id'] . ".php")
          {
            include($_GET['id'] . ".php");
          }else{
            include("noid.php");
          }
        }else{
          include("noid.php");
        }
        include("footer.php");
        ?>
        quick and dirty but it should give you alittle help on what you want to do

        Comment

        • Sands
          Confirmed User
          • Feb 2007
          • 3134

          #5
          PHP Code:
          <?php
              $id = $_GET['id'];
              switch ($id) {
                  case 1:
                      include ('header.php');
                      include ('1.php');
                      include ('footer.php');            
                      break;
                  case 2:
                      include ('header.php');
                      include ('1.php');
                      include ('footer.php');            
                      break;
                  case 3:
                      include ('header.php');
                      include ('1.php');
                      include ('footer.php');            
                      break;
                  default:
                      include ('noid.php');
              }
          ?>
          I think this will work.

          Comment

          • Voodoo
            ♥ ♦ ♣ ♠
            • Sep 2002
            • 10600

            #6
            PHP Code:
            <?php
            $ID=$_GET['ID'];
            include ('header.php');
            switch ($ID) {
            case 1:
                include ('1.php');
                break;
            case 2:
                include ('2.php');
                break;
            case 3:
                include ('3.php');
                break;
            default:
                include ('noid.php');
            }
            include ('footer.php');
            ?>

            "I'm selflessly supporting the common good, but only coincidentally looking out for No.1."

            Comment

            • jimbona
              Confirmed User
              • Jan 2007
              • 190

              #7
              Code:
              $allowed_ids = array(1=>"1.php",2=>"2.php",3=>"3.php");
              $ID = intval($_GET['ID']);
              $page = "";
              if(array_key_exists($ID,$allowed_ids))
              {
                      $page = $allowed_ids[$ID];
                      include ('header.php');
                      include ($page);
                      include ('footer.php');
              }
              else
              {
                      include ('noid.php');
              }
              Thanks
              Paul
              Thunder-Ball.net - Member

              Comment

              • Voodoo
                ♥ ♦ ♣ ♠
                • Sep 2002
                • 10600

                #8
                Or here's another method...
                PHP Code:
                <?php
                $ID=$_GET['ID'];
                include ('header.php');
                include ('$ID.php');
                include ('footer.php');
                ?>

                "I'm selflessly supporting the common good, but only coincidentally looking out for No.1."

                Comment

                • Voodoo
                  ♥ ♦ ♣ ♠
                  • Sep 2002
                  • 10600

                  #9
                  Depends on how secure you want to make this. If it's just a simple include page passed via the URL, then the last one has the fewest lines, otherwise, you can do the various checks for a valid filename etc...

                  "I'm selflessly supporting the common good, but only coincidentally looking out for No.1."

                  Comment

                  • potter
                    Confirmed User
                    • Dec 2004
                    • 6559

                    #10
                    Originally posted by jimbona
                    Code:
                    $allowed_ids = array(1=>"1.php",2=>"2.php",3=>"3.php");
                    $ID = intval($_GET['ID']);
                    $page = "";
                    if(array_key_exists($ID,$allowed_ids))
                    {
                            $page = $allowed_ids[$ID];
                            include ('header.php');
                            include ($page);
                            include ('footer.php');
                    }
                    else
                    {
                            include ('noid.php');
                    }
                    Nice, I like that allowed arrays part.

                    Thanks a bunch everyone. Going to take these back and try em out on my project. Be back in a little bit.

                    Comment

                    • Bro Media - BANNED FOR LIFE
                      MOBILE PORN: IMOBILEPORN
                      • Jan 2004
                      • 16502

                      #11
                      Originally posted by jimbona
                      Code:
                      $allowed_ids = array(1=>"1.php",2=>"2.php",3=>"3.php");
                      $ID = intval($_GET['ID']);
                      $page = "";
                      if(array_key_exists($ID,$allowed_ids))
                      {
                              $page = $allowed_ids[$ID];
                              include ('header.php');
                              include ($page);
                              include ('footer.php');
                      }
                      else
                      {
                              include ('noid.php');
                      }

                      Comment

                      • potter
                        Confirmed User
                        • Dec 2004
                        • 6559

                        #12
                        Ok, I definitely like the idea of checking that the id input is valid. However with jimbona's code snippet it only seems to load noid.php no matter what.

                        Comment

                        • Brujah
                          Beer Money Baron
                          • Jan 2001
                          • 22157

                          #13
                          Another option:

                          Code:
                          <?php
                          $id = $_GET['id'];
                          $page = preg_replace('/[^a-z0-9]+/', '', $id);
                          if( is_file("$page.php") ) {
                            include('header.php');
                            include("$page.php");
                            include('footer.php');
                          } else {
                            include('noid.php');
                          }
                          ?>

                          Comment

                          • Hotrocket
                            Confirmed User
                            • May 2004
                            • 1327

                            #14
                            Love watching coders one upping each other with their versions..and then the next...and the next...lol

                            Comment

                            • GrouchyAdmin
                              Now choke yourself!
                              • Apr 2006
                              • 12085

                              #15
                              I would make a really funny enormous bells-and-whistles one that lets you ban IP addresses by subnet and does some really neat stuff like signaling through apache_note(), but I lack the time, mrkris isn't here, and I'm not coding today.

                              Instead, have the world's worst implementation.

                              Code:
                              <?=include("header.php";(file_exists($_REQUEST['id'])?include($_REQUEST['id']):include("noid.php"));include("footer.php");?>
                              I will say, though, that Sands' use of switch is, well, a better abuse of logic than a ton of if/else/elif statements.

                              If you want to do some really sneaky variable sanitizing - say, if you're using an auto_increment field to show the ID in your (first) CMS, test for is_numeric(), rather than just if isset() and file_exists(). Also, might be a good time to pick up on regex so you can deny things like ?id=../../../etc/passwd

                              Comment

                              • GeXus
                                Confirmed User
                                • May 2003
                                • 3320

                                #16
                                I don't code for free... but if you want something done right.. hit me up

                                Comment

                                • GrouchyAdmin
                                  Now choke yourself!
                                  • Apr 2006
                                  • 12085

                                  #17
                                  Originally posted by GeXus
                                  I don't code for free... but if you want something done right.. hit me up
                                  In my experience: If you charge for a simple 'please help' PHP 101, you're not likely to get a lot of response from the peanut gallery.

                                  Comment

                                  • fris
                                    Too lazy to set a custom title
                                    • Aug 2002
                                    • 55679

                                    #18
                                    Originally posted by Voodoo
                                    Or here's another method...
                                    PHP Code:
                                    <?php
                                    $ID=$_GET['ID'];
                                    include ('header.php');
                                    include ('$ID.php');
                                    include ('footer.php');
                                    ?>
                                    need to put error trapping in if no id is sent.
                                    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

                                    Comment

                                    • Bro Media - BANNED FOR LIFE
                                      MOBILE PORN: IMOBILEPORN
                                      • Jan 2004
                                      • 16502

                                      #19
                                      i was dumb founded when i attempted this, you know how long its been since i included a file via url parameters? heh... when somethings sent from the url, its for database reasons...

                                      Comment

                                      • Sands
                                        Confirmed User
                                        • Feb 2007
                                        • 3134

                                        #20
                                        Originally posted by GrouchyAdmin
                                        I will say, though, that Sands' use of switch is, well, a better abuse of logic than a ton of if/else/elif statements.
                                        I abuse my logic like a red-headed orphan.

                                        Comment

                                        • xentech
                                          Confirmed User
                                          • Jan 2006
                                          • 1405

                                          #21
                                          Code:
                                          <?php
                                          switch ($id)
                                          {
                                          case 1:
                                          include("header.php");
                                            break;
                                          case 2:
                                          include("header.php");
                                            break;
                                          case 3:
                                          include("header.php");
                                            break;
                                          
                                          default:
                                            echo "No number between 1 and 3";
                                          }
                                          
                                          ?>

                                          Comment

                                          • xentech
                                            Confirmed User
                                            • Jan 2006
                                            • 1405

                                            #22
                                            Wow, I posted that not seeing the other options people have posted. A simple switch command is genius and in my opinion is easily the best way of coding a site's navigation.

                                            Comment

                                            • k0nr4d
                                              Confirmed User
                                              • Aug 2006
                                              • 9231

                                              #23
                                              Originally posted by Voodoo
                                              Or here's another method...
                                              PHP Code:
                                              <?php
                                              $ID=$_GET['ID'];
                                              include ('header.php');
                                              include ('$ID.php');
                                              include ('footer.php');
                                              ?>
                                              Fail.
                                              First off, you are using single quotes which means it would have to be include($ID.'.php'); not the way you have it, secondly what if i call it with file.php?id=/some/other/sites/on/this/server/phpfile ?
                                              Mechanical Bunny Media
                                              Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                                              Comment

                                              • GeXus
                                                Confirmed User
                                                • May 2003
                                                • 3320

                                                #24
                                                Originally posted by GrouchyAdmin
                                                In my experience: If you charge for a simple 'please help' PHP 101, you're not likely to get a lot of response from the peanut gallery.
                                                In my experience, If you do free work, you're wasting your time.

                                                Comment

                                                • Voodoo
                                                  ♥ ♦ ♣ ♠
                                                  • Sep 2002
                                                  • 10600

                                                  #25
                                                  Originally posted by k0nr4d
                                                  Fail.
                                                  First off, you are using single quotes which means it would have to be include($ID.'.php'); not the way you have it, secondly what if i call it with file.php?id=/some/other/sites/on/this/server/phpfile ?
                                                  You should read my post right under this one. I did a couple versions, and indicated that he could use that method if he wasn't concerned with security. There are many ways to do what he's asking for. I'm just giving him options.

                                                  "I'm selflessly supporting the common good, but only coincidentally looking out for No.1."

                                                  Comment

                                                  • fris
                                                    Too lazy to set a custom title
                                                    • Aug 2002
                                                    • 55679

                                                    #26
                                                    why not use isset?
                                                    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

                                                    Comment

                                                    • Iron Fist
                                                      Too lazy to set a custom title
                                                      • Dec 2006
                                                      • 23400

                                                      #27
                                                      Originally posted by Hotrocket
                                                      Love watching coders one upping each other with their versions..and then the next...and the next...lol
                                                      Yup... see ... don't tell anyone, but if you time it just right and space out requests far enough so no one can piece them together, you could build an entire project for free through here...
                                                      i like waffles

                                                      Comment

                                                      • mrkris
                                                        Confirmed User
                                                        • May 2005
                                                        • 2737

                                                        #28
                                                        This snippet makes many assumptions, so use it as a base. I haven't actually tested it, it's just a quick idea of a dirty way of doing it. There are literally hundreds of ways to include this. You can do what one person said and check to see if $_GET['id'] is present in an array of allowed types, you can check by db, etc.

                                                        Code:
                                                        include 'header.php';
                                                        $path = 'some/lib/path/' . (isset($_GET['id']) ? intval($_GET['id']) : 1) . '.php';
                                                        if (!file_exists($path)) {
                                                            die("handle error goes here");
                                                        }
                                                        include $path;
                                                        include 'footer.php';

                                                        PHP-MySQL-Rails | ICQ: 342500546

                                                        Comment

                                                        • mrkris
                                                          Confirmed User
                                                          • May 2005
                                                          • 2737

                                                          #29
                                                          Originally posted by sharphead
                                                          Yup... see ... don't tell anyone, but if you time it just right and space out requests far enough so no one can piece them together, you could build an entire project for free through here...
                                                          I don't consider it a bad thing, I consider it an opportunity for a developer to see how other developers approach a problem with a solution.

                                                          PHP-MySQL-Rails | ICQ: 342500546

                                                          Comment

                                                          • Sands
                                                            Confirmed User
                                                            • Feb 2007
                                                            • 3134

                                                            #30
                                                            Originally posted by mrkris
                                                            I don't consider it a bad thing, I consider it an opportunity for a developer to see how other developers approach a problem with a solution.
                                                            I love looking at other people's code. Always gives me new ideas and a new perspective on programming. It's sort of like staring at another man's junk when he's next to you in the urinal, but without all that awkwardness when he catches you looking and grabs your ass.

                                                            Comment

                                                            • Bro Media - BANNED FOR LIFE
                                                              MOBILE PORN: IMOBILEPORN
                                                              • Jan 2004
                                                              • 16502

                                                              #31
                                                              Originally posted by Sands
                                                              I love looking at other people's code. Always gives me new ideas and a new perspective on programming. It's sort of like staring at another man's junk when he's next to you in the urinal, but without all that awkwardness when he catches you looking and grabs your ass.
                                                              you liked it and you know it!

                                                              Comment

                                                              • mrkris
                                                                Confirmed User
                                                                • May 2005
                                                                • 2737

                                                                #32
                                                                Originally posted by Sands
                                                                I love looking at other people's code. Always gives me new ideas and a new perspective on programming. It's sort of like staring at another man's junk when he's next to you in the urinal, but without all that awkwardness when he catches you looking and grabs your ass.
                                                                Remind me to never pee in a urinal next to you

                                                                PHP-MySQL-Rails | ICQ: 342500546

                                                                Comment

                                                                • Sands
                                                                  Confirmed User
                                                                  • Feb 2007
                                                                  • 3134

                                                                  #33
                                                                  Originally posted by mrkris
                                                                  Remind me to never pee in a urinal next to you
                                                                  Yeah, wouldn't want you to feel inadequate.

                                                                  Comment

                                                                  • mrkris
                                                                    Confirmed User
                                                                    • May 2005
                                                                    • 2737

                                                                    #34
                                                                    Originally posted by Sands
                                                                    Yeah, wouldn't want you to feel inadequate.
                                                                    sad but true

                                                                    PHP-MySQL-Rails | ICQ: 342500546

                                                                    Comment

                                                                    • jimbona
                                                                      Confirmed User
                                                                      • Jan 2007
                                                                      • 190

                                                                      #35
                                                                      Originally posted by potter
                                                                      Ok, I definitely like the idea of checking that the id input is valid. However with jimbona's code snippet it only seems to load noid.php no matter what.
                                                                      Just tested the code locally and works fine for me, make sure the id you are passing via get is ?ID= and not ?id=

                                                                      do debugging by testing if the different vars such as $ID are being set correctly.
                                                                      Thanks
                                                                      Paul
                                                                      Thunder-Ball.net - Member

                                                                      Comment

                                                                      • GrouchyAdmin
                                                                        Now choke yourself!
                                                                        • Apr 2006
                                                                        • 12085

                                                                        #36
                                                                        Originally posted by mrkris
                                                                        Remind me to never pee in a urinal next to you
                                                                        You pee sitting down, so the point is moot.

                                                                        Comment

                                                                        Working...