php


Checking user privileges before executing functions


This is more of a concept question. I would like to get an advice from very experienced members on how to better arrange my code. Often when handling user's requests we need to check their privileges in order to determine their rights to execute some function or method. The question is whether it is better to verify access rights prior to executing a function like so:
//handling some POST data from AJAX
//lets assume we have some function check_some_rights() which takes user id as argument and returns 1 if user has some right or 0 if he/she doesnt
if (check_some_rights($user)){ //the privileges are verified
some_function_that_does_some_stuff()
} else {//nope, this user got no right to do this
echo "Like hell you do!";//denying access to this user
}
Or is it better to check access rights as soon as execution of function is initiated like so:
//alternative solution with embedded privilege check
function some_function_that_does_some_stuff($user){
if (check_some_rights($user)){//the privileges are verified
//go on with the procedure
// ...
} else {
echo "Like hell you do!";//denying access to this user
}
}
I personally believe that the second approach would be better, because we wouldn't have to worry remembering to check privileges each time, but opinions of people with more experience are needed and would be highly appreciated. Thank you.

Related Links

PHP curly braces inside strings colour in Notepad++
How to clean background captcha image using imagick
How can I get Magento Each Session values by using Magento frontend cookie value through Web Service?
WooCommerce displaying variable description after variable price
if else condition in jquery ajax response
php fastcgi is crashing if the url contains &start_debug=1
Having issue modifying theme code to replace logo URL
Combination of php_opcache and IIS causing intermittent 500 errors
DOMPDF class not found
wordpress APi integration
How Remove extra tag in xml in php
How to add a custom year drop-down in place of Export CSV drop-down on admin panel for custom grid and filter the collection?
Sending boolean values with $.ajax to PHP
Loop row in bootstrap every 3 columns
Get friends name/id list form graph api into array
LAMP shows index.php in var/www/html instead of project

Categories

HOME
macos-sierra
gaming
paypal-ipn
visual-studio-2010
apk
websocket
dronekit-python
header
navigation
phpmqtt
wagtail
selection
log4j2
lda
phpstorm-2017.1
createjs
jquery-file-upload
mpu6050
outlook-restapi
sonicwall
scheduled-tasks
bazaar
pfobject
kamailio
restier
raima
hdmi
background-color
echosign
boolean-logic
bitbake
netbios
opnet
riotjs
rspec-rails
devtools
love2d
mars-simulator
aurelia-http-client
sbjson
windows-store
access-denied
elasticsearch-aggregation
httphandler
bit-shift
intellij-lombok-plugin
photoswipe
jgrapht
gtk#
connect-direct
swiperefreshlayout
emf-compare
kendo-treeview
adobe-reader
nitrousio
oracle-fusion-apps
controllers
libharu
fuzzer
jtag
gnucash
uibinder
frame-grab
certificate-authority
self
fanotify
dsa
netdatacontractserializer
modern.ie
nsmutabledata
nanomsg
sql-processor
jolie
encryption-symmetric
ildasm
dmake
jquery-mobile-flipswitch
coding-efficiency
two.js
thruway
cpu-time
windows-taskbar
profiles
lync-2010
django-sites
assetic
apache-shindig
frontbase
disjoint-union
ax
days
tfs-sdk
itunes-sdk
multiscaleimage
getopt-long
viewpagerindicator
uitextfielddelegate
microblogging
sql-parametrized-query
upshot
3des
dip
formal-semantics
mygeneration
jdownloader
tabbarcontroller
mysqli-multi-query
fuzzy-comparison
ntruencrypt
rootkit

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App