Forum Moderators: coopster

Message Too Old, No Replies

PHP Newbie Question..

Undefined variable error message

         

Harvs

2:49 am on Dec 8, 2004 (gmt 0)

10+ Year Member



I'm working my way through a book and one of the first examples isn't working. Code is as follows

--text.html--
<html><head></head><body>
<form method=get action="text.php">
Who is your favourite author?
<input name="Author" type="text">
<br><br><input type=submit>
</form></body></html>

--text.php--
<html><head></head><body>
Your favourite Author is:
<?php
echo $Author;
?>
</body></html>

--Error Message--
Notice: Undefined variable: Author in c:\inetpub\wwwroot\book\text.php on line 6

Can anyone fill me in on what may be going wrong?

lorax

3:01 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I tested your code and it works fine! Did you cut and paste any of the code from a webpage?

olwen

3:43 am on Dec 8, 2004 (gmt 0)

10+ Year Member



It looks to me as if register_globals is off. This is the preferred setting.

Try
echo $_GET['Author'];

Remember there won't be a value until the form is submitted either so you will get an error at first. Good coding tests for these conditions, bad coding turns off error messages.

Harvs

5:13 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Olwen, what you said was corrent and it works if I use echo $_GET['Author'];

Is there a way to turn on register_globals so I don't have to do this in the future?

Can you also explain how $_GET[] works if global variables are turned off?

olwen

6:32 am on Dec 8, 2004 (gmt 0)

10+ Year Member



It's generally recognised as better from a security point of view to run with register_globals off, and as this is now the default learning to alter code to suit is a good idea.

If you do want to change it:
If this is your own server you can edit php.ini

register_globals off
to
register_globals on

If not your own server the documentation says you can set it for a directory in the .htaccess file assuming an Apache server. I think the line would be:
php_value register_globals 1

$_GET is automatically global.

Harvs

6:45 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Thanks for the help, I'll turn globals on for now so I can work with the book and do the exercises, but I'll be sure to turn it back off when it comes to doing my commercial work.

Thanks eveyone...

Hester

9:32 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is in the wrong forum, surely?

ergophobe

10:44 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's either here or in New to Web Development. This is fine.

On the register_globals thing... I think it's a common misconception that there is a security issue around this. It is not fundamentally a security issue, though it can have some security implications.

The more important issue is code stability. If you accidentally have something like this

<form method="post" action="index.php?filename=show_biography.php">
<input type="text" name="name" value="Bilbo">
<input type="text" name="filename" value="bilbo_biography.html">
....

Now what's the value of $filename in the script? I know for sure that $_POST['filename'] = "bilbo_biography.html" and that $_GET['filename'] = "show_biography.php".

What's $filename though? Fortunately we have some control [php.net] over the situation, but it's still likely to lead to confusion.

Tom