row248
row248

Reputation: 119

Function return

I try make navigation bar.

/* Only for admin */
function getAccessRights($links) {
    if ( isset($_SESSION['right']) && $_SESSION['right'] === ACCESS_ADMIN  ||
        isset($_COOKIE['right']) && $_COOKIE['right'] === ACCESS_ADMIN ) {
        $links[] = '<li><a href="messages.phtml">Read messages</a></li>';
        var_dump($links); // **Back, Login, Read messages.**
        return $links;
    }
}



 function drawNavBar() {
    if ( $_SERVER['PHP_SELF'] == "/form-msg.php" ) {
        $links[] = '<li><a class="back" href="index.php">Back</a></li>';
        $links[] = '<li><a href="admin.php">Login</a></li>';
        getAccessRights(); // I dont know what access have user. If he have admin access, he will see link "read messages".
        var_dump($links) // **Back, Login**
    }
    //some if
    makeNavBar($links); //makeNavBar it is function which do pattern for html
  }

Where link "Read messages"? Maybe you know a better way to do make navigation bar.

Upvotes: 0

Views: 58

Answers (3)

bitWorking
bitWorking

Reputation: 12665

In function drawNavBar

$links[] = getAccessRights();

And in function getAccessRights

$links = array();
$links[] = '<li><a href="messages.phtml">Read messages</a></li>';
return $links;

By the way..the naming of the function getAccessRights is not right and I would try to use a more generic approach to the rights management for example a Access control list.

Upvotes: 0

helmbert
helmbert

Reputation: 38004

The link is the return value of the getAccessRights() method, but you call this method ignoring the return value. Furthermore, getAccessRights() expects a parameter $links, but you do not pass one.

BTW: You determine a user's admin status by a flag in the $_COOKIE variable. This is highly insecure. The $_COOKIEvariable contains everything that is sent in the client's cookie. This means that the user can modify the cookie's contents (and make himself admin by editing his own cookie).

Upvotes: 1

noslone
noslone

Reputation: 1289

I would do something like this:

//returns true or false based on user rights
function isAdmin() {
    return (isset($_SESSION['right']) && $_SESSION['right'] === ACCESS_ADMIN  || isset($_COOKIE['right']) && $_COOKIE['right'] === ACCESS_ADMIN)
}

 function drawNavBar($links = array()) {
    if ( $_SERVER['PHP_SELF'] == "/form-msg.php" ) {
        $links[] = '<li><a class="back" href="index.php">Back</a></li>';
        $links[] = '<li><a href="admin.php">Login</a></li>';
        if(isAdmin())
            $links[] = '<li><a href="messages.phtml">Read messages</a></li>';

        var_dump($links) // **Back, Login**
    }
    //some if
    makeNavBar($links);
  }

Upvotes: 1

Related Questions