/    Sign up×
Community /Pin to ProfileBookmark

PHP login – IF ELSEIF statements

Hello all,

So I am working on a personal project with a login script that will limit certain sections of a website depending on how you signed up, or in essence your “privilege”. I know how to do the rest, however this is the first time I have done something like this, so I would like if someone could take a look at this code, and see if its correct. I don’t think there are any errors, but any comments are welcome!

[code=php]
<?php
if($_SESSION[‘loggedA1’]==1)
{
/*show html code 1 here */
}
elseif
($_SESSION[‘loggedA2’]==1)
{
/*show html code 2 here */
}
elseif
($_SESSION[‘loggedA3’]==1)
{
/*show html code 3 here */

}
else
{
/*kill the process */
}
?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJul 16.2015 — Is the session var a different one for EACH logged in user? Why not a consistent session var with a value that matches/indicates the user and compare on that?

[code=php]
switch ($_SESSION['loggedinuser'])
{
case 'A1':
(do a1 logic here)
break;
case 'A2':
case 'A5':
(do a2 or a5 logic here)
break;
case 'A3':
(do a3 logic here)
break;
(add other cases here as needed)
default:
(handle error here)
break;
}
[/code]
Copy linkTweet thisAlerts:
@UAL225authorJul 16.2015 — Yes, the session var is different one for each logged in user. I was thinking its easier to do the logic on the login script side when it pulls stuff out of the DB and validate and sort logic there, than rather have the logic placed into the nav HTML mix where this would be added.

Although both options work, just not sure which would be easier to pursue.
Copy linkTweet thisAlerts:
@ginerjmJul 16.2015 — It's a bit backwards (IMHO) to use a unique variable name for each user. Think long-term - you'll have to reference diff vars in your code all the time perhaps. Think about how many users you may end up with some day. Use one var with a unique value and you won't have so much code to modify.
×

Success!

Help @UAL225 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...