Jump to content

[SOLVED] Login and logout links


JCS1988

Recommended Posts

I am working on my class final project.. still.. and I have run into another small problem. I have a registration system setup for users, but I would like the "Login" link at the top to be displayed when the user is not logged in, and the "Logout" link to be displayed when the user is logged in. Right now both links are at the very top of the page, regardless if the user is logged in or not. I setup this page http://jcs5325.aisites.com/gallery.php to require authorization to view the content, otherwise you are directed to an access denied page.

 

I have the site setup so anyone willing to help me can see how it works. Go to http://jcs5325.aisites.com/ and you will see both "Login" and "Logout", but for a page like this (no authorization required) I would like the "Logout" link to be hidden. The page that I have authorization only set on is the "Galleries" page. Now if you click login and provide a valid login (use the test one listed below) then you will be able to access the "Galleries" page, but "Login" and "Register" links will still be displayed at the top, when they really should be hidden for a member. I'll attach some scripts below, I'm not sure what ones will be needed to help me with this problem but I'll use my best judgment. If anything else is needed let me know, thanks a bunch in advance!

 

Username: jcsimmers

Password: 203633

 

On the galleries page I have this script at the top to check for the user info. As you can see if there is no user information present they will be taken to the login page.

<?php
//Start session
session_start();
//Check whether the session variable
//SESS_MEMBER_ID is present or not
if(!isset($_SESSION['SESS_CUSTOMER_ID']) || (trim($_SESSION['SESS_CUSTOMER_ID'])=='')) {
	header("location: login.php");
	exit();
}
?>

 

 

Now for the main index page I don't have any authorization scripts so everything is displayed logged in or not. Here I just want "Login" and "Register" visible, so I imagine I need some sort of script included to tell the page that's all I want.

Link to comment
Share on other sites

<?php
//Start session
session_start();
//Check whether the session variable
//SESS_MEMBER_ID is present or not
if(!isset($_SESSION['SESS_CUSTOMER_ID']) || (trim($_SESSION['SESS_CUSTOMER_ID'])=='')) {
	header("location: login.php");
}else{
header("location: Main index page");

	exit();
}
?>

Link to comment
Share on other sites

<?
$n=$_SESSION['n]])){
Session_destroy();
}
?>

<form action="register.php" method="POST">

       <div id="somerandomid">

               <p class="somerandomclass"><b>Welcome <?php echo($_SESSION["n"]); ?></b> <?php if (empty($_SESSION['n'])){?> Guest ( <a href="login.php">Log In</a> | <a href="register.php">Register</a> )<? }else{ ?> ( <a href="logout.php">Logout</a> ) <? } ?> </p>

       </div>

</form>

 

Try this code. Should help with you =]

Link to comment
Share on other sites

I forgot to mention that I am using a separate php file called auth.php, in this file I have the script I posted. I am using the require function to call in the auth.php on any of the pages I want members only.

 

I am using the include() function on all pages for my header.html file, which contains the login, logout, and register links. As you can see in the code below I have all of the links displayed right now. I would like to modify the header.html file based on whether the user is logged in or not.

 

User logged in: Shows the logout link in the header.html only, and hides the register and login links

 

User not logged in: Shows the login and register links from the header.html, and hides the logout link

 

I think I will need another script similar to auth.php for the pages that can be accessed by anyone. So then I will require('auth.php') for private pages, which will only display the page if the user is logged in and then remove the register and login link on that page from the header.html file.

 

Now for public pages lets say I made a file called public.php that I will require() at the top. I want this file to also authorize the users login info, but unlike auth.php, I would like the user to still be able to view that page even if they aren't logged in. What I would need to add though is something to tell the page to remove the logout link from the header.html file and only display login and register to the public.

 

Here is my header.html file with all the links I want at the top:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JCSimmers Photography</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
<link rel="stylesheet" href="lightbox.css" type="text/css" media="screen" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="stmenu.js"></script></head>
<body>
<div id="headerContainer">
<div id="header">
  <div id="topNav"><span class="headerText">My Account </span><span class="divider">|</span><span class="headerText"> Shopping Cart</span> <span class="divider">|</span> <a href="login.php">Login</a> <span class="divider">|</span> <a href="register.php">Register </a><span class="divider">|</span> Logout </div>
  </div>
</div>

 

 

Again, here is the auth.php file I require() on the private pages.

<?php
//Start session
session_start();
//Check whether the session variable
//SESS_MEMBER_ID is present or not
if(!isset($_SESSION['SESS_CUSTOMER_ID']) || (trim($_SESSION['SESS_CUSTOMER_ID'])=='')) {
	header("location: login.php");
	exit();
}
?>

 

 

And the content of the public.php file for public pages is what I need help with, if thats how it's done anyway. I just figured public and private pages would require a different script.

Link to comment
Share on other sites

Why don't you just use a boolean when the user logs in

 

// If logged in

$_SESSION['logged'] = true;

 

// If not

$_SESSION['logged'] = false;

 

 

<?php
session_start();
if ( $_SESSION['logged'] ) {
  echo 'Logout';
} else {
  echo 'Login';
}
?>

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.