Replace Login form with the logout form once logged in...

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
JsyJack DWcs5.5
Forum Newbie
Posts: 4
Joined: Mon Dec 19, 2011 2:21 pm

Replace Login form with the logout form once logged in...

Post by JsyJack DWcs5.5 »

Hi, I want to replace the login form (top right of home page) with the logout button once logged in on the same page. I have been using dreamweaver cs5.5 and it doesn't have an easy function for it. Can anyone help me or direct me to the right place.
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Re: Replace Login form with the logout form once logged in..

Post by php3ch0 »

how about using the session variable. When you log in a user

Code: Select all

$_SESSION['userid'] = $userid;
then you can check if the user is signed in by checking if the variable exists

Code: Select all

if($_SESSION['userid']) {
     //display sign out button
} else {
   //display sign-in button
}
JsyJack DWcs5.5
Forum Newbie
Posts: 4
Joined: Mon Dec 19, 2011 2:21 pm

Re: Replace Login form with the logout form once logged in..

Post by JsyJack DWcs5.5 »

OK I have got it to work, well only with showing 2 different links to a form (logout or login in) BUT I have used that in the main template, and it only works when I restrict a page.....

I can click through all the links on my site and stay logged in but it only shows the logout button on the pages that are restricted... I don't understand why, if I UN-restrict the page it goes back to showing login when I am already logged in :s
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Replace Login form with the logout form once logged in..

Post by social_experiment »

Can you paste the code here
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
JsyJack DWcs5.5
Forum Newbie
Posts: 4
Joined: Mon Dec 19, 2011 2:21 pm

Re: Replace Login form with the logout form once logged in..

Post by JsyJack DWcs5.5 »

This is the template code:


