Basic code causing errors - but i've no idea why

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: Basic code causing errors - but i've no idea why

Post by jim.barrett »

Well....as you have them laid out there neither will work...
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: Basic code causing errors - but i've no idea why

Post by jim.barrett »

sorry....

of course you mean to say $_REQUEST['page'] or $_GET['page'] right?

because if you ask for 'test', no such variable is being passed in...and you will get an error.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic code causing errors - but i've no idea why

Post by simonmlewis »

Ok this is the problem that I have yet to resolve.
In the template I have to ask if there is something in the URL for the variable.
So how do I ask that, and then use the function of getPage or getMenu, without causing the errors? That really is the "money question".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Basic code causing errors - but i've no idea why

Post by califdon »

simonmlewis wrote: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.
I guess I'm not making myself clear to you. This has nothing to do with the error log. It has to do with programming in PHP and properly using the global variables that store data related to a web page that has been previously sent to a browser. You should read about these variables and try to understand what they are and how they should be used. http://devlog.info/2010/02/04/why-php-r ... dangerous/
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic code causing errors - but i've no idea why

Post by simonmlewis »

Will do - tho sadly, two pages on, my original question hasn't been answered.
Oh well.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Basic code causing errors - but i've no idea why

Post by califdon »

simonmlewis wrote:Ok this is the problem that I have yet to resolve.
In the template I have to ask if there is something in the URL for the variable.
So how do I ask that, and then use the function of getPage or getMenu, without causing the errors? That really is the "money question".
Sorry, I didn't address that question because, somehow, I didn't see it when I made my previous post.

What I am trying to explain to you is that you'll never be able to write and debug PHP scripts without understanding the basics, like what $_GET, $_POST and $_REQUEST are. They are not simple variables, they are arrays, and they are related to each other due to the fact that $_REQUEST contains all the array elements of both $_GET and $_POST, as well as $_COOKIE. So, usually you could get away with using $_REQUEST without concerning yourself with whether a particular array element came from the URL string ($_GET) or a form element ($_POST), BUT that is very bad practice because some day you may have a script that has a $_POST element and a $_GET element with the same name and you won't know which one you're dealing with! Not to mention the security issue described in the reference I gave you. So, as I said, rather clearly, I thought, in my earlier post, use $_GET. That is what you should always use to get the contents of a parameter that is passed in the URL. Why would you use anything else? Similarly, you should always use $_POST to get the contents of a value that has been entered in an HTML form with the "method='post'" specification.

If you not sure about how an array is different from a variable, again it is vitally important that you acquire this understanding. You cannot successfully write even the simplest of PHP scripts without a firm grasp of fundamentals like that.

I hope that clears this up for you.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic code causing errors - but i've no idea why

Post by simonmlewis »

Hi - yes it does.
And I think my solution that I posted (that someone said had too many braces, but then noticed it didn't) is correct.
I have tested this and it appears to be so, as I see no errors on the log anymore related to that.

So the query must first see if there is anything in the URL. And if there is, only THEN run the function getPage. Which is what I think my script now does.

And I agree with you about GET POST and REQUEST. I was just taught to use REQUEST to get info from the URL. And I have to admit, I have countless pages on the web that use it, but will test the theory and at some point, alter them. but there are many!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Basic code causing errors - but i've no idea why

Post by califdon »

I'm afraid you were taught wrong.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic code causing errors - but i've no idea why

Post by simonmlewis »

That's down to Forums! As I learnt that you can collect the data from the URL, via $_REQUEST.
But you state that to do that, for example to capture that something is completed, this:
index.php?page=edit&c=y
Rather than my using
$c=$_REQUEST['c'];

I should be using:
$c=$_GET['c'];

Correct?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Basic code causing errors - but i've no idea why

Post by califdon »

Yes. Would you please go back and read my previous post, where I boldfaced the pertinent instructions, as well as explained why you can get the same data with $_REQUEST, but should not. I don't know how to say it more emphatically.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic code causing errors - but i've no idea why

Post by simonmlewis »

Pertinent?
Emphatically?
Nice big words. But yes, I'll take a look. Thank you. At the time, I was more interested in a 'fix' rather than a lecture about variables. But I see the need to understand.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply