Undefined variable problem

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

Post Reply
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

Undefined variable problem

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

show the code producing the error.

Mark
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

Post by cto1mac »

Code: Select all

<?php include("php_header.php"); ?> 
//my db connection and session maintenance
<?php
if(isset($_SESSION&#1111;user_id])) &#123;
?>
   <a href="next.php">Next</a>
<?php
&#125; elseif (is_null($_SESSION&#1111;user_id])) &#123;
?>
<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
&#125;
?>
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']
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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.
Post Reply