SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help: Access Variables From Included PHP File?

    Hi,
    I have a situation where I need to access a variable defined in a php file from a different php file where I include the firts php file using full web URL.

    The code is something like this:

    PHP Code:
    <?php 
    error_reporting
    (0); 
    include 
    "http://mydomain.com/myphpfile.php"
    ?>
    <?
    echo $TheVariableINeed;
    ?>
    In this, $TheVariableINeed is the variable that I define in the http://mydomain.com/myphpfile.php";

    If I include the http://mydomain.com/myphpfile.php"; as just
    include myphpfile.php;

    ..it works but if I include it as a URL it does't access the TheVariableINeed.

    How can I do that? Any help is appreciated!

    Thanks,

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Including files as URL is a bad bad thing. Stay away from it! I don’t' see why would you want to do that when you can access the file 'myphpfile.php' directly.

    Also, it's glaringly obvious that when you can include a file as a HTTP resource (http://mydomain.com/myphpfile.php), myphpfile.php is no longer your average PHP file; it's just a bunch of html/css/js. The variables won't be imported when you include them.

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Kailash,
    I understand that its a bad thing but I have a specific requirement where my page, where I want to include the php file, resides on a different server and domain.

    Is there an alternative way to capture the variables from the php file that is included - in the page that has the include statement?

    Thanks,
    Deepak

  4. #4
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there an alternative way to capture the variables from the php file that is included
    Is that file on the remote server, on different domain? And do you have control over that server (control in the sense you can change the files). Also, are you in position to program the remote servver send variable/data in alternative formats like CSV, XML or something?

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That page is going to be parsed by the remote server before sending it to you -- you'll get its output, not its contents. If you want different behavior you need to explicitly configure the remote web server to not parse that file as PHP code and send it as text only instead. That leaves your code open to public viewing if someone gets the URL.

    Including remote code with include() is very unusual.

  6. #6
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman
    That page is going to be parsed by the remote server before sending it to you -- you'll get its output, not its contents. If you want different behavior you need to explicitly configure the remote web server to not parse that file as PHP code and send it as text only instead. That leaves your code open to public viewing if someone gets the URL.

    Including remote code with include() is very unusual.
    Exactly.
    So the first thing I would suggest is to do away with the current design of your app that requires you to include a remote code (if possible). If that is not possible at all, you can write the remote file to directly output PHP code instead of HTML. You'll capture the PHP code and subsequently use something like eval() to run the code. This is going to be security nightmare though. You can add some safety measures to the mix (like the remote file giving out the PHP code only if supplied with a correct cipher/password).

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Look up Web Services.
    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
  •