Reply to Re: Include with variables - why does this work ?

Your name:

Reply:


Posted by TMN on 01/12/07 10:34

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>Delete
> >> > 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>Delete
> >> 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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация