Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #2172 > unrolled thread

use GET in include

Started bycerr <ron.eggler@gmail.com>
First post2011-06-14 21:09 -0700
Last post2011-06-16 07:14 -0400
Articles 7 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  use GET in include cerr <ron.eggler@gmail.com> - 2011-06-14 21:09 -0700
    Re: use GET in include Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> - 2011-06-15 10:04 +0200
      Re: use GET in include Denis McMahon <denis.m.f.mcmahon@gmail.com> - 2011-06-15 12:01 +0000
        Re: use GET in include Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> - 2011-06-15 15:21 +0200
          Re: use GET in include Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-15 12:02 -0400
      Re: use GET in include cerr <ron.eggler@gmail.com> - 2011-06-15 21:58 -0700
        Re: use GET in include Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-16 07:14 -0400

#2172 — use GET in include

Fromcerr <ron.eggler@gmail.com>
Date2011-06-14 21:09 -0700
Subjectuse GET in include
Message-ID<57655471-4b66-45db-abcf-e9194758655c@22g2000prx.googlegroups.com>
Hi  There, I would like to pass variables with GET to a file i include
in my source file but it doesn't work, I get something like
"Warning: include(./getfile.php?dir=headers11) [function.include]:
failed to open stream: No such file or directory in /mnt/stor5-wc2-
dfw1/479383/493261/www.quaaoutlodge.com/web/content/themes/Quaaout11-
dev/photoshuffler.js.php on line 29"
But when I remove the "?dir=" part from the url it works just fine.
What would that be?
Line 29 looks like:
blablabla<?php include "./getfile.php?dir=".$_GET['dir'];?>

Thank you for hints & suggestions!

Ron

[toc] | [next] | [standalone]


#2175

FromErwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com>
Date2011-06-15 10:04 +0200
Message-ID<4df8676c$0$49180$e4fe514c@news.xs4all.nl>
In reply to#2172
On 6/15/2011 6:09 AM, cerr wrote:
> Hi  There, I would like to pass variables with GET to a file i include
> in my source file but it doesn't work, I get something like
> "Warning: include(./getfile.php?dir=headers11) [function.include]:
> failed to open stream: No such file or directory in /mnt/stor5-wc2-
> dfw1/479383/493261/www.quaaoutlodge.com/web/content/themes/Quaaout11-
> dev/photoshuffler.js.php on line 29"
> But when I remove the "?dir=" part from the url it works just fine.
> What would that be?
> Line 29 looks like:
> blablabla<?php include "./getfile.php?dir=".$_GET['dir'];?>
>
> Thank you for hints&  suggestions!
>
> Ron


Hi Ron,

You are confusing a http request with an include.
They are different beasts.
A http request look like:
http://www.example.com/yourphpfile.php
and you can add info to an URL by adding name/value pairs, like this:
http://www.example.com/yourphpfile.php?name=ron&favcol=blue

In $_GET you will find 2 keys:
$_GET["name"] will contain ron
$_GET["favcol"] will contain blue


Now to the include:
If you include a file, you include a file. :-)
No http or apache involved. So no GET superglobal is set. (And that is 
the reason things don't work as you expected.)

Unless you use http-wrappers, like
include ("http://www.example.com/getfile.php?file=...");

WARNING: the above is REALLY poor programming to get information into 
your include file: you make a trip using  the http-protocol/apache/new 
PHP instance/etc to get the info into your included file).

How to solve this?
Depending on your situation, in general:
1) Make the included file contain a function or an class, and use that 
function or class from your main file.
Something like (for Object):
require_once ("class_myFileFetcher.php");
$myFileFetcher = new FileFetcher();
$myFileFetcher->setFile(..);
$myFileFetcher->setHearder(..);
$myFileFetcher->stream(..);

The above is just fantasycode of course.

Or with a function:
require_once ("myfunctions/myFileFetcherFunction.php");
fetchFileAndStream("/some/path/some/file");

option 2 (ugly in my opinion):
Simply set a variable, then the include. Let the code in the include use 
that variable.
So:
$theUglyWayFileName = "/some/path/some/file";
require_once ("myFileFetcher.php");
And let the code in myFileFetcher.php use the variable named:
$theUglyWayFileName.

I advise you to both avoid http-wrappers and also to avoid option 2.
Go with a function or a class.


Good luck.

Regards,
Erwin Moller

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#2183

FromDenis McMahon <denis.m.f.mcmahon@gmail.com>
Date2011-06-15 12:01 +0000
Message-ID<4df89f17$0$8527$bed64819@gradwell.net>
In reply to#2175
On Wed, 15 Jun 2011 10:04:08 +0200, Erwin Moller wrote:

