cgm225 Posted February 9, 2008 Share Posted February 9, 2008 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(); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90276-better-way-to-do-authentication-of-footer-options/ Share on other sites More sharing options...
cgm225 Posted February 10, 2008 Author Share Posted February 10, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/90276-better-way-to-do-authentication-of-footer-options/#findComment-463260 Share on other sites More sharing options...
cgm225 Posted February 10, 2008 Author Share Posted February 10, 2008 (bump) Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/90276-better-way-to-do-authentication-of-footer-options/#findComment-463475 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.