Quick question about "doc=" in a PHP page's URL
Moderator: General Moderators
Quick question about "doc=" in a PHP page's URL
Hallo. I'm currently coding a simple PHP site where index.php calls .doc files to fill its contents. i.e. In the links page would be index.php?doc=links which would be calling links.doc from the docs folder and displaying its contents. Now I know that all my syntax is correct and this should be working 100%. But when I have doc=something in my URL it just displays index.php and not the doc.
I'm running it on a test Apache server with PHP Mod installed and working (tested it with a simple php echo). For some reason though when a variable is declared in the URL it isn't seeing it. As I tried putting <? echo $doc ?> in index.php and did index.php?doc=something and got no return for that echo.
Anyone had this or similar before or know what may be causing it?
I'm running it on a test Apache server with PHP Mod installed and working (tested it with a simple php echo). For some reason though when a variable is declared in the URL it isn't seeing it. As I tried putting <? echo $doc ?> in index.php and did index.php?doc=something and got no return for that echo.
Anyone had this or similar before or know what may be causing it?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Quick question about "doc=" in a PHP page's UR
Post your code. Are you using the $_GET or $_REQUEST syntax for your query_string vars? Give more info please.dvergatal wrote:Hallo. I'm currently coding a simple PHP site where index.php calls .doc files to fill its contents. i.e. In the links page would be index.php?doc=links which would be calling links.doc from the docs folder and displaying its contents. Now I know that all my syntax is correct and this should be working 100%. But when I have doc=something in my URL it just displays index.php and not the doc.
I'm running it on a test Apache server with PHP Mod installed and working (tested it with a simple php echo). For some reason though when a variable is declared in the URL it isn't seeing it. As I tried putting <? echo $doc ?> in index.php and did index.php?doc=something and got no return for that echo.
Anyone had this or similar before or know what may be causing it?
Code: Select all
<? if (! $doc) { ?>
Something here..
<? } else {
include('docs/'.$doc.'.doc');
}
?>So http://localhost/index.php opens up index.php with the text "Something here.." in it.
And http://localhost/index.php?doc=articleadd should replace "Something here.." with the contents of docs/articleadd.doc
But it doesn't detect the variable.
The code and structure im using for this site is copied and pasted from another site I did hosted elsewhere that functions 100% correctly. I'm pretty certain it's something to do with php.ini. I've tried adjusting the global variables setting in the many ways many websites have suggested and to no avail.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
Code: Select all
$_GET[doc];No, it doesn't depend on it. Its a warning - its a coding style that has been depreciated and may one day not work. Whether you choose to SEE and ADMIT that you are using depreciated code is dependent on you setting the error reporting to ignore that important fact.shiznatix wrote:depends on if you are using error_reportion(E_ALL) or not, i use Edit Plus and its much easier to read if you dont use the ' but thats really a bad habit that i am trying to get out of
Notably, the fact that the original poster seems to be suffering from a global_register issue in the first place suggests we should encourage *correct* code, not code that might not work in a couple months. Talk about a perfect example.
shiznatix wrote:suicide is my only option i guess OR i will continue to break my "addiction" to bad coding practices... o snap im overdosing again
Me, I don't comment enough. Like, less than 1:100 ratio of comments to code.
Then again, McGruff has a 1,000 line MVC, true OOP version of "Hello, World".
Don't let him tell you otherwise - I've seen the unit tests. All 50 of them.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
One quick suggestion... don't rely on register_globals. If it is on, turn it off. Always code as though there were no register_globals. That being said, change your code to something like this...dvergatal wrote:The above code is in index.php and in the same folder as index.php is a folder called "docs" in which there is an "articleadd.doc" file containing some HTML/More PHP.Code: Select all
<? if (! $doc) { ?> Something here.. <? } else { include('docs/'.$doc.'.doc'); } ?>
So http://localhost/index.php opens up index.php with the text "Something here.." in it.
And http://localhost/index.php?doc=articleadd should replace "Something here.." with the contents of docs/articleadd.doc
But it doesn't detect the variable.
The code and structure im using for this site is copied and pasted from another site I did hosted elsewhere that functions 100% correctly. I'm pretty certain it's something to do with php.ini. I've tried adjusting the global variables setting in the many ways many websites have suggested and to no avail.
Code: Select all
<?
// This would also be a great place for some validation
$doc = $_GET["doc"];
if ( !isset($doc) ) { ?>
Something here..
<? } else {
include('docs/'.$doc.'.doc');
}
?>Everah wrote:One quick suggestion... don't rely on register_globals. If it is on, turn it off. Always code as though there were no register_globals. That being said, change your code to something like this...
Code: Select all
<?php // Note 1
if (empty($_GET['doc'])) { // Note 2
?>
Something here..
<?php
} else {
// Note 3
include('docs/'.$_GET['doc'].'.doc');
}
?>Note 2; $doc = $_GET["doc"]; in the above example will throw an 'Undefined index error' without verifying it actually exists.
Note 3; ...and even if it exists, there is a need for verifying that the $_GET['doc'].'.doc' file actually exists before including it.
Just my $.02
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Nice catch Jam. I was so focused on using the $_GET notation that I threw all other standards out the window. I agree with you completely.JAM wrote:Everah wrote:One quick suggestion... don't rely on register_globals. If it is on, turn it off. Always code as though there were no register_globals. That being said, change your code to something like this...I agree with the usage of register_globals. Main reason, appart from security, is that it makes you code 'better'. But if code should be better, don't use the short-tag version (Note 1) of <?php. Not all servers support that.Code: Select all
<?php // Note 1 if (empty($_GET['doc'])) { // Note 2 ?> Something here.. <?php } else { // Note 3 include('docs/'.$_GET['doc'].'.doc'); } ?>
Note 2; $doc = $_GET["doc"]; in the above example will throw an 'Undefined index error' without verifying it actually exists.
Note 3; ...and even if it exists, there is a need for verifying that the $_GET['doc'].'.doc' file actually exists before including it.
Just my $.02
In the step where I set $doc = $_GET["doc"], what if the developer was to write a validation function that checks the value of $_GET["doc"] to make sure that a) the var is not empty and b) the file exists on the server. If it passes, it returns the filename else it will return false. Then, without messing with the rest of the code that has been written, the developer can set $doc equal to the returned value of the function and run the rest of the script the way it is. What do you think?
Code: Select all
<?php
$doc = validate_included_file($_GET["doc"]);
//After validating, run the script
if (! $doc) { ?>
Something here..
<?php } else {
include('docs/'.$doc.'.doc');
}?>