> On 6/15/2011 6:09 AM, cerr wrote:
>> Hi  There, I would like to pass variables with GET to a file i include
>> in my source file but it doesn't work, I get something like "Warning:
>> include(./getfile.php?dir=headers11) [function.include]: failed to open
>> stream: No such file or directory in /mnt/stor5-wc2-
>> dfw1/479383/493261/www.quaaoutlodge.com/web/content/themes/Quaaout11-
>> dev/photoshuffler.js.php on line 29"
>> But when I remove the "?dir=" part from the url it works just fine.
>> What would that be?
>> Line 29 looks like:
>> blablabla<?php include "./getfile.php?dir=".$_GET['dir'];?>
>>
>> Thank you for hints&  suggestions!
>>
>> Ron
> 
> 
> Hi Ron,
> 
> You are confusing a http request with an include. They are different
> beasts.
> A http request look like:
> http://www.example.com/yourphpfile.php and you can add info to an URL by
> adding name/value pairs, like this:
> http://www.example.com/yourphpfile.php?name=ron&favcol=blue
> 
> In $_GET you will find 2 keys:
> $_GET["name"] will contain ron
> $_GET["favcol"] will contain blue
> 
> 
> Now to the include:
> If you include a file, you include a file. :-) No http or apache
> involved. So no GET superglobal is set. (And that is the reason things
> don't work as you expected.)
> 
> Unless you use http-wrappers, like
> include ("http://www.example.com/getfile.php?file=...");
> 
> WARNING: the above is REALLY poor programming to get information into
> your include file: you make a trip using  the http-protocol/apache/new
> PHP instance/etc to get the info into your included file).
> 
> How to solve this?
> Depending on your situation, in general: 1) Make the included file
> contain a function or an class, and use that function or class from your
> main file. Something like (for Object):
> require_once ("class_myFileFetcher.php"); $myFileFetcher = new
> FileFetcher();
> $myFileFetcher->setFile(..);
> $myFileFetcher->setHearder(..);
> $myFileFetcher->stream(..);
> 
> The above is just fantasycode of course.
> 
> Or with a function:
> require_once ("myfunctions/myFileFetcherFunction.php");
> fetchFileAndStream("/some/path/some/file");
> 
> option 2 (ugly in my opinion):
> Simply set a variable, then the include. Let the code in the include use
> that variable.
> So:
> $theUglyWayFileName = "/some/path/some/file"; require_once
> ("myFileFetcher.php");
> And let the code in myFileFetcher.php use the variable named:
> $theUglyWayFileName.
> 
> I advise you to both avoid http-wrappers and also to avoid option 2. Go
> with a function or a class.

Erwin

If his basic requirement is to make variables from including file 
available in included file, then surely any variable that's set in the 
including file before the include occurs will be available to the 
included code?

eg:

includer.php:
<?php
// including file
$value = "text";
include "included.php";
?>

included.php:
<?php
if (!isset($value)) $value="\$value was not set";
echo $value . "\n";
?>

and testing:

$php includer.php
text
$

although it would probably be a good idea to use:

if (!isset($var)) $var=default_value;

in included.php for every expected var.

Rgds

Denis McMahon

[toc] | [prev] | [next] | [standalone]


#2188

FromErwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com>
Date2011-06-15 15:21 +0200
Message-ID<4df8b1b6$0$49184$e4fe514c@news.xs4all.nl>
In reply to#2183
On 6/15/2011 2:01 PM, Denis McMahon wrote:
> On Wed, 15 Jun 2011 10:04:08 +0200, Erwin Moller wrote:
>
>> On 6/15/2011 6:09 AM, cerr wrote:
>>> Hi  There, I would like to pass variables with GET to a file i include
>>> in my source file but it doesn't work, I get something like "Warning:
>>> include(./getfile.php?dir=headers11) [function.include]: failed to open
>>> stream: No such file or directory in /mnt/stor5-wc2-
>>> dfw1/479383/493261/www.quaaoutlodge.com/web/content/themes/Quaaout11-
>>> dev/photoshuffler.js.php on line 29"
>>> But when I remove the "?dir=" part from the url it works just fine.
>>> What would that be?
>>> Line 29 looks like:
>>> blablabla<?php include "./getfile.php?dir=".$_GET['dir'];?>
>>>
>>> Thank you for hints&   suggestions!
>>>
>>> Ron
>>
>>
>> Hi Ron,
>>
>> You are confusing a http request with an include. They are different
>> beasts.
>> A http request look like:
>> http://www.example.com/yourphpfile.php and you can add info to an URL by
>> adding name/value pairs, like this:
>> http://www.example.com/yourphpfile.php?name=ron&favcol=blue
>>
>> In $_GET you will find 2 keys:
>> $_GET["name"] will contain ron
>> $_GET["favcol"] will contain blue
>>
>>
>> Now to the include:
>> If you include a file, you include a file. :-) No http or apache
>> involved. So no GET superglobal is set. (And that is the reason things
>> don't work as you expected.)
>>
>> Unless you use http-wrappers, like
>> include ("http://www.example.com/getfile.php?file=...");
>>
>> WARNING: the above is REALLY poor programming to get information into
>> your include file: you make a trip using  the http-protocol/apache/new
>> PHP instance/etc to get the info into your included file).
>>
>> How to solve this?
>> Depending on your situation, in general: 1) Make the included file
>> contain a function or an class, and use that function or class from your
>> main file. Something like (for Object):
>> require_once ("class_myFileFetcher.php"); $myFileFetcher = new
>> FileFetcher();
>> $myFileFetcher->setFile(..);
>> $myFileFetcher->setHearder(..);
>> $myFileFetcher->stream(..);
>>
>> The above is just fantasycode of course.
>>
>> Or with a function:
>> require_once ("myfunctions/myFileFetcherFunction.php");
>> fetchFileAndStream("/some/path/some/file");
>>
>> option 2 (ugly in my opinion):
>> Simply set a variable, then the include. Let the code in the include use
>> that variable.
>> So:
>> $theUglyWayFileName = "/some/path/some/file"; require_once
>> ("myFileFetcher.php");
>> And let the code in myFileFetcher.php use the variable named:
>> $theUglyWayFileName.
>>
>> I advise you to both avoid http-wrappers and also to avoid option 2. Go
>> with a function or a class.
>
> Erwin
>

