Jump to content

[SOLVED] How do I use login session to show spesific areas when logged in?


thelberg

Recommended Posts

Hi,

I'm realy a newbie in PHP programming. (Using Dreamweaver for basic operations.)

 

Now I need to create a site where users who are logged in can get access to additional contents, compared to anonymus users who are NOT logged inn.

Example:

On a public website with information about shoes, anyone can see the pictures and description. What I want is when a salesperson or administrator in my companys staff is logged in, he/she will be able to see some more information - about prices and stuff..

 

In .ASP the command for this is

<% if (Session("MM_UserAuthorization")) = "Administrator" then %> {Enter the additional stuff here... Text, tables, pictures, buttons...} <% end if %>

 

Anyone know the similar code for .PHP?  ???

Link to comment
Share on other sites

This is what I have for my member section, more or less the same as what you're asking but you'll need to customise it to your needs.

 

<?php
session_start();

// If users aren't logged in then display error message and exit.

if($_SESSION['user_level'] == 0){
echo "You do not have access to this section. Please log in first.";exit;
}

// Users logged in successfully, display options.

echo "Welcome ". $_SESSION['username'] ."!<br /><br />";

echo "Member Options";

echo "Member Option 1";
echo "Member Option 2";
echo "Member Option 3";

// Special options for admins and developers.

if($_SESSION['user_level'] == admin){
include "admin/adminoptions.php";
}
if($_SESSION['user_level'] == dev){
include "admin/devoptions.php";
}

echo "<a href=logout.php>Logout</a>";

?>

 

This is assuming you already have a login script set up, as this requires sessions from the login script. Just change them to suit your needs.

Link to comment
Share on other sites

Thanks, just what I needed  :)

 

It worked well; both the section with "Member options" displayed, and including different files depending on wich level you are logged in.

 

But;

 

When an anonymus user NOT are logged in... That's difficult...

I can't get that right...

 

Here's my code based on my own loginprosedure:

<?php
session_start();

// If users aren't logged in then display this message.

if($_SESSION['MM_UserGroup'] ==  0){
echo "You are not logged in.";
}

// Users logged in successfully, display options.


echo $_SESSION['MM_Username'] ."</b><br /><br />";


?>

 

"...UserGroup'] ==  0..." I don't have any usergrups with level 0. Is there any way of telling the session that no one has logged on? I can't put thousands of anonymius users in their own usergroup??

 

My point is,

anonymus users will see some information, like including a file containing a recordset with some fields visible. When a registerd user log in, he or she will see more information! From another file containing the same recordset, but with some more fields visible now.

 

Your describtion solve this partialy. However, the missing link here is how to include one spesific file when nobody is logged in, and another file when somebody is logged in.

Link to comment
Share on other sites

You can use this tutorial also http://www.devarticles.com/c/a/PHP/Creating-a-Membership-System/1/

 

On the page you want to protect from unregistered users you could use something like this

 

<?php

    if (!is_authed()) //function to check if user logged in
    {
print ('You need to login to view this page, <a href="/login">click here</a> to login.');
include('login.php');
    }
    else
    {
             //display the page content
    }
?>

 

 

function to check if use logged in

<?php
function is_authed()
{
     if (isset($_SESSION['username']))
     {
         return true;
     }
     else
     {
         return false;
     }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.