Jump to content

How can I functionise this?


TeddyKiller

Recommended Posts

I've got the best of it done with a function. The function requires "$user" and if the user isn't logged in, in some places it'll display a logged out user navigator. So without most the features.

Now.. I'm not able to do something similar to..

if($user){ 
//display logged in navigator
} else {
//display logged out navigator
}

in a function.

 

So far I have this (code below), I haven't tested it but it should only get the logged in users navigator. It's similar to above code really.. except my best shot in a function. check_user function will redirect users if they aren't logged in. So for pages in which they have no need to redirect I have a little.. check if they are logged in, and if so to go to the check_user funciton for further checking, else the check user function isn't called. There for $user isn't defined. For example..

if(isset($_SESSION['uid']) && isset($_SESSION['hash'])){
    $user = check_user($secret_key);
} else {
    session_unset();
}

Though in this case it may be possible to do $user = ''; for when the user isn't isn't logged in. Although that wouldn't quite work as the way the function is setup, it will still display a logged in users navigator as it only does if($user){ blah }

 

The function is below, can you help? Thanks!

 

function header_display($user) {
    $header = '<div id="extra">';
    if($user){
        $header .= '<span><a style="margin-bottom:8px;" href="#">My Account</a> | <a href="logout.php">Log Out</a></span>';
    } else {
        echo '<span><a style="margin-bottom:8px;" href="?action=register">Sign Up</a> | <a href="/">Log In</a></span>';
    }
    $header .= '</div>
    <div id="navigator"><ul id="topnav">';
    if($user){
        $header .= '<li><a href="/main.php">Home</a></li>
        <li><a href="/main.php?sk=wall">Horble Wall</a></li>
        <li><a href="/mail.php">Mail(';
            $query = mysql_query("select * from `sent_messages` where `status`='unread' and `to_id`='$user->id'");
            $number = mysql_num_rows($query);
        $header .= $number;
        $header .= ')</a></li>';
    } else {
        echo '<li><a href="/">Home</a></li>';
    }
    $header .= '</ul></div>';
    
    return $header;
}

Link to comment
https://forums.phpfreaks.com/topic/197573-how-can-i-functionise-this/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.