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

EDI X.12 PHP Mapping - resources needed
Localize current time in PHP
Translating php app with gettext
In RegEx, how do you find a line that contains no more than 3 unique characters?
Generating Luhn Checksums
Simple Php Echo
tcpdf - start with existing PDF document
php-curl script that saves images---actually captcha images
PHP can't connect to localhost XMPP server on port 5222
PHP Doctrine: generation problem?
Why is my mysql database timestamp changing by itself?
Access/Download server files, not in site root, with PHP
Convert enumerated records to php object
Access class static function via variable
Document root problem in Eclipse/PHP/Zend
AJAX only access

Categories

HOME
qt
events
apache-nifi
selenium-builder
code-formatting
hdfs
iverilog
typeerror
tcp
tweepy
match
clish
jquery-select2-4
sslhandshakeexception
heap-dump
cocos2d-android
exec-maven-plugin
boomi
cpu-usage
weex
bsd
tortoisemerge
introspection
tf-idf
jackrabbit-oak
sparkle
social-networking
credit-card
xenapp
matlab-app-designer
netbios
riotjs
quickcheck
cloveretl
creation
busboy
subclassing
uivisualeffectview
stat
virtual-memory
nuclio
xcode8.2
wysihtml5
viewmodel
cyanogenmod
cloudwatch
xcglogger
ol3-google-maps
gnome-terminal
1010
mpmovieplayercontroller
business-rules
http4s
nitrousio
gawk
annotatorjs
data-management
bilinear-interpolation
frame-grab
accessibilityservice
gulp-uglify
flush
ajax4jsf
lovefield
in-app-billing
astropy
green-threads
web-api
deque
text-align
python-curses
nsmutabledata
pass-by-value
treeline
screwturn
pre
mod-perl
django-sites
google-code-prettify
ldif
sunstudio
statechart
rikulo
ui-design
icenium
zend-pdf
audiotoolbox
getproperty
kaazing
autosize
kolite
infopath-2007
filelock
aptitude
mygeneration
ios-4.2
multiple-users
asdoc
rawcontacts

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App