473,516 Members | 3,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Include with variables - why does this work ?

TMN
Hi All

I am new to PHP and I do not understand why the following works ??

$file=urlencode("displayIncidents.php");
echo "<a href=statistics.php?fileName=$file&delete=true>Del ete
Incident</a><br />";

When this link is selected the statistics.php simply includes the file
that passed to it - but why does it find the file and not try to load
the literal 'fileName=$file&delete=true' (that obviously does not
exist) ?

thanks
Tim

Jan 12 '07 #1
12 1515
TMN wrote:
Hi All

I am new to PHP and I do not understand why the following works ??

$file=urlencode("displayIncidents.php");
echo "<a href=statistics.php?fileName=$file&delete=true>Del ete
Incident</a><br />";

When this link is selected the statistics.php simply includes the file
that passed to it - but why does it find the file and not try to load
the literal 'fileName=$file&delete=true' (that obviously does not
exist) ?
Hi,

We cannot say what statistics.php will do with the contents in the url
because you didn't show any code from that file.

But what you do here is simply creating an URL.
urlencode takes a string and transforms it to a form that can be passed
through a url, as you did.
Nothing more nothing less.

So what happens is:
1) your variable $file contains 'displayIncidents.php'
2) you echo:
<a href=statistics.php?fileName=$file&delete=true>Del ete Incident</a><br/>

where $file gets replaced by the value in $file, so you get:

<a href=statistics.php?fileName=displayIncidents.php& delete=true>
Delete Incident</a><br/>

This happens because you put a variablename into "", it gets replaced.
If you use '' this will not happen.

consider the following code:
$myvar = "John";
echo "Hi $myVar";
// will produce: Hi John

Regards,
Erwin Moller

>
thanks
Tim
Jan 12 '07 #2
TMN
Thanks - I should have include the statistics code:

$file=$_GET['fileName'];
echo "Requested File is: ".$file;
include($file);

The include works and finds 'displayIncidents.php' instead of trying
to find 'displayIncidents.php&delete=true' - is this because I used
urlencode ?

thanks
Tim
Erwin Moller wrote:
TMN wrote:
Hi All

I am new to PHP and I do not understand why the following works ??

$file=urlencode("displayIncidents.php");
echo "<a href=statistics.php?fileName=$file&delete=true>Del ete
Incident</a><br />";

When this link is selected the statistics.php simply includes the file
that passed to it - but why does it find the file and not try to load
the literal 'fileName=$file&delete=true' (that obviously does not
exist) ?

Hi,

We cannot say what statistics.php will do with the contents in the url
because you didn't show any code from that file.

But what you do here is simply creating an URL.
urlencode takes a string and transforms it to a form that can be passed
through a url, as you did.
Nothing more nothing less.

So what happens is:
1) your variable $file contains 'displayIncidents.php'
2) you echo:
<a href=statistics.php?fileName=$file&delete=true>Del ete Incident</a><br/>

where $file gets replaced by the value in $file, so you get:

<a href=statistics.php?fileName=displayIncidents.php& delete=true>
Delete Incident</a><br/>

This happens because you put a variablename into "", it gets replaced.
If you use '' this will not happen.

consider the following code:
$myvar = "John";
echo "Hi $myVar";
// will produce: Hi John

Regards,
Erwin Moller


thanks
Tim
Jan 12 '07 #3
Rik
TMN wrote:
Thanks - I should have include the statistics code:

$file=$_GET['fileName'];
echo "Requested File is: ".$file;
include($file);

The include works and finds 'displayIncidents.php' instead of trying
to find 'displayIncidents.php&delete=true' - is this because I used
urlencode ?
No, in $_GET['fileName'] is only displayIncidents.php at this point. The
ampersand divides the GET variables. So, $_GET['delete'] == 'true' (the
string true) at this point. If you want to have the
'displayIncidents.php&delete=true' in one GET variable fileName, you should
(raw)urlencode all of it, this means including the & and =.
--
Rik Wasmus
Jan 12 '07 #4
TMN wrote:

answer: Because it destroys the order of the conversation.
question: Why?
anser: Topposting
question: What is the most annoying thing on Usenet?

Thanks - I should have include the statistics code:

$file=$_GET['fileName'];
echo "Requested File is: ".$file;
include($file);

