SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast owentech's Avatar
    Join Date
    Apr 2006
    Location
    Nairobi, Kenya
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Using Variable in Included Page

    Hi,
    I have a file called incshowresults.php that has:
    Code:
    <?php
    if ((!$_GET[pollchoiceid]) AND (!$_GET[pollid]) AND (!$pollid)){
        die ("Error: Please select a Poll<br><br><a href=\"pollvote.php\">Click here to try again</a>");
    }
    //..other stuff..
    ?>
    If I use the following or include the file from the same folder, it works:

    Code:
    <?php
    $pollid=2;
    include ('demo/incshowresults.php');
    ?>
    But If I try to include from an entirely different folder as shown below, it does not work, and I am told to select a poll:
    ($pollid seems to be empty after including the file).

    Code:
    <?php
    $pollid=2;
    include ('http://localhost/myfolder/demo/incshowresults.php');
    ?>
    However, the following DOES work:
    Code:
    <?php
    $pollid =2;
    include ("http://localhost/myfolder/demo/incshowresults.php?pollid=$pollid");
    ?>
    Why does the second setup not work?

    Abe
    Life is too short to think small - John Mason
    What is any life if not the pursuit of a dream? - Vanilla Sky
    PHP Membership Script

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's because you use an url to include a file. If you do that, you get only the output of the script and not the source as you'd expect. Use a file system path:
    PHP Code:
    include ('/path/to/myfolder/demo/incshowresults.php'); 
    Both cases are wrong to do, but what makes the second to work is that incshowresults.php works on it's own if passed the pollid param -- and you do pass it this case as opposed to the first.
    Saul

  3. #3
    SitePoint Enthusiast owentech's Avatar
    Join Date
    Apr 2006
    Location
    Nairobi, Kenya
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    It's because you use an url to include a file. If you do that, you get only the output of the script and not the source as you'd expect. Use a file system path:
    PHP Code:
    include ('/path/to/myfolder/demo/incshowresults.php'); 
    Both cases are wrong to do, but what makes the second to work is that incshowresults.php works on it's own if passed the pollid param -- and you do pass it this case as opposed to the first.
    I think you misunderstood me.
    The second one (similar to the one you have posted) does NOT work.
    PHP Code:
    <?php
    $pollid
    =2;
    include (
    'http://localhost/myfolder/demo/incshowresults.php');
    //this does NOT work
    ?>
    Apparently it does not receive $pollid.

    Abe
    Life is too short to think small - John Mason
    What is any life if not the pursuit of a dream? - Vanilla Sky
    PHP Membership Script

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes it doesn't receive it. It's all about the path. As I said, if you use an url, you'll get the output (go with your browser to the url and you'll see what php sees too). You can only pass varialble though url query. But it's not the way we work with includes.

    If you look closer, I posted a path which is local server path, not url. That provides php the *source* code as opposed the *output* you get from url.

    Just use a local path -- either absolute or relative -- that's how the includes work.
    Saul


Bookmarks

Posting Permissions

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