php include problem

Discussion in 'PHP' started by smatts9, Jul 18, 2006.

  1. #1
    I have a page 1.php?id=4 and in the 1.php page I set
    $id = $_REQUEST['id'];
    In the included page, 2.php I want to be able to use $id also. I have tried everything from even putting a query string in the included file, and still nothing works.
     
    smatts9, Jul 18, 2006 IP
  2. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, its kind of tough to tell from your message what exactly you're trying to do. Plus, I'm pretty new around here, just trying to give back to a community that helps me a bunch.

    Could you tell us if you're getting an error message or what?

    As far as I know, something like this should work for page 1.php:

    
    <?
    
    //code for page 1
    
    $id = $_REQUEST['id'];
    
    include("2.php");
    
    ?>
    
    PHP:
    If you place the include after you create the variable, it should work. Also, I think this should work to:

    
    <?
    
    //code for page 1
    
    $id = $_REQUEST['id'];
    
    include("2.php?id=$id");
    
    ?>
    
    PHP:
    I'm not completely sure about this, but I think I'm right, maybe someone with a little more experience could chime in...
     
    ip076, Jul 18, 2006 IP
  3. fsmedia

    fsmedia Prominent Member

    Messages:
    5,163
    Likes Received:
    262
    Best Answers:
    0
    Trophy Points:
    390
    #3
    try $id = $_GET['id']

    that should probably work better.
     
    fsmedia, Jul 18, 2006 IP
  4. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #4
    I get no error, nothin appears because it is just some php code. This is a page that includes 1 of 2 pages, one will show when logged in another will show when logged out...

    I have tried both of these:

    
    $id = blah;
    include(2.php?id=$id);
    
    PHP:
    
    id = $blah;
    include(2.php?id={$id});
    
    PHP:
    I have just trying including the page, and just going ahead and using $id within 2.php, but still nothing??...

    fsmedia, that did not work, the files being included are in another directory, while the page calling on the included ones is in the root? if that makes any difference..?
     
    smatts9, Jul 18, 2006 IP
  5. fsmedia

    fsmedia Prominent Member

    Messages:
    5,163
    Likes Received:
    262
    Best Answers:
    0
    Trophy Points:
    390
    #5
    To make sure you're getting the right variable, you may try
    <?php print_r($_REQUEST['id']); ?>
    Code (markup):
     
    fsmedia, Jul 18, 2006 IP
  6. livingearth

    livingearth Well-Known Member

    Messages:
    1,469
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    140
    #6
    I am not certain about your case, but I recently solved a similar problem by changing the extension of the included file to .inc. Seems the php file attempts to render itself as html before being included. Whereas the .inc file passes the php code to the first document as is. I am not certain if this was due to an unusual server configuration on my end or if it is default but it worked for me...

    Please let me know if this helps.
     
    livingearth, Jul 18, 2006 IP
  7. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #7
    Thanks for your help, I accidently goofed up on some variable name when querying the db and that's why it kept returning blank :rolleyes:
     
    smatts9, Jul 18, 2006 IP
  8. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #8
    In 1.php it should just be: include("2.php"); without "?id=$id".

    Then in 2.php you can access id the same way as in 1.php: $_GET['id']; or $_REQUEST['id'];.
     
    phper, Jul 18, 2006 IP
  9. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Just wanted to say that it is not safe to use extensions like .INC etc. unless you have restricted access to them using .HTACCESS or something. Otherwise, anyone can view the source of that include file by just viewing it in a browser.

    Just my $0.02

    Thomas
     
    coderlinks, Jul 21, 2006 IP
  10. Koncept

    Koncept Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    That does not look like php.
     
    Koncept, Jul 21, 2006 IP
  11. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #11
    When using PHP include, you CANNOT pass arguments with ? . This is because the included PHP files are not called through HTTP but the code in them is placed in the place of the include call, in the source code. So it should be possible to access the $id variable directly from within 2.php . Uless 2.php contains only functions. In that case, you will have to declare $id as global in each function
    
    <?php
    // 2.php
    function foobar1()
    {
         global $id;
    // blah blah...
    }
    function foobar2()
    {
         global $id;
    // blih blih...
    }
    ?>
    
    PHP:
    Hope that made sense.
    Thomas
     
    coderlinks, Jul 22, 2006 IP
  12. haentz

    haentz Peon

    Messages:
    78
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The $id variable should be availible in the included PHP-File as well. Try the following code.

    $id = $_REQUEST['id'];
    
    // check if the variable is really set:
    echo ("The id: ".$id);
    
    include("2.php");
    
    //or:
    
    require ("2.php");
    HTML:
     
    haentz, Jul 23, 2006 IP