Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Cannot Define Variable

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Cannot Define Variable

Postby broer » Tue Aug 20, 2002 10:16 am

Please help me with my little problem....

I've just installed my webserver with PHP
and it works (i think) fine.

There is however one problem. Sometime
when i want to install a script i got the
error that de variable is not defined and i
get a list of variables that are not defined.

I think the problem has got to do with the
fact that i cannot call the PHP like this:

http://blabla.com/program.php?setup=admin

the php script then says that the variable
setup is not defined...
broer
New php-forum User
New php-forum User
 
Posts: 12
Joined: Tue Aug 20, 2002 10:08 am

Postby DoppyNL » Tue Aug 20, 2002 10:31 am

You will want to read this page:
http://www.php.net/manual/en/reserved.variables.php

and especially the part about the variable $_COOKIE, $_GET, $_POST and $_REQUEST (that starts about halfway the page)

to answer you're question without explaining:
acces the variable like this:
Code: Select all
$whatever = $_REQUEST['setup'];
//-- or
$whatever = $_GET['setup'];



Greetz Daan
DoppyNL
 

Postby broer » Tue Aug 20, 2002 12:07 pm

Thanks for the fast reply :D

I've read the article but i kind of a newbie
with this stuff.

Let's say the script says:

$ip = $REMOTE_ADDR;

This results:

Notice: Undefined variable: REMOTE_ADDR in E:\Apache2\htdocs\program.php4 on line 27

when i change it to:

$ip =$_GET["$REMOTE_ADDR"];

I still get the same error ... :?
broer
New php-forum User
New php-forum User
 
Posts: 12
Joined: Tue Aug 20, 2002 10:08 am

Postby DoppyNL » Tue Aug 20, 2002 12:11 pm

In you're first post you where talking about this:

http://blabla.com/program.php?setup=admin

that's why I said $_GET or $_REQUEST

if you read the same manual page again you will find that the remote adress is located in $_SERVER['remote_addr']

Greetz Daan
DoppyNL
 

Postby broer » Tue Aug 20, 2002 12:54 pm

I'm sorry that i mixed these questions but i thought
that it was the same problem because even i use
variables only in the page (not outside) i get the same error:
broer
New php-forum User
New php-forum User
 
Posts: 12
Joined: Tue Aug 20, 2002 10:08 am

Postby Jay » Tue Aug 20, 2002 1:42 pm

You will have to learn to make use of the Super Global Arrays, and work out which array contains what information.

Alternatively, you could also change your php.ini file and set the register globals setting to On, and then you wouldn't need the Arrays, you can just access $REMOTE_ADDR just like that!

I do have to point out its less secure that the SGA's, and there's also a strict order in which the variables are accessed (in case you have a GET, POST, SESSION, COOKIE, AND SERVER variable all with the same name!!!)
Jay
 

Postby broer » Thu Aug 22, 2002 8:10 am

I am a lot further in the understanding of how PHP works.

But, when i set the register globals setting = on
i now can access the variables without using
get, post etc. i still get the same error,
undefined variable.

Isn't it possible that this has got to do with
something else ?
broer
New php-forum User
New php-forum User
 
Posts: 12
Joined: Tue Aug 20, 2002 10:08 am

Postby broer » Thu Aug 22, 2002 8:17 am

here is my example for sending email with a form:
(i hope this has got to do with the same problem)

html piece:

<form action="anonym.php" method=post>
<table align=center border=1 bordercolordark=#996600
bordercolorlight=#cc6600 height=409 width=442>
<tr>
<td height=403 width=432>
<table>
<tr>
<td align=left valign=center> Sender Email
<td align=left valign=center>
<input name=from_mail size=25>
</td>
<tr>
<td align=left valign=center> Sender Name
<td align=left valign=center>
<input maxlength=256 name=from_name size=25>
</td>
<tr>
<td align=left valign=center>Receiver's email
<td align=left valign=center>
<input maxlength=256 name=to_mail size=25>

php piece:

