[SOLVED] Members and Non-Member Content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasemhi
Forum Newbie
Posts: 13
Joined: Sun Aug 29, 2004 10:31 pm

Members and Non-Member Content

Post by jasemhi »

Hi- I have been working on this for a while but cannot figure this out. What I would like to do is have content for members (logged in users) that is slightly different than the content for non-members (non-logged in users). For example, I would allow members to have their own lists of content where non-members would have generic lists.

How would one go about this? Would I create a variable that shows a user is logged in as a member and then do an if statement that called information for members and an else statement that gives different info for non-members? Or would I send the non-members to a different folder in my site and have them surf around in that where members would surf the members folder?

Please help. I am going crazy staring at my screen and not being able to figure this out.

Thanks!
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

It can be done with a [php_man]switch()[/php_man] conditional. It would be easier to manage the site instead of having two seperate sites for users and anonymous guests. So that's what I recommend doing.

Base the conditional on if the user is logged in. $_SESSION and $_COOKIE both work fine for this.

I also recommend taking a look at some open source apps to see how they have it all setup and coded. That is a good way to learn some more advanced techniques in programming.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Bit of sudo code for your problem:

Code: Select all

-user requests page

if($cookie){
   //check to see if the cookie is valid - check with data in database or something
   if(valid cookie){
      $logged_in = TRUE
   }else{
      $logged_in = FALSE
   }
}else{
   //user is guest
   $logged_in = FALSE
}

echo page that is the same for all users

if($logged_in){
   echo special user only data
}else{
   echo  guest data
}
jasemhi
Forum Newbie
Posts: 13
Joined: Sun Aug 29, 2004 10:31 pm

Post by jasemhi »

Let me show you what I have. The following code is called from a session include and if the person isn't logged in they get the login form. If they are logged in they get the members page.

Code: Select all

<?php
session_start();
session_name('MyPHPSite');
header("Cache-control: private"); // Fix for IE

function login_check()&#123;
    if($_SESSION&#1111;'login'] != TRUE)&#123;
      myheader("Member's Area");
	  $member = 1; // not logged in
      include $_SERVER&#1111;'DOCUMENT_ROOT'].
              '/login_form.html';
      footer();
      exit();
   &#125;
&#125;

?>
How do I create members only data based on the switch that I have poorly coded? I have read about switch and this is my attempt, but it seems that $member==1 isn't working.

Code: Select all

<?php
	include $SERVER&#1111;'DOCUMENT_ROOT'].
	        '/member_box.php';
	switch ($member==1)
	&#123;
	case 1:
	echo 'become a member to create your own kanji list';
	break;

	default:
	include $_SERVER&#1111;'DOCUMENT_ROOT'].
                '/membersonly.php';
	&#125;
	?>
Thanks for your replyies. I hope to hear more form you soon.
Jason
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

What you need to do is add only the variable that you want to work with within the conditional's argument.

It's best explained here: http://php.net/switch (along with some good examples/comparisons)

:)
jasemhi
Forum Newbie
Posts: 13
Joined: Sun Aug 29, 2004 10:31 pm

Post by jasemhi »

Thanks, I read it and worked it out. I now have content for members and non-members!

Jason
Post Reply