Hello Dennis,


> If his basic requirement is to make variables from including file
> available in included file, then surely any variable that's set in the
> including file before the include occurs will be available to the
> included code?

Absolutely. It can be done. Hence my option 2). ;-)

I think it is just ugly.
Ugly because the included file depends on variables outside itself.

Compared to using a function or class it is needlessly fragile.
(At least, I do not see any advantages doing it that way.)


>
> eg:
>
> includer.php:
> <?php
> // including file
> $value = "text";
> include "included.php";
> ?>
>
> included.php:
> <?php
> if (!isset($value)) $value="\$value was not set";
> echo $value . "\n";
> ?>
>
> and testing:
>
> $php includer.php
> text
> $
>
> although it would probably be a good idea to use:
>
> if (!isset($var)) $var=default_value;
>
> in included.php for every expected var.

Yes, I know the approach. Used it a few times myself in the past, but 
nowadays I think it is very ugly. ;-)

Regards,
Erwin Moller

>
> Rgds
>
> Denis McMahon


-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#2194

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-06-15 12:02 -0400
Message-ID<ital2b$12v$1@dont-email.me>
In reply to#2188
On 6/15/2011 9:21 AM, Erwin Moller wrote:
> On 6/15/2011 2:01 PM, Denis McMahon wrote:
>
> Hello Dennis,
>
>
>> If his basic requirement is to make variables from including file
>> available in included file, then surely any variable that's set in the
>> including file before the include occurs will be available to the
>> included code?
>
> Absolutely. It can be done. Hence my option 2). ;-)
>
> I think it is just ugly.
> Ugly because the included file depends on variables outside itself.
>
> Compared to using a function or class it is needlessly fragile.
> (At least, I do not see any advantages doing it that way.)
>
>

You are correct, Erwin.  Making the code in the included file a function 
or a class decouples it from the rest of the logic.  This makes the code 
more robust, easier to maintain and, if coded correctly, reusable.

Definitely an advantage over just including a file which then depends on 
other variables outside of itself.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#2200