$result=1;
if($name==""){
$result=0;
$error1="<li>$no_name</li>";
}
if($message==""){
$result=0;
$error5="<li>$no_message</li>";
}
elseif (strlen($message) <= $min_size) {
$error2="<li>$min_error</li>";
$result=0;
}
elseif (strlen($message) >= $max_size) {
$error3="<li>$max_error</li>";
$result=0;

end:

when i run this html-form
i got back the variable errors.... (also error1, error2)
and the error variables are only uses in this
php script.
broer
New php-forum User
New php-forum User
 
Posts: 12
Joined: Tue Aug 20, 2002 10:08 am

Postby Jay » Thu Aug 22, 2002 12:02 pm

You're not accessing the variables through the $_POST array. Use $_POST['name'] and $_POST['message'] instead of $name and $message
Jay
 

Postby cho@ » Fri Aug 23, 2002 3:03 am

in all my script i use the variable without using $POST when i send my form...


example: (it's just a part of the script)

elseif($list == 3)
{
if($Sub_daily)
{
$Link = mysql_connect('localhost','root','');
mysql_select_db("imc",$Link);

$contents = nl2br($contents);
mysql_query("INSERT INTO news (authorID, Type, Date, title, Content) VALUES ($id, 'daily', NOW(), '$title', '$contents')",$Link);
mysql_close($Link);

echo"<script language=javascript>";
echo"window.open('./info.php?id=";
print($id);
echo"','InfoMan','width=650,height=450,resizable=no,screenX=75,screenY=150,left=75,top=150');";
echo"window.close();";
echo"</script>";
}
else
{
echo"<form name=write_daily action=\"./info.php?list=3\" method=post>";
echo"<input type=hidden name=id value=";
print($id);
echo">";
echo"<table width=\"100%\">";
echo"<tr><td><b><font color=\"#003399\">Topic or Title:</font><b></td><td><b><font color=\"#003399\">Your name:</font><b></td></tr>";
echo"<tr><td><input type=text size=70 name=title></td><td><input type=text size=25 name=author></td></tr>";
echo"<tr><td colspan=2><br></td></tr>";
echo"<tr bgcolor=\#00000\><td colspan=2></td></tr>";
echo"<tr><td colspan=2><b><font color=\"#003399\">Your Daily News Text:</font><b></td></tr>";
echo"<tr><td colspan=2><textarea name=contents cols=100 rows=10></textarea></td></tr>";
echo"<tr><td colspan=2 align=center><input type=submit name=Sub_daily value=send!></td></tr>";
echo"</table>";
echo"</form>";
}
}

is the fact that i dont have anyprob with my var came of the fact that i test my script in localhost ????

will i have prob when i will put that script online cuz i dont use the array $POST to test my var ???????

:? :cry: :cry: :cry:
cho@
New php-forum User
New php-forum User
 
Posts: 39
Joined: Tue Aug 06, 2002 8:58 am
Location: UK

Postby DoppyNL » Fri Aug 23, 2002 3:08 am

is the fact that i dont have anyprob with my var came of the fact that i test my script in localhost ????

will i have prob when i will put that script online cuz i dont use the array $POST to test my var ???????

You don't need to use because in you're configuration file "register_globals" is set to "on", alle variables posted to the page will then be global.

Most Web Host Company's that I've seen have "register_globals" default to "Off", In that case you will have to use the Super Global Array's.

My best guess is that in the future this setting will always be off and cannot be changed because of security reasons.

Greetz Daan
DoppyNL
 

Postby cho@ » Fri Aug 23, 2002 3:17 am

so ....

if i change all my var declaration in $POST[varname]....
will it works even if the "register_globals" is set to "on".???..

like that no probs anymore if my boss ask me to put the "register_globals" is set to "off"...

and just a last stuff....

can someone give my an address or explain me the security features involved in?

thanx
cho@
New php-forum User
New php-forum User
 
Posts: 39
Joined: Tue Aug 06, 2002 8:58 am
Location: UK

Postby DoppyNL » Fri Aug 23, 2002 3:48 am

On this page http://www.php.net/manual/en/reserved.variables.php is explained wich variables are placed in wich global array.

Greetz Daan
DoppyNL
 

Postby broer » Fri Aug 23, 2002 6:25 am

elseif($list == 3)
{
if($Sub_daily)
{
$Link = mysql_connect('localhost','root','');
mysql_select_db("imc",$Link);

$contents = nl2br($contents);
mysql_query("INSERT INTO news (authorID, Type, Date, title, Content) VALUES ($id, 'daily', NOW(), '$title', '$contents')",$Link);
mysql_close($Link);

echo"<script language=javascript>";
echo"window.open('./info.php?id=";
print($id);
echo"','InfoMan','width=650,height=450,resizable=no,screenX=75,screenY=150,left=75,top=150');";
echo"window.close();";
echo"</script>";



If i use these variables with the "register_globals" is set to "on",
i still get the same error: variabele undefined.

i'm realy stuck en desperate !! :cry:
broer
New php-forum User
New php-forum User
 
Posts: 12
Joined: Tue Aug 20, 2002 10:08 am

Postby DoppyNL » Fri Aug 23, 2002 6:34 am

wich variable??
on wich line?

check if you didn't make a typo in you're form and script with the variablename.
variable-names are case-sensitive!

Greetz Daan
DoppyNL
 

Postby Jay » Fri Aug 23, 2002 8:12 am

Have a look at the line where the error occurs, and the lines immediately preceeding it (because an error on an earlier line might show up later). Read the message carefully, what does it say? Have a look for any possible cause.

By the way, the SGA for post is $_POST, not $POST! It's the same format with the rest! And you can use the SGA's with or without the register_globals setting from PHP 4.1.0 onwards!
Jay
 

Postby broer » Sat Aug 24, 2002 4:12 am

Thanx for all your hulp....

I've solved the problem... The problem was not
the super globals but it was that i not used
the recommended ini file.... 8O

I changed the old ini file to the good one
and everything work fine...

greetz,

Broer
broer
New php-forum User
New php-forum User
 
Posts: 12
Joined: Tue Aug 20, 2002 10:08 am


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.