Page 1 of 1
Undefined variable problem
Posted: Wed Apr 07, 2004 9:06 am
by cto1mac
I uninstalled IIS and went to Apache2. I took the same code that had worked fine under IIS and now under Apache2 it will run but I get all these "Undefined variable..." errors. What am I doing wrong?
Thanks
Posted: Wed Apr 07, 2004 9:08 am
by JayBird
show the code producing the error.
Mark
Posted: Wed Apr 07, 2004 9:18 am
by lostboy
undefined var errors are notices and not really errors, turn off notices in the error handling and you shouldn't get those messages
Posted: Wed Apr 07, 2004 9:26 am
by JayBird
lostboy wrote:undefined var errors are notices and not really errors, turn off notices in the error handling and you shouldn't get those messages
Turning down error reporting
IS NOT really a solution though!
Mark
Posted: Wed Apr 07, 2004 9:27 am
by cto1mac
Code: Select all
<?php include("php_header.php"); ?>
//my db connection and session maintenance
<?php
if(isset($_SESSIONїuser_id])) {
?>
<a href="next.php">Next</a>
<?php
} elseif (is_null($_SESSIONїuser_id])) {
?>
<form name="login" action="login.php" method="POST">
<table>
<tr>
<td>Username: </td><td><input type="text" size="20" name="uname"></td>
</tr>
<tr>
<td>Password: </td><td><input type="password" size="20" name="pword"></td>
</tr>
</table>
<input type="Submit" value="Submit><input type="Reset" value="Reset">
</form>
<?php
}
?>
And when it runs, the code runs and works but I get this error above the form (or link, if logon was successful):
Notice: Use of undefined constant user_id
--or--
Notice: Use of undefined index user_id
And I have tried both $_SESSION[user_id] and $_SESSION['user_id']
Posted: Wed Apr 07, 2004 9:45 am
by JayBird
is should be $_SESSION['user_id'] no matter what.
Is $_SESSION['user_id'] always set when this script is run. If not that is why you get this error.
Change this line...
Code: Select all
elseif (is_null($_SESSION['user_id'])) {
to...
Code: Select all
elseif (!isset($_SESSION['user_id'])) {
You were checking to see if it was NULL, but if it hadn't been set at all, you will get a warning.
Mark
Posted: Wed Apr 07, 2004 9:48 am
by JayBird
you can achieve the same thing, but in a better way IMO
Code: Select all
<?php
include("php_header.php");
//my db connection and session maintenance
if(isset($_SESSION['user_id'])) {
?>
<a href="next.php">Next</a>
<?php
} else {
?>
<form name="login" action="login.php" method="POST">
<table>
<tr>
<td>Username: </td><td><input type="text" size="20" name="uname"></td>
</tr>
<tr>
<td>Password: </td><td><input type="password" size="20" name="pword"></td>
</tr>
</table>
<input type="Submit" value="Submit"><input type="Reset" value="Reset">
</form>
<?php
}
?>
Mark
Posted: Wed Apr 07, 2004 9:56 am
by twigletmac
cto1mac - Please have a read of the second link in my signature.
lostboy - as Bech pointed out, turning down error reporting is not a solution, it only hides problems which are generally easily solved.