Hello, I try to call a header A if a user is logged in, and a header B if a user is not logged in on some pages

I haven't managed to do so, I've tried to create a php function, but didn't manage to make it work. Then, I setup a connection to the mySql database just to check if 0 users are connected (or 1), but it didn't work. This just includes the header in the if clause

 $result = mysql_query("SELECT * FROM members
                       WHERE username = '$username'");
if (mysql_num_rows($result) == 0)
{
    include ("header.php");
}
else 
    include ("loginHeader.php");

?>

can anyone help me with this? Is it easier to solve as above with mySql or is it easier to create a php function and then call it on the respective page?

Ove way to do this would be.

You should set a session_start(); at the beginning of your script.
If mysql_num_rows() returns > 0 then set a $_SESSION, then if your Session isset() then you can load the desired header.

Yeah, a session-stored variable or a cookie is definitely the way to go.

Do you still need help? i created one of these and it took me sometime to figure it out. Let me know if you still need help.

commented: Hello aflor, yes I still need help. I hadn't been looking into it for a few days +1

thanks for the input. I'm still struggling a bit
I've tried it this way

<?PHP session_start();

$username = $_SESSION['username'];

 // Connects to your Database 
     mysql_connect(...) or die(mysql_error()) ; 
     mysql_select_db("...") or die(mysql_error()) ; 

 $result = mysql_query("SELECT * FROM members
                       WHERE username = '$username'");
if (mysql_num_rows($result) > 0)
    $_SESSION['username'] = true;
{

    if ((isset($_GET['header'])) AND $_SESSION[''] = true AND file_exists($_GET['header'].'.php'))

    {   
        include $_GET['header'].'.php';
    }
    else
    {

        include 'loginHeader.php';
    }
?>

with this I get a Parse error: syntax error, unexpected $end in

what am I doing wrong?

well you have to pass your form value into a variable to pass it to a session $username = $_GET['username'] and then use the $_SESSION['username'] = $username; or $_GET['username'] = $_SESSION['username'];

thanks for your answers - I maybe have to check the variables stored as I always end up with the header in the if clause and never get the one in the else clause

it solved now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.