Another display issue

Hey guys. Just another display issue. So I have a div which is used to display content by calling up lDisplay.


function lDisplay(){
    echo "You are logged in. <a href=\\"Logout.php\\">Logout</a>";
}

If they click on Logout, it unsets some sessions etc and echos


if(!$logged_in){
   echo "<h1>Error!</h1>\
";
}
else{
   echo "You have successfully <b>logged out</b>. Back to <a href=\\"Homepage1.html\\">Home</a>";
}

Problem is that it displays this underneath the div it should display in, and doesnt remove the logged in message. Somehow, I think I need to do something like associate the Logout href to a function or something, and on the same page do the logout code. Not sure what my best approach is. Any advise on what I can do?

cheers

not sure what the issue is, but a quick and simple logout.php could be something similar to this.

 
<?php
session_start();

//check if user is logged in
if(!isset($_SESSION['sessVar']) || $_SESSION['sessVar'] != 'some value') {
 die('<p>You are not an authorised user</p>');
}
 
//log the user out
unset($_SESSION['sessVar']);
session_destroy();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
 
<p>You are now logged out</p>
 
</body>
</html>

I am actually trying to make everything as neat as I can, and keep it in the same section. So in my first page I have

<div id="centerBar"> 
  <?php
    displayUser();
   ?>
  </div>

In this function I have some stuff, but the main part of it for this is

if($logged_in){
       lDisplay();
   }

So, it will display the result of the below function within the original div. So lDisplay is

function lDisplay(){
    echo "You are logged in. <a href=\\"Logout.php\\">Logout</a>";
}

So if the user is logged in, that echo will display in the original div. Now if they click on the logout link, I want the lDisplay echo to be removed from the div, and replaced with the echo from Logout.php.

So ideally, in lDisplay, underneath the echo would be something like
if(linkispressed){ //obviously incorrect
echo …
}
Would something like this be possible?

cheers

yes it’s possible, but the coding would be a bit messy if, if I understand you correctly, all you want to do is log out a user with a message saying they have logged out and optionally provide them with a link back to the login page.

imho it’s much simpler to take them to logout.php, log them out by destryoing the session, display a message saying they have logged out and give them a link back to the login page.

it seems, if I understand correctly, that from your logout.php you want to redirect back to the page where the logout link was clicked and display any logout message there.

I have been trying so many things now, but still no success. Even with the way you describe, it gets displayed underneath everything and not on its own page, and it doesnt remove the login success message.

What I was thinking of doing was redirecting back to the Login page if the Logout Link is pressed. Not sure If I can directly do this off a link though.

to be honest, I still haven’t got my head around exactly what you want to do, but it seems that after the logout link on a page is clicked, you want to logout the user and then display some message and/or link on the page the logout link was clicked.

that can be done, but imho the code would be messy and is not something I have ever really contemplated doing.

since all you want to do is logout a user and then give them a link back to your home or login page, I think the best and easiest way to do it is that when a user clicks the logout link on any page on your website, they are taken to logout.php (as in post 2) and under

<p>You are now logged out</p>

you can add a link to the home page if you like.

I just don’t understand why after the logout link is clicked you want to display a message on the page the logout link was clicked after the user is logged out.

Sounds like an ideal fit for a ‘flash message’. :slight_smile:

They tend to go something like…


<?php
function set_flash_message($key, $message, $namespace = '__flashMessage'){
  $_SESSION[$namespace][$key] = $message;
  return;
}

function get_flash_message($key, $namespace = '__flashMessage', $persist = false){
  $message = $_SESSION[$key];
  if(!$persist){
    unset($_SESSION[$key]);
  }
  return $message;
}

set_flash_message('user.logout', 'Thanks for logging out!');
header('Location: http://www.example.org/home.php');
exit;

#home.php
echo get_flash_message('user.logout');
?>

The code probably won’t work, but should give you a rough idea.

Its just that the way I had everything set up to display within a div, when a new page was being echoed, it would place it outside this div. I managed to get something which works, but outputting a javascript message box, and when they click ok, I redirect them back to the main page.

I have one new issue I just noticed. On my main page, the page where I do most of the html code (Basically a template page), I have

<script type="text/javascript" src="Validate.js"></script>

Now in my php file where I handle the log in, I have a form which gets echoed out. This calls up the Validate.js onsubmit.

In the same page, I also have another function which process the form, so gets the values out, compares them to values in the database etc. One of the jobs here is to see whether an option had been made on the form, and if it has, set some cookies.

 if(isset($_POST['remember'])){
		setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
		setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
	 }

I now get the common error

Warning: Cannot modify header information - headers already sent by

Its complaining that the script type code has already sent the header. However, I need to have this in the head of my main document as this is where it goes. Now if I do this, how can I then set the cookies? Is there anyway around this?

cheers

Just to give you an idea, this is how i call everything up


<?php
session_start();
include("LoginForm.php");
?>
<!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>L Page</title>
<link href="css/RegStyles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="JSValidate.js"></script>
</head>

<body>
<div id="container">
  <div id="leftBar">
    <ul>
      <li><a href="#">A</a></li>
      <li><a href="#">B</a></li>
      <li><a href="#">C</a></li>
      <li><a href="#">D</a></li>
    </ul>
  </div>
  <div id="centerBar"> 
  <?php
    displayStatus();
   ?>
  </div>
  <div id="rightBar"></div>
</div>
</body>
</html>

So the outcome of all processes I make goes into the centerBar div.

The cookies need to be set before the HTML or you can opt for quick and dirty giving your set-up isn’t adequate to handle what your trying to do (shown below).


<?php
session_start();
include("LoginForm.php");
ob_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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>L Page</title>
<link href="css/RegStyles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="JSValidate.js"></script>
</head>

<body>
<div id="container">
  <div id="leftBar">
    <ul>
      <li><a href="#">A</a></li>
      <li><a href="#">B</a></li>
      <li><a href="#">C</a></li>
      <li><a href="#">D</a></li>
    </ul>
  </div>
  <div id="centerBar"> 
  <?php
    displayStatus();
   ?>
  </div>
  <div id="rightBar"></div>
</div>
</body>
</html>
<?php 
$contents = ob_get_contents(); 
ob_end_clean();
echo $contents;
?>

Extremely dirty, but it will work. Othe rthan that the only other way is to move the cookie setting outside the HTML. That is why its always a good idea to separate display and application logic, than you don’t run into these issues.