Login Session code snippets

Posted by: Beverleyh
Hi
Firstly, I'd like to say thanks for the great script - I've been playing with it today and it does everything I need.
I wanted to make some code snippets available as they may help others.
OK - First, we've established that we need this line at the top of every php page to carry across the login session:
Code:
<?php session_start(); ?>
Please note, you only need to use the <?php require("login.php"); ?> code aswell if you want the whole page protected and prompt a visitor to login before seeing it. If you just use the <?php session_start(); ?> code, any visitor can see your page but a visitor who has previously logged-in can be served additional/special content using the snippets below.
Now you can use the following code snippets in the body of your web page/HTML and tinker with then to suit your needs.

Logout link with username:

Code:
<?php if (isset($_SESSION['ls_user'])) { ?>
<a href="<?php $_SERVER['PHP_SELF']; ?>?ls_logout" rel="">Log out <?php echo $_SESSION['ls_user']; ?></a>
<?php } ?>

User-only content (for all registered users)

Code:
<?php if (isset($_SESSION['ls_user'])) { ?>
Hello <?php echo $_SESSION['ls_user']; ?> You are registered.
<?php } else { ?>
You are not registered - go away!
<?php } ?>

Specific-user content (only show content to a specific named user)

Code:
<?php if (isset($_SESSION['ls_user'])) { if ($_SESSION['ls_user']=="Jimmy") { ?>
You are the named user. Here is your special content.
<?php }} ?>

Multiple specific-user content (only show content to specific named users):

Code:
<?php if (isset($_SESSION['ls_user'])) { if (($_SESSION['ls_user']=="Jimmy") || ($_SESSION['ls_user']=="Katie") || ($_SESSION['ls_user']=="Robert")) { ?>
You are the named user. Here is your special content.
<?php }} ?>
Hope this helps somebody.

Beverley
Views: 562