The include works and finds 'displayIncidents.php' instead of trying
to find 'displayIncidents.php&delete=true' - is this because I used
urlencode ?
No, that is simply because your URL contains a few name-value pairs,
seperated by &.
You extract one of them, as you did, via $_GET["somename"].

The URLencoding only make sure that the value you are posting is valid for
passing around via url.
An example:

[BAD]
$url = "test.php";
// now add a few name/value pair without urlencode:
$name1 = "question";
$value1 = "How do I pass strange stuff around? (like this:&%#)";

$name2 = "username";
$value2 = "Jim Johnson";

$url .= "?$name1=$value1&$name2=$value2";

This will end up with the following url:
test.php?question=How do I pass strange stuff around? (like
this:&%#)&username=Jim Johnson

Which will surely fail.

[GOOD]
$url = "test.php";
// now add a few name/value pair without urlencode:
$name1 = "question";
$value1 = "How do I pass strange stuff around? (like this:&%#)";

$name2 = "username";
$value2 = "Jim Johnson";

$url .= "?$name1=".urlencode($value1)."&$name2=".urlendode ($value2);

This will end up with an url that replaced all naughty characters with
url-encoded characters tat are OK to use in an url.

Hope that helps.

Regards,
Erwin Moller
>
thanks
Tim
Erwin Moller wrote:
>TMN wrote:
Hi All

I am new to PHP and I do not understand why the following works ??

$file=urlencode("displayIncidents.php");
echo "<a href=statistics.php?fileName=$file&delete=true>Del ete
Incident</a><br />";

When this link is selected the statistics.php simply includes the file
that passed to it - but why does it find the file and not try to load
the literal 'fileName=$file&delete=true' (that obviously does not
exist) ?

Hi,

We cannot say what statistics.php will do with the contents in the url
because you didn't show any code from that file.

But what you do here is simply creating an URL.
urlencode takes a string and transforms it to a form that can be passed
through a url, as you did.
Nothing more nothing less.

So what happens is:
1) your variable $file contains 'displayIncidents.php'
2) you echo:
<a href=statistics.php?fileName=$file&delete=true>Del ete
Incident</a><br/>

where $file gets replaced by the value in $file, so you get:

<a href=statistics.php?fileName=displayIncidents.php& delete=true>
Delete Incident</a><br/>

This happens because you put a variablename into "", it gets replaced.
If you use '' this will not happen.

consider the following code:
$myvar = "John";
echo "Hi $myVar";
// will produce: Hi John

Regards,
Erwin Moller

>
thanks
Tim
Jan 12 '07 #5
TMN

Erwin Moller wrote:
TMN wrote:

answer: Because it destroys the order of the conversation.
question: Why?
anser: Topposting
question: What is the most annoying thing on Usenet?

Thanks - I should have include the statistics code:

$file=$_GET['fileName'];
echo "Requested File is: ".$file;
include($file);

The include works and finds 'displayIncidents.php' instead of trying
to find 'displayIncidents.php&delete=true' - is this because I used
urlencode ?

No, that is simply because your URL contains a few name-value pairs,
seperated by &.
You extract one of them, as you did, via $_GET["somename"].

The URLencoding only make sure that the value you are posting is valid for
passing around via url.
An example:

[BAD]
$url = "test.php";
// now add a few name/value pair without urlencode:
$name1 = "question";
$value1 = "How do I pass strange stuff around? (like this:&%#)";

$name2 = "username";
$value2 = "Jim Johnson";

$url .= "?$name1=$value1&$name2=$value2";

