Adding Logout link

Started by zubrag, June 16, 2007, 12:39:38 AM

zubrag

Password Protector will automatically logout user when browser is closed (if TIMEOUT_MINUTES is set to 0).
But script also supports a manual logout feature which you may find useful.

In order to implement manual logout feature you'll have to accomplish following steps:

1. Setup logout URL in the script

If you look at the password_protect.php source code you'll see following

// User will be redirected to this page after logout
define('LOGOUT_URL', 'http://www.example.com/');

2. Add Logout link on your website

Add following HTML code on your site pages wherever you want a logout link to be shown

<a href="http://www.example.com/path/to/protected/page.php?logout=1">Logout</a>

Upon logout script will redirect user to the URL you specified on step 1.

sabotage


dmax

Script works great. Also installed logout script which is exactly what i needed. There is 1 small problem, after logout if I hit the browser back button it takes me right back into the protected page I just logged out of.  Is there a way to fix this.  Cheers, -d

zubrag

Browser cached protected page. Add following code to all protected pages (to avoid caching):

  <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
  <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

The code needs to be placed somewhere between <head> and </head> tags.

For example page's HTML code was

<html>
<head>
  <title>Custom Title</title>
</head>
...

after change it would be


<html>
<head>
  <title>Custom Title</title>
  <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
  <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</head>
...

dmax

I tried this and it doesnt seem to work. Before I copied the script into my file, I converted the script into php.
Here's what it looks like inside my protect php page.

echo "  <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";
echo "<META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-CACHE\"><META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">\n";
echo "  \n";
echo "  \n";
echo "\n";
echo "<body>\n";
echo "    <meta name=\"Generator\" content=\"iWeb 1.1.2\" />\n";
echo "    <title>Calendar_1</title>\n";
echo "    <link rel=\"icon\" href=\"favicon.ico\">\n";
echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"Calendar_1_files/Calendar_1.css\" /><script type=\"text/javascript\" src=\"Calendar_1_files/Calendar_1.js\"></script>\n";
echo "  </head>\n";

dmax

Would it be easier to adjust the log out script so the redirect page ('LOGOUT_URL', 'http://www.example.com/');  opens in a new browser window?  If yes, how would I do this? Thank You! Cheers, -dmax

zubrag

Currently you have body tag inside header. This is not correct. Move echo "<body>\n"; after the </header>.

In order to open in a new browser window add target="_blank" to the logout url, like this:

<a href="http://www.example.com/path/to/protected/page.php?logout=1" target="_blank">Logout</a>

dmax

Hi, sorry for the trouble. It's still allowing the back button in safari browser to take me back into the secure page. Here's the code, I'm sure there's something wrong.

<head>
      <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";
      echo "
      <META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-CACHE\">
      <META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">\n";
      echo "  \n";
      echo "  \n";
      echo "\n";
      echo "
      <meta name=\"Generator\" content=\"iWeb 1.1.2\" />\n";
      echo "
      <title>Calendar_1</title>\n";
      echo "
      <link rel=\"icon\" href=\"favicon.ico\">\n";
      echo "
      <link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"Calendar_1_files/Calendar_1.css\" />
      <script type=\"text/javascript\" src=\"Calendar_1_files/Calendar_1.js\"></script>\n";
      echo "
   </head>\n";
   echo "
   <body>
      <body style=\"background: transparent url(Images/bckgrnd2.jpg) repeat scroll top left; margin: 0pt; \" onload=\"onPageLoad();\">\n";
         echo "
         <div style=\"text-align: center; \">\n";
            echo "
            <div style=\"margin-bottom: 0px; margin-left: auto; margin-right: auto; margin-top: 0px; overflow: hidden; position: relative;  background: transparent; text-align: left; width: 800px; \" id=\"body_content\">\n";
               echo "
               <div style=\"float: left; margin-left: 0px; position: relative; width: 800px; z-index: 0; \" id=\"nav_layer\">\n";
                  echo "
                  <div style=\"height: 0px; line-height: 0px; \" class=\"tinyText\"> </div>\n";
                  echo "
               </div>\n";
               echo "<a href=\"http://www.midnightlighting.com/Calendar.php?logout=1\">
                  <img src=\"Calendar_1_files/shapeimage_1.png\" alt=\"LOG OUT!\" title=\"\" id=\"id2\" style=\"height: 47px; left: 306px; position: absolute; top: 1px; width: 187px; z-index: 1; \" />\n";
                  echo "
                  <div style=\"height: 50px; line-height: 50px; \" class=\"tinyText\"> </div>\n";
                  echo "

tinem

First I like to thank you for this great script. I just have some problems that when closing the browserwindow it doesn't clear the access and I have put this code in the passwordprotected file as you suggest like this:
   <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
     <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

It's working if I click the Logout link but is there a way to get it functioning when closing the browserwindow beside what I have done?

Click on the map link from this site http://www.tinemuller.dk/Saving_User-Added_Form_Data/ and login as admin - adminpass and then you can see what I'm talking about.

Do you have a script for password protect a folder?


zubrag

Hi. I tried that url in IE and Firefox. Both logged out when i closed browser. What browser you are using?

tinem

Thanks for your quick reply.

Firefox 2.0.0.9 it's not functioning but Explorer 6 it's OK. I use xp pro if it matters. After you close the browser window try pasting the adress http://www.tinemuller.dk/Saving_User-Added_Form_Data/phpsqlinfo_add.php again and you will se the problem.

What about my other question about password protect a folder?


zubrag

I have 2.0.0.9. Opened that page, logged in, closed browser. Opened page again. Login dialog appeared. Not sure why your Firefox is working differently. I'm on XP pro also. Maybe firefox cached page. Try Ctrl+F5 on that page afte you log in.

tinem

It's still the same. So maybe I should leave out close the browser and only have click logout?

If you don't have a script for password protect a folder please tell me yes og no?

zubrag

Sorry, forgot to answer that one. No specific script for protecting folders, unless you protect each file in the folder using password protector. If you want to protect a folder try using .htaccess protection (should be some 'protect folder' menu in your webhosting account management to do that).