Fromcerr <ron.eggler@gmail.com>
Date2011-06-15 21:58 -0700
Message-ID<aeeed188-7766-4b00-a323-9abd60e9efae@s41g2000prb.googlegroups.com>
In reply to#2175
On Jun 15, 1:04 am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spamyourself.com> wrote:
> On 6/15/2011 6:09 AM, cerr wrote:
>
> > Hi  There, I would like to pass variables with GET to a file i include
> > in my source file but it doesn't work, I get something like
> > "Warning: include(./getfile.php?dir=headers11) [function.include]:
> > failed to open stream: No such file or directory in /mnt/stor5-wc2-
> > dfw1/479383/493261/www.quaaoutlodge.com/web/content/themes/Quaaout11-
> > dev/photoshuffler.js.php on line 29"
> > But when I remove the "?dir=" part from the url it works just fine.
> > What would that be?
> > Line 29 looks like:
> > blablabla<?php include "./getfile.php?dir=".$_GET['dir'];?>
>
> > Thank you for hints&  suggestions!
>
> > Ron
>
> Hi Ron,
>
> You are confusing a http request with an include.
> They are different beasts.
> A http request look like:http://www.example.com/yourphpfile.php
> and you can add info to an URL by adding name/value pairs, like this:http://www.example.com/yourphpfile.php?name=ron&favcol=blue
>
> In $_GET you will find 2 keys:
> $_GET["name"] will contain ron
> $_GET["favcol"] will contain blue
>
> Now to the include:
> If you include a file, you include a file. :-)
> No http or apache involved. So no GET superglobal is set. (And that is
> the reason things don't work as you expected.)
>
> Unless you use http-wrappers, like
> include ("http://www.example.com/getfile.php?file=...");
>
> WARNING: the above is REALLY poor programming to get information into
> your include file: you make a trip using  the http-protocol/apache/new
> PHP instance/etc to get the info into your included file).
>
> How to solve this?
> Depending on your situation, in general:
> 1) Make the included file contain a function or an class, and use that
> function or class from your main file.
> Something like (for Object):
> require_once("class_myFileFetcher.php");
> $myFileFetcher = new FileFetcher();
> $myFileFetcher->setFile(..);
> $myFileFetcher->setHearder(..);
> $myFileFetcher->stream(..);
>
> The above is just fantasycode of course.
>
> Or with a function:
> require_once ("myfunctions/myFileFetcherFunction.php");
> fetchFileAndStream("/some/path/some/file");
>
> option 2 (ugly in my opinion):
> Simply set a variable, then the include. Let the code in the include use
> that variable.
> So:
> $theUglyWayFileName = "/some/path/some/file";
> require_once ("myFileFetcher.php");
> And let the code in myFileFetcher.php use the variable named:
> $theUglyWayFileName.
>

I implemented option 2 so far and it works well - why would thast be
bad anyways?
Thanks! :)

Ron

[toc] | [prev] | [next] | [standalone]


#2202

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-06-16 07:14 -0400
Message-ID<itcois$fcj$2@dont-email.me>
In reply to#2200
On 6/16/2011 12:58 AM, cerr wrote:
> On Jun 15, 1:04 am, Erwin Moller
> <Since_humans_read_this_I_am_spammed_too_m...@spamyourself.com>  wrote:
>> On 6/15/2011 6:09 AM, cerr wrote:
>>
>>> Hi  There, I would like to pass variables with GET to a file i include
>>> in my source file but it doesn't work, I get something like
>>> "Warning: include(./getfile.php?dir=headers11) [function.include]:
>>> failed to open stream: No such file or directory in /mnt/stor5-wc2-
>>> dfw1/479383/493261/www.quaaoutlodge.com/web/content/themes/Quaaout11-
>>> dev/photoshuffler.js.php on line 29"
>>> But when I remove the "?dir=" part from the url it works just fine.
>>> What would that be?
>>> Line 29 looks like:
>>> blablabla<?php include "./getfile.php?dir=".$_GET['dir'];?>
>>
>>> Thank you for hints&    suggestions!
>>
>>> Ron
>>
>> Hi Ron,
>>
>> You are confusing a http request with an include.
>> They are different beasts.
>> A http request look like:http://www.example.com/yourphpfile.php
>> and you can add info to an URL by adding name/value pairs, like this:http://www.example.com/yourphpfile.php?name=ron&favcol=blue
>>
>> In $_GET you will find 2 keys:
>> $_GET["name"] will contain ron
>> $_GET["favcol"] will contain blue
>>
>> Now to the include:
>> If you include a file, you include a file. :-)
>> No http or apache involved. So no GET superglobal is set. (And that is
>> the reason things don't work as you expected.)
>>
>> Unless you use http-wrappers, like
>> include ("http://www.example.com/getfile.php?file=...");
>>
>> WARNING: the above is REALLY poor programming to get information into
>> your include file: you make a trip using  the http-protocol/apache/new
>> PHP instance/etc to get the info into your included file).
>>
>> How to solve this?
>> Depending on your situation, in general:
>> 1) Make the included file contain a function or an class, and use that
>> function or class from your main file.
>> Something like (for Object):
>> require_once("class_myFileFetcher.php");
>> $myFileFetcher = new FileFetcher();
>> $myFileFetcher->setFile(..);
>> $myFileFetcher->setHearder(..);
>> $myFileFetcher->stream(..);
>>
>> The above is just fantasycode of course.
>>
>> Or with a function:
>> require_once ("myfunctions/myFileFetcherFunction.php");
>> fetchFileAndStream("/some/path/some/file");
>>
>> option 2 (ugly in my opinion):
>> Simply set a variable, then the include. Let the code in the include use
>> that variable.
>> So:
>> $theUglyWayFileName = "/some/path/some/file";
>> require_once ("myFileFetcher.php");
>> And let the code in myFileFetcher.php use the variable named:
>> $theUglyWayFileName.
>>
>
> I implemented option 2 so far and it works well - why would thast be
> bad anyways?
> Thanks! :)
>
> Ron

The code now depends on variables set in other files, making it harder 
to maintain and less reusable.

Functions are much better and cleaner.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web