This will end up with the following url:
test.php?question=How do I pass strange stuff around? (like
this:&%#)&username=Jim Johnson

Which will surely fail.

[GOOD]
$url = "test.php";
// now add a few name/value pair without urlencode:
$name1 = "question";
$value1 = "How do I pass strange stuff around? (like this:&%#)";

$name2 = "username";
$value2 = "Jim Johnson";

$url .= "?$name1=".urlencode($value1)."&$name2=".urlendode ($value2);

This will end up with an url that replaced all naughty characters with
url-encoded characters tat are OK to use in an url.

Hope that helps.

Regards,
Erwin Moller

thanks
Tim
Erwin Moller wrote:
TMN wrote:

Hi All

I am new to PHP and I do not understand why the following works ??

$file=urlencode("displayIncidents.php");
echo "<a href=statistics.php?fileName=$file&delete=true>Del ete
Incident</a><br />";

When this link is selected the statistics.php simply includes the file
that passed to it - but why does it find the file and not try to load
the literal 'fileName=$file&delete=true' (that obviously does not
exist) ?

Hi,

We cannot say what statistics.php will do with the contents in the url
because you didn't show any code from that file.

But what you do here is simply creating an URL.
urlencode takes a string and transforms it to a form that can be passed
through a url, as you did.
Nothing more nothing less.

So what happens is:
1) your variable $file contains 'displayIncidents.php'
2) you echo:
<a href=statistics.php?fileName=$file&delete=true>Del ete
Incident</a><br/>

where $file gets replaced by the value in $file, so you get:

<a href=statistics.php?fileName=displayIncidents.php& delete=true>
Delete Incident</a><br/>

This happens because you put a variablename into "", it gets replaced.
If you use '' this will not happen.

consider the following code:
$myvar = "John";
echo "Hi $myVar";
// will produce: Hi John

Regards,
Erwin Moller

thanks
Tim

No more top posting for me !!!!!

Thanks for the explanation - in displayIncidents.php I can do this:
$showDelete = $_GET["delete"];
So the php function "include" knows to parse the name-value pairs and
the url ?

thanks
Tim

Jan 12 '07 #6
Rik
TMN wrote:
Erwin Moller wrote:
>>The include works and finds 'displayIncidents.php' instead of
trying
to find 'displayIncidents.php&delete=true' - is this because I used
urlencode ?

No, that is simply because your URL contains a few name-value pairs,
seperated by &.
You extract one of them, as you did, via $_GET["somename"].

The URLencoding only make sure that the value you are posting is
valid for passing around via url.

[GOOD]
$url .= "?$name1=".urlencode($value1)."&$name2=".urlendode ($value2);

This will end up with an url that replaced all naughty characters
with url-encoded characters tat are OK to use in an url.
No more top posting for me !!!!!
Hear, hear! :-)
Thanks for the explanation - in displayIncidents.php I can do this:
$showDelete = $_GET["delete"];
So the php function "include" knows to parse the name-value pairs and
the url ?
The variables are already parsed at the beginning. All variables in the
scope the include is called in (so global scope if called in global, the
scope of a function if called in that) are available to the included
script.

Furthermore, the $_GET ($_POST/$_SERVER/$_SESSION/$_REQUEST/$_ENV) are
'superglobals', which means you can access them directly from any point in
the script(s), they're always in scope.
--
Rik Wasmus
Jan 12 '07 #7
TMN wrote:
The include works and finds 'displayIncidents.php' instead of trying
to find 'displayIncidents.php&delete=true' - is this because I used
urlencode ?
PHP takes a query string, e.g. the part after the question mark in:

http://example.net/foo.php?a=1&b=2&c=3

and splits it up using ampersands (although it can be configured to
use different characters instead/as well) like this:

a=1
b=2
c=3

and then uses these to populate a global array called $_GET, such that:

$_GET['a'] = 1;
$_GET['b'] = 2;
$_GET['c'] = 3;

This $_GET array can now be accessed by "foo.php" and used as it likes.
(foo.php is also able to access the raw, unprocessed query string, but
this is not usually very useful.)

In your example, statistics.php sees:

$_GET['fileName'] = 'displayIncidents.php';
$_GET['delete'] = 'true';

so the following code:

$file=$_GET['fileName'];
echo "Requested File is: ".$file;
include($file);

works.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 12 '07 #8
TMN

Toby Inkster wrote:
TMN wrote:
The include works and finds 'displayIncidents.php' instead of trying
to find 'displayIncidents.php&delete=true' - is this because I used
urlencode ?

PHP takes a query string, e.g. the part after the question mark in:

http://example.net/foo.php?a=1&b=2&c=3

and splits it up using ampersands (although it can be configured to
use different characters instead/as well) like this:

a=1
b=2
c=3

and then uses these to populate a global array called $_GET, such that:

$_GET['a'] = 1;
$_GET['b'] = 2;
$_GET['c'] = 3;

