Page 1 of 2
Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 12:36 pm
by simonmlewis
Code: Select all
<?php
function getPage()
{
$thispage="includes/".$_GET['page'].".inc";
include $thispage;
}
function getMenu()
{
$thismenu="includes/menu/".$_GET['menu'].".inc";
include $thismenu;
}
?>
Line 108 is
And I get this
[text][10-Mar-2011 13:08:46] PHP Warning: include(includes/.inc) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in /home/trimex/public_html/index.php on line 108
[/text]
What am I doing wrong - is it old??
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 12:47 pm
by califdon
Do you see in the PHP Warning message where it says "PHP Warning: include(includes/.inc)"? There isn't any file in your includes directory named ".inc", right? Now look at your code and notice what happens if the value of $_GET['menu'] is blank. You'd get exactly that, wouldn't you? So that leads us to see that there was no value passed in the URL string for an argument named "menu".
Learn to read errors, notices, and warnings. They will often contain all you need to know to pinpoint the problem.
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 12:56 pm
by simonmlewis
So it is because I am using HTACCESS to show a particular URL on page opening, and those PAGE and MENU items are not in the URL, that these show?
Would this overcome it?
Code: Select all
$page= isset($_REQUEST['page']) ? $_REQUEST['page'] : null;
//long form
$page= null;
if (isset($_REQUEST['page']))
{
function getPage()
{
$thispage="includes/".$_GET['page'].".inc";
include $thispage;
}
}
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 1:03 pm
by simonmlewis
Code: Select all
$page= isset($_REQUEST['page']) ? $_REQUEST['page'] : null;
if (isset($_REQUEST['page']))
{
function getPage()
{
$thispage="includes/".$_GET['page'].".inc";
include $thispage;
}
}
$menu= isset($_REQUEST['menu']) ? $_REQUEST['menu'] : null;
if (isset($_REQUEST['menu']))
{
function getMenu()
{
$thismenu="includes/menu/".$_GET['menu'].".inc";
include $thismenu;
}
}
This works - but tell me, is it correct?
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 1:10 pm
by califdon
No. What is in your .htaccess file? If you're not passing the URL string in your redirect, that would explain why it's blank. I'm not experienced enough with mod_rewrite to advise you how to do that.
$_REQUEST is just the combination of $_POST, $_GET and $_COOKIE, so that wouldn't gain you anything. I believe most developers avoid using the $_REQUEST array.
Your code has some incorrect syntax in it, too, such as an extra closing curly bracket in your functions.
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 1:13 pm
by simonmlewis
HTACCESS:
[text]DirectoryIndex index.html index.htm index.php
order allow,deny
allow from all
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^$ index.php?page=home&img=home [L][/text]
And sorry, but I see no error in the {} of the code. There is an opening, followed by a second opening of the function. And then two closes.
Same on the second query.
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 1:30 pm
by califdon
simonmlewis wrote:HTACCESS:
[text]DirectoryIndex index.html index.htm index.php
order allow,deny
allow from all
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^$ index.php?page=home&img=home [L][/text]
And sorry, but I see no error in the {} of the code. There is an opening, followed by a second opening of the function. And then two closes.
Same on the second query.
Oh, I see that the functions are included within if blocks. Those are not 2 opening or closing brackets--that would be syntactically incorrect, anyway--one set is the if block, within which is a function and its block. I'm accustomed to a different way of indenting blocks, which threw me off. But your practice is perfectly acceptable, just different than what I'm used to reading.
Well, from your .htaccess RewriteRule, it would seem that the value "home" should be in the $_GET['page'] array element. Perhaps the problem is the scope of variables within a function. I would have thought that, $_GET being a global array, you could use that within the function, but perhaps not. The way to resolve this quickly is to assign $_GET['page'] to a variable outside the function, then pass it as an argument to the function. Even before doing that, I think I would echo out the value of $_GET['page'] immediately upon starting the script, to see what it really is.
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 1:33 pm
by califdon
Going back to your original post, I see that you were using an invalid array index ('menu') instead of 'page', which is undoubtedly why that didn't work, but now your most recent code does. So forget about my comments regarding scope.
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 1:37 pm
by simonmlewis
Ok I have to say I am more than just a little confused now.
That method of doing the GetPage, was something i learnt years ago.
They altered part of the function, but I don't think it's been changed since.
And as you say, the HTACCESS does go to the page, so what *should* my function be saying?? Or is it the _REQUEST should be _GET instead, in the pre-check ISSET query?
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 2:18 pm
by califdon
simonmlewis wrote:Ok I have to say I am more than just a little confused now.
That method of doing the GetPage, was something i learnt years ago.
They altered part of the function, but I don't think it's been changed since.
And as you say, the HTACCESS does go to the page, so what *should* my function be saying?? Or is it the _REQUEST should be _GET instead, in the pre-check ISSET query?
I didn't say there was anything wrong with that method. What I said was that your URL key-value pairs are "page=home&image=home" (any reason you need 2 variables with the same value?), which means that your $_GET array will look like this:
$_GET['page'] will have the value 'home'
$_GET['image'] will have the value 'home'
But the code you showed in your original post was looking for a non-existent array element, $_GET['menu'], which is what created the problem.
I would certainly use $_GET rather than $_REQUEST. As I explained earlier, $_REQUEST contains ALL the values, including the $_GET values, so it will work, but it muddies up your code so that you can't tell what you are looking for, and if there SHOULD be a $_GET index of, say, 'page',
AND a $_POST or $_COOKIE index with the same name, you can't be sure which one you're accessing. So, Yes, use $_GET.
Does that clarify your confusion?
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 2:25 pm
by simonmlewis
Ok.... I think I was correct in my guess of this problem, once you spotted the crux of it.
It's looking for a variable, and rather than say "it's NULL... move on", it errors.
So the ISSET part of the question does fix that, but what you are saying is that in that ISSET it should be a _GET, not _REQUEST.
Onto your other part: no reason. Just so that we know which page each "include" is for. One for the main body, one for the menu, and one for an image part of the page in the template.
I think the basic style of that function is ok without the ISSET, but since I am using HTACCESS, I think that is why it's throwing the wobbly on the error log.
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 5:45 pm
by califdon
simonmlewis wrote:Ok.... I think I was correct in my guess of this problem, once you spotted the crux of it.
It's looking for a variable, and rather than say "it's NULL... move on", it errors.
So the ISSET part of the question does fix that, but what you are saying is that in that ISSET it should be a _GET, not _REQUEST.
Onto your other part: no reason. Just so that we know which page each "include" is for. One for the main body, one for the menu, and one for an image part of the page in the template.
I think the basic style of that function is ok without the ISSET, but since I am using HTACCESS, I think that is why it's throwing the wobbly on the error log.
I'm not trying to beat you over the head with small details, but it's the small details that make or break a program--and a programmer. Variables and Arrays are very different sorts of objects in a programming language. A reference to an ARRAY checks to see if the INDEX is valid. That's what generated the error. Variables are not subject to such a check. You did NOT have a Null value for the reference to $_GET['menu']--there was NO SUCH INDEX. That makes a difference.
Your explanation of why you pass 2 key-value pairs doesn't hold water, unless there will be conditions in which their values will be different. If the image reference will always be the same word (such as 'home') as the page reference--that is, if the same image will always be used for the same page, you only need to pass the page reference, which will determine the corresponding image. Cluttering up a script with unnecessary duplications of data only makes your script harder to understand and creates additional opportunities to make mistakes when editing the code. Just because something works is no justification for doing it wrong. Now if you want to be able to specify an arbitrary image to be displayed on a given page, you would need 2 references.
Let me pass on to you one of the best pieces of programming philosophy I ever got, from the best programming instructor I ever had (teaching a Forth language class, years ago). He told us that the 2 most important rules of programming are:
- Everything matters; and
- Never give up!
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 11:33 pm
by jim.barrett
califdon wrote:
Let me pass on to you one of the best pieces of programming philosophy I ever got, from the best programming instructor I ever had (teaching a Forth language class, years ago). He told us that the 2 most important rules of programming are:
- Everything matters; and
- Never give up!
Hey, that's pretty good! Mind if I use that? LOL.
Re: Basic code causing errors - but i've no idea why
Posted: Thu Mar 10, 2011 11:42 pm
by califdon
jim.barrett wrote:califdon wrote:Hey, that's pretty good! Mind if I use that? LOL.
Not at all. I've been quoting Gary Firebaugh for years and he's never complained yet.

Re: Basic code causing errors - but i've no idea why
Posted: Fri Mar 11, 2011 2:55 am
by simonmlewis
Thanks for the length explanation, most of which I didn't quite understand.
Are you say that:
$test=$_REQUEST['test'];
.... on it's own, and if the url is, for example:
http://www.mytest.com/index.php?page=moon
.... IS going to cause errors in the error log, but...
$test=$_GET['test'];
.... on it's own, and if the url is, for example:
http://www.mytest.com/index.php?page=moon
... ISN'T??
If that's the case, it's simple. And what I said about whether my new piece of script is correct...... sounds like it is.