using variable in include?

Discussion in 'PHP' started by mokimofiki, Sep 28, 2009.

  1. #1
    Can anyone tell me whats wrong with the following code:

    include("restaurantsignup.php?restaurantname=".$_POST[restaurantname]."");



    it says the page doesn't exist but if I do:

    include("restaurantsignup.php");

    it works fine?

    EDIT: btw the $_POST variable shows up fine when echoed and the restautantname is a variable on the restaurant.php page.
     
    mokimofiki, Sep 28, 2009 IP
  2. 321nixin123

    321nixin123 Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Shouldn't:
    $_POST[restaurantname]
    be
    $_POST['restaurantname']
    with quotes inside the brackets?
     
    321nixin123, Sep 28, 2009 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Either way thats not working ... this is the error

    Warning: include(restaurantsignup.php?restaurantname=Mofiki's Indian) [function.include]: failed to open stream: No such file or directory in /home/regretthecurve/verify_restreg.php
     
    mokimofiki, Sep 28, 2009 IP
  4. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    variables don't pass through includes

    if you're including a file the variables will already be defined... not through POST though... if you defined $restaurant you can call the variable in $restaurantsignup.php
     
    Last edited: Sep 28, 2009
    orionoreo, Sep 28, 2009 IP
  5. venetsian

    venetsian Well-Known Member

    Messages:
    1,105
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    168
    #5
    Yup. Variables can't be assigned inside include statement. You must pre-set the variable and global it inside the included php file. That's the only way.

    Cheers,
    Venetsian.
     
    venetsian, Sep 28, 2009 IP
  6. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What you could do using the $_POST['restaurantname'] and set its value to a session. Declare the session into a variable in the include and then unset the session variable.

    ie:

    first page
    
    session_start();
    $m = $_POST['restaurantname'];
    $_SESSION['restaurantname'] = $m;
    include("restaurantsignup.php");
    
    PHP:
    restaurantsignup.php
    
    session_start();
    $name = $_SESSION['restaurantname'];
    unset($_SESSION['restaurantname']);
    
    PHP:
     
    SHOwnsYou, Sep 28, 2009 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    You really don't have to do that. All variables in the parent page are global to all included pages.
     
    ThePHPMaster, Sep 28, 2009 IP
  8. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #8
    100% correct.

    Furthermore, using the question mark (reg.php?var=value) makes sense for a browser only. It's the GET method of HTTP protocol.
     
    caprichoso, Sep 28, 2009 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Hah, yeah. Include works via the filesystem, not via HTTP get... you cannot pass data that way... Funny someone would even try (am I that far removed now?)

    Generally in my includes I use the security practice that no library file should ever output anything directly - which means everything in that include should be wrapped in a function... Which would be your solution and also mean you don't have to use globals. Likewise I tend to include them with require_once just to prevent accidental double-load.

    
    require_once('restaurantsignup.php');
    restaurantsignup($_POST[restaurantname]);
    
    Code (markup):
    Of course, you shouldn't have to pass that particular value in the first place, since $_POST is already a superglobal... In any case that would take a rewrite of your restaurantsignup.php to work... Said rewrite being doing it the 'right way' instead of the 'half-assed way'.

    Since globals are the half-assed way. Of course, that you are handling superglobals without sanitizing ;)
     
    deathshadow, Sep 28, 2009 IP