whats the mistake??

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
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

whats the mistake??

Post by claws »

Code: Select all

if(isset($_SESSION['UserType']) && $_SESSION['UserType']=="Administrator" || $_SESSION['UserName']==$reply_arr['user_name'])
{

...
...

}
if a person is logged in all the session variables will be set otherwise not.

so if person is not logged in. the above code should not show any mistake because
isset($_SESSION['UserType']) will return false.

and therefore further checking is not done.
but to my surprise it shows the following error:
Notice: Undefined index: UserName in D:\htdocs\temp.php on line 144
Last edited by claws on Mon Sep 24, 2007 3:09 pm, edited 1 time in total.
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

one more...

Post by claws »

Code: Select all

echo "xyz".  isset($_SESSION['UserName'])?$_SESSION['UserName']:'Guest'. "xyz"
should workfine if a person is not logged in ( session variables are not set) but it shows the error

Notice: Undefined index: UserName in D:\htdocs\temp.php on line 79
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The last snippet appears fine, technically.
Hemlata
Forum Commoner
Posts: 35
Joined: Mon Sep 10, 2007 5:40 am
Location: India
Contact:

Post by Hemlata »

Hello claws

You should slightly modify your code to use braces while using compulsory conditions. Your previous code was checking condition as whether $_SESSION['UserType'] is set or not AND $_SESSION['UserType'] is administrator OR $_SESSION['UserName'] is $reply_arr['user_name']. Means conditions within if block was AND and OR, so it was executing the if block. Modify your code with following code and i think you could get the required results..

Code: Select all

if(isset($_SESSION['UserType']) && ($_SESSION['UserType']=="Administrator" || $_SESSION['UserName']==$reply_arr['user_name']))
Regards,
Post Reply