By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,693 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,693 IT Pros & Developers. It's quick & easy.

keep getting undefined variable errors - please help

P: n/a
hello,

i am new to PHP, so go easy.

I am using the examples in the book:
PHP: Your Visual Blueprint For Creating Open Source, Server Side Content

In the section where they talk about getting values from a form
submission, the book says:

"PHP makes it easy to process data from a form. When a PHP
page receives form information, the page automatically converts
the names of form elements to PHP variables, and assigns
the data entered in the elements to the variables"

So I try to display what was submitted, but I get an undefined
variable error.

For example,

<? php
print "the ID you entered was: ";
print $userid;
?>

This returns an error. But, if I use the following it works:

<? php
print "the ID you entered was: ";
print $HTTP_POST_VARS['userid'];
?>

Can someone explain this?

One more example:

I'm trying to simply display a session ID (using the example in the book):

<?php
session_start();
?>

<html>
<head>
<title>PHP Session Test</title>
</head>
<body>

<?php
print "The session ID is: ";
print $PHPSESSID;
?>

</body>
</html>

When I access this page I get:

The session ID is:
Notice: Undefined variable: PHPSESSID in c:\inetpub\wwwroot\sessiontest.php
on line 13

Why do I get this error? This is the exact example from the book.

Please post replies to the newsgroup rather than a direct e-mail.
Thanks for your help.




Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 28 Jul 2004 03:45:36 +0000, Jason wrote:
hello,

i am new to PHP, so go easy.

I am using the examples in the book:
PHP: Your Visual Blueprint For Creating Open Source, Server Side Content

In the section where they talk about getting values from a form
submission, the book says:

"PHP makes it easy to process data from a form. When a PHP
page receives form information, the page automatically converts
the names of form elements to PHP variables, and assigns
the data entered in the elements to the variables"

So I try to display what was submitted, but I get an undefined
variable error.

For example,

<? php
print "the ID you entered was: ";
print $userid;
?>

This returns an error. But, if I use the following it works:

<? php
print "the ID you entered was: ";
print $HTTP_POST_VARS['userid'];
?>

Can someone explain this?

An old book =)

Since PHP4.2.0, the php.ini setting 'register_globals' has been disabled
by default (for security reasons). The method you want to use today is:
echo $_POST['userid'];
and from what I gather, to be PHP5 compliant too:
if (isset($_POST['userid'])) echo $_POST['user_id'];
but I haven't tried PHP5 myself as yet.

One more example:

I'm trying to simply display a session ID (using the example in the book):

<?php
session_start();
?>

<html>
<head>
<title>PHP Session Test</title>
</head>
<body>

<?php
print "The session ID is: ";
print $PHPSESSID;
?>

</body>
</html>

When I access this page I get:

The session ID is:
Notice: Undefined variable: PHPSESSID in c:\inetpub\wwwroot\sessiontest.php
on line 13

Why do I get this error? This is the exact example from the book.

Please post replies to the newsgroup rather than a direct e-mail.
Thanks for your help.

Again due to register_globals.
session_start();
echo session_id();
Hit php.net and do a "search online documentation" for 'register_globals'
and 'superglobals' and you'll find the others too, such as $_GET,
$_SERVER, $_COOKIE, $_FILES, $_SESSION =)
HTH.

Regards,

Ian
PS: FWIW.. I'd scrap your book.. it's old and sounds like it will teach
you some poor coding habits (and obvious problems). After many years now
of coding in PHP, I've never once opened the cover of a book... php.net is
the best online reference I've come across for anything of its nature.

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2

P: n/a
Red
Jason wrote:
hello,

i am new to PHP, so go easy.

I am using the examples in the book:
PHP: Your Visual Blueprint For Creating Open Source, Server Side Content

In the section where they talk about getting values from a form
submission, the book says:

"PHP makes it easy to process data from a form. When a PHP
page receives form information, the page automatically converts
the names of form elements to PHP variables, and assigns
the data entered in the elements to the variables"

So I try to display what was submitted, but I get an undefined
variable error.

For example,

<? php
print "the ID you entered was: ";
print $userid;
?>

This returns an error. But, if I use the following it works:

<? php
print "the ID you entered was: ";
print $HTTP_POST_VARS['userid'];
?>

Can someone explain this?
Sure, your book is out of date.
use $_POST['userid'] instead.
as in:$userid=$_POST['userid'].
For security purposes, resister globals are turned off by default in the
more recent versions of php
read about register globals
http://www.phpbuilder.com/manual/lan...predefined.php
One more example:

I'm trying to simply display a session ID (using the example in the book):

<?php
session_start();
?>

<html>
<head>
<title>PHP Session Test</title>
</head>
<body>

<?php
print "The session ID is: ";
print $PHPSESSID;
?>

</body>
</html>

When I access this page I get:

The session ID is:
Notice: Undefined variable: PHPSESSID in c:\inetpub\wwwroot\sessiontest.php
on line 13

Why do I get this error? This is the exact example from the book.

Please post replies to the newsgroup rather than a direct e-mail.
Thanks for your help.



Jul 17 '05 #3

P: n/a
thanks for the reply. man that was frustrating. i just bought
a new book that covers php 5, and i'll be sure to check out
php.net

Can someone explain this?

An old book =)

Since PHP4.2.0, the php.ini setting 'register_globals' has been disabled
by default (for security reasons). The method you want to use today is:
echo $_POST['userid'];
PS: FWIW.. I'd scrap your book.. it's old and sounds like it will teach
you some poor coding habits (and obvious problems). After many years now
of coding in PHP, I've never once opened the cover of a book... php.net is
the best online reference I've come across for anything of its nature.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.