tags:

views:

42

answers:

3

Please give me an idea on how to display elements in a page depending on who is logged in. For example, a user or an administrator. I'm thinking of something like this but I get a parse error, what do I lack in this code?:

EDIT:

 <?php

session_start();

if (!(isset($_SESSION['loginAdmin']) && $_SESSION['loginAdmin'] != '')) {
header ("Location: loginam.php");
}

else if (!(isset($_SESSION['loginAdmin']) && $_SESSION['loginAdmin'] =='')) { 
                                                 include('head2.php');  
                                                   }


else if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: login.php");
}

else if  (!(isset($_SESSION['login']) && $_SESSION['login'] =='')) { 
                                            include('head3.php');
                                                   }    

?>    

Please help, there's no error but its not functioning properly. Whenever I try to access the page where I have this code. And login as a user. It redirects to loginam.php(the page where the admin will login). But there's no problem when I log in as admin. It works properly. What do I do?

+2  A: 

if else is not valid. It's else if.

Other than that, it would help if you posted the parser error along with your code.

You're also not closing your <?php statement before opening it again.

Jan Kuboschek
php has elseif so there is no need for else if :)
AntonioCS
+1  A: 
Mihai Iorga
how do I avoid notices whenever the other condition is not true?I get an undefined index
This has been answered a dozen times already. http://stackoverflow.com/questions/2891042/setting-default-value-of-superglobal
Jan Kuboschek
+2  A: 

Use

if (condition)
{ 

}
else if (condition) {

}

Also Just to make things simpler .. try something like ..

function is_admin() {

if(isset($_SESSION['loginAdmin']) && $_SESSION['loginAdmin'])
  return true;
} else {
  return false;
}
}

and then check

if(is_admin()) {
///admin block
} else {
//admin login
}



if(is_user()) {
///user block
} else {
//user login
}
Wbdvlpr