Jump to content

Better way to do authentication of footer options..


Recommended Posts

I have a authentication system based on a php session and a mysql database with the following columns:

 

id username password admin

 

If the admin column has a value of "1" then the user is administrator.  Administrators can backup the database and add new users.

 

My question is this, I am generating a footer for my main page that has three "states," that for (1) a non-logged in user, (2) a logged in non administrator, and (3) an administrator; however, this code is cumbersome, and I want to make it more flexible so that I can use it on different pages, providing different options for those different states depending on the page.  For example, maybe on a gallery page I want an admin to be also able to delete photos, but not a non-logged in user, a logged in non administrator.  Is there a better way to code what I am doing, and somehow turn it into a function or class?

 

Please let me know if you need any clarification.

 

Thank you all in advance!

 

<?php

    //Determine if the user is authenticated, and if so, what administrative options to display in the footer
    echo "\t\t";
    if (!$_SESSION['username'] || !$_SESSION['password']) {
        echo "<a href='http://www.example.com/home/login' class='black_link'>login</a>";
        } else {
        db_connect();
        $result = mysql_query("SELECT count(id) FROM users WHERE password='$_SESSION[password]' AND username='$_SESSION[username]'") or die("Couldn't query the user-database.");
        $num = mysql_result($result, 0);
        if (!$num) {
            echo "<a href='http://www.example.com/index.php5?id=login' class='black_link'>login</a>";
            mysql_close();
            } else {
            $result2=mysql_query("SELECT * FROM users WHERE username='$_SESSION[username]'");
            $admin=mysql_result($result2,$q,"admin");
            if ($admin == 1) {
                echo "<a href='http://www.example.com/home/logout' class='black_link'>logout</a> | <a href='http://www.example.com/home/add_user' class='black_link'>add user</a> | <a href='http://www.example.com/home/backupDBs' class='black_link'>backup mysql</a>";
                mysql_close();
                } else {
                echo "<a href='http://www.example.com/home/logout' class='black_link'>logout</a>";
                mysql_close();
                }
            }
        } 

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.