This $_GET array can now be accessed by "foo.php" and used as it likes.
(foo.php is also able to access the raw, unprocessed query string, but
this is not usually very useful.)

In your example, statistics.php sees:

$_GET['fileName'] = 'displayIncidents.php';
$_GET['delete'] = 'true';

so the following code:

$file=$_GET['fileName'];
echo "Requested File is: ".$file;
include($file);

works.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Thanks again that is a very clear explanation...

Tim
South Africa

Jan 12 '07 #9
TMN wrote:
Toby Inkster wrote:
>TMN wrote:
>>The include works and finds 'displayIncidents.php' instead of trying
to find 'displayIncidents.php&delete=true' - is this because I used
urlencode ?
PHP takes a query string, e.g. the part after the question mark in:

http://example.net/foo.php?a=1&b=2&c=3

and splits it up using ampersands (although it can be configured to
use different characters instead/as well) like this:

a=1
b=2
c=3

and then uses these to populate a global array called $_GET, such that:

$_GET['a'] = 1;
$_GET['b'] = 2;
$_GET['c'] = 3;

This $_GET array can now be accessed by "foo.php" and used as it likes.
(foo.php is also able to access the raw, unprocessed query string, but
this is not usually very useful.)

In your example, statistics.php sees:

$_GET['fileName'] = 'displayIncidents.php';
$_GET['delete'] = 'true';

so the following code:

$file=$_GET['fileName'];
echo "Requested File is: ".$file;
include($file);

works.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact


Thanks again that is a very clear explanation...

Tim
South Africa
As no one else has pointed it out (though it should be obvious)...

Doing include($file) without any validation of $file would be a big bad
security hole.

Robin

Jan 12 '07 #10
Rik
Robin wrote:
As no one else has pointed it out (though it should be obvious)...

Doing include($file) without any validation of $file would be a big
bad security hole.

It never even occured to me to include files I did not write myself :P

Validating a file is very difficult, you'll have to check it by hand.
--
Rik Wasmus
Jan 12 '07 #11
Rik wrote:
It never even occured to me to include files I did not write myself :P
Validating a file is very difficult, you'll have to check it by hand.
Don't validate the file itself -- validate the filename! For example,
check that the filename doesn't include any slashes, backslashes or colons
and you should be sorted.

$file=$_GET['fileName'];
if (preg_match('/[\/\\\:]/', $file))
die("Dirty, rotten scoundrel!");
echo "Requested File is: ".$file;
include($file);

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 12 '07 #12
Rik
Toby Inkster wrote:
Rik wrote:
>It never even occured to me to include files I did not write myself
:P Validating a file is very difficult, you'll have to check it by
hand.

Don't validate the file itself -- validate the filename! For example,
check that the filename doesn't include any slashes, backslashes or
colons and you should be sorted.

$file=$_GET['fileName'];
if (preg_match('/[\/\\\:]/', $file))
die("Dirty, rotten scoundrel!");
echo "Requested File is: ".$file;
include($file);
DOH! Offcourse that was what was meant... Haven't used constructions like
this in a while.

I'd whitelist the filename, but this would work also offcourse.
--
Rik Wasmus
Jan 12 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
5043
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is relative NOT to the immediate script that is including it, but is relative to the top-level calling script. In practice, this means that you have to...
2
1879
by: Marcus | last post by:
Hello, I am having problems with an include statement. I'm setting a session variable flag and then including a file, and in that include file I have a check at the top to make sure that the session variable is set, otherwise I stop executing and redirect. My problem is that this works if I use a relative path to the include file, but...
20
3523
by: Brian Burgess | last post by:
Hi all, Anyone know if this is possible? If so, on which page would the cookie be? .. On the page calling a function defined in the include file? thanks in advance.. -BB
5
2495
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to very carefully define the order in which paths are searched with command line options on the compiler. Both can cause problems, especially when...
6
4176
by: j.woodcock | last post by:
is there a way of having a file that's name is a variable (eg dependant on the user name) act like a include. i know that you cant define the file for an include asp tag using a variable and that reading the file using "Response.Write FSO.OpenTextFile(ppp, 1, False, False).readall" prints the file, and treating it like a html file. can...
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7548
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.