Post: [PHP] Basic Admin Function
10-27-2013, 12:09 AM #1
Dan
I'm a god.
(adsbygoogle = window.adsbygoogle || []).push({}); I had to create this function for my current project (custom CMS), and I figured I share it.

    
<?php
function isAdmin()
{
global $db; // Have to do this so the function can access $db in connect.php
$user = $_SESSION['username']; // I just use this due to lazyness. It's from login.php it holds a session for the user's username.
$funct = $db->prepare("SELECT * FROM user_table WHERE username =:username"); // The query is getting prepared for sanitization.
$funct->bindValue(':username', $user, PDO::PARAM_STR); // The query is getting sanitized, PARAM_STR represents the string data type in the SQL
$funct->execute(); // And the query is executed, plain and simple
$result = $funct->fetch(); // fetch(); is used to fetch a row from the table you're using in the $funct query.
if ($result['usergroup'] == 1) { // This is pulling the usergroup from the user table and is checking if the usergroup is equal to 1
return true; // Here you put what you want to display if they are an admin
} else {
return false; // And what you want to display here if they aren't an admin.
}
}
?>
Last edited by Dan ; 10-27-2013 at 07:00 PM.
10-27-2013, 04:47 PM #2
Sloth
Banned
Originally posted by Shadow View Post
I had to create this function for my current project (custom CMS), and I figured I share it.

    
<?php
function isAdmin()
{
global $db; // Have to do this so the function can access $db in connect.php
$user = $_SESSION['username']; // I just use this due to lazyness.
$funct = $db->prepare("SELECT * FROM user_table WHERE username =:username"); // Preparing is getting the query ready for sanitizing.
$funct->bindValue(':username', $user, PDO::PARAM_STR); // It gets sanitized here.
$funct->execute(); // And the query is executed
$result = $funct->fetch(); // Fetching a row.
if ($result['usergroup'] == 1) {
return true; // Here you put what you want to display if they are an admin
} else {
return false; // And what you want to display here if they aren't an admin.
}
}
?>


Nice share (Don't know shit about PHP :/) It'd be awesome if you could add comment tags explaining what certain functions do so that the "Skids" at least have some idea of what they are skidding.
10-27-2013, 07:00 PM #3
Dan
I'm a god.
Originally posted by Sloth View Post
Nice share (Don't know shit about PHP :/) It'd be awesome if you could add comment tags explaining what certain functions do so that the "Skids" at least have some idea of what they are skidding.


Went more into depth about what's happening as requested. :p

The following user thanked Dan for this useful post:

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo