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 6 of 6

Thread: PHP GET id help

  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP GET id help

    PHP Code:
    <?php
    if ($_GET['id'] == 'page.php')
    include_once(
    'page.php');
    ?>
    I have been searching for how to use this code or codes like this for about an hour I am a bit frustrated because I am not sure what I am looking for. Basically I want this format for linking:

    index.php?id=page1.html

    Does that make sense? I realize I am completely new at this sort of thing and I would greatly appreciate any help.

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    It makes sense though it's rather dangerous to include things based simply on what is passed in the URL. Change what you have to read:
    PHP Code:
    <?php
    if ($_GET['id'] == 'page1.html')
    include_once(
    'page1.html');
    ?>
    You could also use something like:

    PHP Code:
    <?php
    if ($_GET['id'] == 'page1')
    include_once(
    'page1.html');
    ?>
    and call it using: index.php?id=page1
    Dave .... HostMonster for all of your hosting needs

  • #3
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php $id $HTTP_GET_VARS['id']; if ( !$id || $id == "" ) else { include "$id"; } ?>
    This is another version of the code that I have found that will not work for me.

    I tried what you gave me, but how would use that code for multiple pages? I want the layout to be the same and the content to change. Page 2 Page 3 etc...

    Many Thanks
    Last edited by mugenINFINITY; 07-22-2008 at 09:53 PM.

  • #4
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by mugenINFINITY View Post
    PHP Code:
    <?php $id $HTTP_GET_VARS['id']; if ( !$id || $id == "" ) else { include "$id"; } ?>
    This is another version of the code that I have found that will not work for me.

    I tried what you gave me, but how would use that code for multiple pages? I want the layout to be the same and the content to change. Page 2 Page 3 etc...

    Many Thanks
    That is for PHP 4 and lower I think, so you'd have to replace $HTTP_GET_VARS with $_GET.

  • #5
    p@p
    p@p is offline
    New Coder
    Join Date
    Apr 2010
    Location
    Indonesia
    Posts
    71
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Or could be like this:

    PHP Code:
    <?php
    $phpself 
    $_SERVER['PHP_SELF'];
    switch(
    $_GET['id']) {
    case 
    '1':
    // This could be include, echo or what ever
    include ("page1.php");
    break;
    case 
    '2':
    // This could be include, echo or what ever
    include("page2.php"); 
    break;
    case 
    '3';
    // This could be include, echo or what ever
    include("page3.php");
    break;
    }
    ?>
    Then you called yourdomain.com/index.php?id=1 for page1.php

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by mugenINFINITY View Post
    PHP Code:
    <?php $id $HTTP_GET_VARS['id']; if ( !$id || $id == "" ) else { include "$id"; } ?>
    This is another version of the code that I have found that will not work for me.

    I tried what you gave me, but how would use that code for multiple pages? I want the layout to be the same and the content to change. Page 2 Page 3 etc...

    Many Thanks
    Quote Originally Posted by PeaTearGriffin View Post
    That is for PHP 4 and lower I think, so you'd have to replace $HTTP_GET_VARS with $_GET.
    Yes and no; most configurations still allow the usage of registered_long_arrays ($HTTP_*_VARS) except for raw which is still valid. There are a few things to note on these though:
    1. As mentioned they are old. Really old. Superglobals were added in 4.1. That like... december 2001ish? Has it been that long already?
    2. Registered long arrays are not superglobals. In fact, when 4.1 was new we used to develop similar to so for maximum compatibility:
      PHP Code:
      if (PHP_VERSION 4.1)
      {
          
      $_POST = &$HTTP_POST_VARS;
          
      $_GET = &$HTTP_GET_VARS;
          ...
      }

      function 
      myfunc()
      {
          global 
      $_POST;

      The biggest flaw of course that registered_long_arrays are NOT superglobals and you couldn't create your own. This is why we needed to globalize the superglobals across functions. I'm so glad those days are gone.
    3. Registered_long_arrays are officially deprecated as of 5.3.0, and we're hoping they will be gone in 6.0. That means there is no guarantee that any future release will include them. Current versions may disable the long arrays, but they are enabled by default.
    4. Registered_long_arrays are not related to the superglobals. They draw the same data, but they are not written as references. Any changes in one will not effect the other during the scripts life.


    Anyway, I'm the lazy one here so I'd probably do the same like so:
    PHP Code:
    <?php
    $page 
    $_GET['page'];
    $aAllowed = array('page1''page2''page3');
    if (
    in_array($page$aAllowed))
    {
        
    $page .= '.php';
        if (
    file_exists($page))
        {
            require_once 
    $page;
        }
    }
    The only thing I'd probably do is make sure that the provided page doesn't include any /'s in it.

    When I write my own stuff I have a controller who passes all requests through filters. So mine is somewhat similar to the integer approach by p@p where the filters make determinations of what to do, and these are dynamically generated so it doesn't have the actual switch.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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