<!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">
<?
if (!isset($_SESSION)) {
session_start();
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<style type="text/css">
</style>
<script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="../SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<link href="../global.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
<div id="header">
<table width="100%" cellpadding="5">
<tr>
<td width="1022"><table>
<tr>
<td class="tablebg">Gamers</td>
</tr>
</table></td>
<td width="300" align="right" nowrap="nowrap" class="loginout">
<?php
if(isset($_SESSION['MM_Username']) && $_SESSION['MM_Username']!="")
{
echo("<a href='logout.php'>Logout</a> ");
echo($_SESSION['MM_Username']);// displays username
}
else
{
echo("<a href='login.php'>Login</a> / <a href='registernow.php'>Register NOW!</a> ");
}
?>
</td>
</tr>
<tr>
<td><ul id="MainNav" class="MenuBarHorizontal">
<li><a href="../home.php">Home</a></li>
<li><a href="#">Forum</a></li>
<li><a href="http://www.miniclips.com" class="MenuBarItemSubmenu">Games</a>
<ul>
<li><a href="#">C.O.D</a></li>
<li><a href="#">CounterStrike</a></li>
<li><a href="#">Planetside</a></li>
</ul>
</li>
<li><a href="../members.php">Members</a></li>
<li><a href="../contact.php">Contact</a></li>
<li><a href="../about.php">About</a></li>
</ul></td>
<td>&nbsp;</td>
</tr>
</table>
</div>
<div id="sidebar1">
<table>
<tr>
<td><p>News</p>
<p>Links</p>
<p>Message Box</p></td>
</tr>
</table>
</div>
<!-- TemplateBeginEditable name="contenteditable" -->
<div id="maincontent">
<table>
<tr>
<td><p>Welcome to the Jersey Gamers Website.</p></td>
</tr>
<tr>
<td>Hope we can supply you with a useful website !</td>
</tr>
</table>
</div>
<!-- TemplateEndEditable -->
<div id="footer">
<table width="100%">
<tr>
<td><p>Copyright</p>
<p>Disclaimer</p>
<p>Founders</p></td>
</tr>
</table>
</div>
</div>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MainNav", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
</html>



This is the code on the members page (restricted) that works...



<?php require_once('Connections/GamerData.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "admin,member";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;

// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "registernow.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_GamerData, $GamerData);
$query_members = "SELECT username FROM users";
$members = mysql_query($query_members, $GamerData) or die(mysql_error());
$row_members = mysql_fetch_assoc($members);
$totalRows_members = mysql_num_rows($members);
?>
<!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"><!-- InstanceBegin template="/Templates/maintemplate.dwt.php" codeOutsideHTMLIsLocked="false" -->
<?
if (!isset($_SESSION)) {
session_start();
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<style type="text/css">
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<link href="global.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
<div id="header">
<table width="100%" cellpadding="5">
<tr>
<td width="1022"><table>
<tr>
<td class="tablebg">Gamers</td>
</tr>
</table></td>
<td width="300" align="right" nowrap="nowrap" class="loginout">
<?php
if(isset($_SESSION['MM_Username']) && $_SESSION['MM_Username']!="")
{
echo("<a href='logout.php'>Logout</a> ");
echo($_SESSION['MM_Username']);// displays username
}
else
{
echo("<a href='login.php'>Login</a> / <a href='registernow.php'>Register NOW!</a> ");
}
?>
</td>
</tr>
<tr>
<td><ul id="MainNav" class="MenuBarHorizontal">
<li><a href="home.php">Home</a></li>
<li><a href="#">Forum</a></li>
<li><a href="http://www.miniclips.com" class="MenuBarItemSubmenu">Games</a>
<ul>
<li><a href="#">C.O.D</a></li>
<li><a href="#">CounterStrike</a></li>
<li><a href="#">Planetside</a></li>
</ul>
</li>
<li><a href="members.php">Members</a></li>
<li><a href="contact.php">Contact</a></li>
<li><a href="about.php">About</a></li>
</ul></td>
<td>&nbsp;</td>
</tr>
</table>
</div>
<div id="sidebar1">
<table>
<tr>
<td><p>News</p>
<p>Links</p>
<p>Message Box</p></td>
</tr>
</table>
</div>
<!-- InstanceBeginEditable name="contenteditable" -->
<div id="maincontent">
<table>
<tr>
<td><p>Members:</p></td>
</tr>
<tr>
<td><?php echo $row_members['username']; ?></td>
</tr>
</table>
</div>
<!-- InstanceEndEditable -->
<div id="footer">
<table width="100%">
<tr>
<td><p>Copyright</p>
<p>Disclaimer</p>
<p>Founders</p></td>
</tr>
</table>
</div>
</div>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MainNav", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($members);
?>


This is the home page that doesnt work....

<?php require_once('Connections/GamerData.php'); ?>
<?
if (!isset($_SESSION)) {
session_start();
}
?>
<!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">
<!-- InstanceBegin template="/Templates/maintemplate.dwt.php" codeOutsideHTMLIsLocked="false" -->
<?
if (!isset($_SESSION)) {
session_start();
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<style type="text/css">
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<link href="global.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
<div id="header">
<table width="100%" cellpadding="5">
<tr>
<td width="1022"><table>
<tr>
<td class="tablebg">Gamers</td>
</tr>
</table></td>
<td width="300" align="right" nowrap="nowrap" class="loginout">
<?php
if(isset($_SESSION['MM_Username']) && $_SESSION['MM_Username']!="")
{
echo("<a href='logout.php'>Logout</a> ");
echo($_SESSION['MM_Username']);// displays username
}
else
{
echo("<a href='login.php'>Login</a> / <a href='registernow.php'>Register NOW!</a> ");
}
?>
</td>
</tr>
<tr>
<td><ul id="MainNav" class="MenuBarHorizontal">
<li><a href="home.php">Home</a></li>
<li><a href="#">Forum</a></li>
<li><a href="http://www.miniclips.com" class="MenuBarItemSubmenu">Games</a>
<ul>
<li><a href="#">C.O.D</a></li>
<li><a href="#">CounterStrike</a></li>
<li><a href="#">Planetside</a></li>
</ul>
</li>
<li><a href="members.php">Members</a></li>
<li><a href="contact.php">Contact</a></li>
<li><a href="about.php">About</a></li>
</ul></td>
<td>&nbsp;</td>
</tr>
</table>
</div>
<div id="sidebar1">
<table>
<tr>
<td><p>News</p>
<p>Links</p>
<p>Message Box</p></td>
</tr>
</table>
</div>
<!-- InstanceBeginEditable name="contenteditable" -->
<div id="maincontent">
<table>
<tr>
<td><p>Welcome to the Jersey Gamers Website.</p></td>
</tr>
<tr>
<td>Hope we can supply you with a useful website !</td>
</tr>
</table>
</div>
<!-- InstanceEndEditable -->
<div id="footer">
<table width="100%">
<tr>
<td><p>Copyright</p>
<p>Disclaimer</p>
<p>Founders</p></td>
</tr>
</table>
</div>
</div>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MainNav", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
<!-- InstanceEnd --></html>
JsyJack DWcs5.5
Forum Newbie
Posts: 4
Joined: Mon Dec 19, 2011 2:21 pm

Re: Replace Login form with the logout form once logged in..

Post by JsyJack DWcs5.5 »

I have sorted the problems, thanks.
Post Reply