...

View Full Version : How to show different content depending on if the user is logged in?



MrTIMarshall
08-03-2012, 02:25 AM
Hello,

Currently I have no login system as I'm either going to need to find a good free one, wait until my friend's real life situations have been resolved or I hire a freelancer. However, I would like to set up the most basic, one account login system so I can implement the feature of seeing different content depending on whether the user is logged in or not.

Maybe this system works differently to my logic, hence why I am posting for help. Eventually, depending on the users level, they will see different content. I suppose my first question would be, is this done by id elements, so if logged in and their access is 1 show this, if 2 show this and so on....

Maybe I could use check boxes to show the differences, but nevertheless, I basically want to start working on the different things people see depending on their level and if they are logged in or not.

Best Regards,
Tim

tracknut
08-03-2012, 02:55 AM
You'd probably end up using PHP for the authentication solution. Leaving that aside for the moment, your individual pages could be structured something like this to get you rolling on the idea:


<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
</head>

<body>
<?php
$loggedin = true; // hardcode these three here for testing, they will be set
$username = "bob"; // by the auth system once we have one
$userlevel = 4; //

if (!$loggedin)
echo "<p>Cannot access this page without logging in</p>";
else {
if ($userlevel == 1) {
?>
<p>You are user level 1</p>
<?php
}
else
if ($userlevel == 4) {
?>
<div>
<p>Welcome to the special page for user level 4 masters</p>
</div>
<?php
}
}
?>
</body>
</html>


* completely untested code, I just typed it like this. You get your money's worth :)

Dave

MrTIMarshall
08-03-2012, 03:05 AM
Hiya Dave,

Thank you for your reply. If I did all my pages like your example, adding the "if user = logged in" and "If user level = 3", no matter how complex the actual login system ends up being like, is this the only/best way it's done?

I'm only asking as it would be a shame to go through everything, implementing this and then needing to edit it all again in the future. Nevertheless, this will get me into practice! :)

tracknut
08-03-2012, 03:13 AM
I couldn't tell you if this is the best way to do it. It certainly isn't the only way you could, but it is one reasonably simple way. Sorry for being vague here... I haven't done many auth systems, so I don't consider myself familiar with the various ways to do it, and was mostly just giving you a workable example.

Dave

MrTIMarshall
08-03-2012, 03:18 AM
I couldn't tell you if this is the best way to do it. It certainly isn't the only way you could, but it is one reasonably simple way. Sorry for being vague here... I haven't done many auth systems, so I don't consider myself familiar with the various ways to do it, and was mostly just giving you a workable example.

Dave

Well I did ask for basic, and there isn't much to implement this into at the moment, so if help comes along soon, there wouldn't be much to change :)

Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum