Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Simple login with cookie fix (static build)

Quote Reply
Simple login with cookie fix (static build)
  
As far as I can tell the issue with using Static pages and detecting when a user has logged in was never answered, aside from use the Dynamic build :-P

If your exporting your Static pages as PHP (Setup > Build Options > build_extension: .php) its a very straightforward fix to set it up so when users login they don't need to login again for 31 days (or what ever time you specify) and also once they're logged in they can have access to the members only features on your site with the static pages like showing the correct Login or Logout link.


In FileMan (Build > FileMan) do a search for the Authenticate.pm file and do a search for this line of code (help to use IE for this as you can do a Ctrl+F search within the browser window):

my $session_cookie = $IN->cookie ( -name => 's', -value => $session_id, -path => '/' );

and replace with:

my $session_cookie = $IN->cookie ( -name => 's', -value => $session_id, -path => '/', -expires => '+31d' );

and Save - the extra code sets the login cookie 's' to expire in 31 days.

Then add the PHP code to detect the cookie on any of your static pages eg. home.html

Code:
<?php
// Checks if the 's' cookie is empty on the browser
if (empty($HTTP_COOKIE_VARS["s"])) {
echo "<a href='http://www.yourdomain.com/cgi-bin/user.cgi'>Login</a>";
} else {
echo "<a href='http://www.yourdomain.com/cgi-bin/user.cgi?logout=1'>Logout</a>";
}

?>


<?php

if (!empty($HTTP_COOKIE_VARS["s"])) {
// Display members only feature here
}

?>

for your dynamic pages (search.cgi etc.) you can use the original LinkSQL code eg.

<%if Username%>
<a href="<%db_cgi_url%>/user.cgi?logout=1">Logout</a>
<%endif%>
<%ifnot Username%>
<a href="<%db_cgi_url%>/user.cgi">Login</a>
<%endif%>



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Quote Reply
Re: [Chas-a] Simple login with cookie fix (static build) In reply to
Any examples for Javascript use for this?

Robert
http://www.pcprofiles.com
PC Profiles and hardware reviews
Quote Reply
Re: [Robert_B] Simple login with cookie fix (static build) In reply to
   
yer here you go, tested on IE6 and Mozilla 0.8

Code:
<script language="JavaScript" type="text/javascript">
<!--

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}


// Tests if the session id value is set and if the length is over zero the user is logged in
if (getCookie("s").length > 0) {
document.write("<a href='http://www.yourdomain.com/cgi-bin/user.cgi?logout=1'>Logout</a>");
} else {
document.write("<a href='http://www.yourdomain.com/cgi-bin/user.cgi'>Login</a>");
}

if (getCookie("s").length > 0) {
// Display members only feature here
}

-->
</script>



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Last edited by:

Chas-a: Jun 8, 2004, 6:34 PM
Quote Reply
Re: [Chas-a] Simple login with cookie fix (static build) In reply to
I added this code to my static pages, and it always indicates "Logout" even when I am logged out. Any ideas?

EDIT - What I am trying to do is to be able to call the cookie from static pages, not built from GT. What code do I use? I have tried the following, and it always shows "Logout"

<script language="JavaScript" type="text/javascript">
<!--

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}


// Tests if the session id value is set and if the length is over zero the user is logged in
if (getCookie("s").length > 0) {
document.write("<a href='http://www.wiredbiz.com/cgi-bin/links/user.cgi?logout=1'>Logout</a>");
} else {
document.write("<a href='http://www.wiredbiz.com/cgi-bin/links/user.cgi'>Login</a>");
}

if (getCookie("s").length > 0) {
// Display members only feature here
}

-->
</script>

In Reply To:

yer here you go, tested on IE6 and Mozilla 0.8

Code:
<script language="JavaScript" type="text/javascript">
<!--

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}


// Tests if the session id value is set and if the length is over zero the user is logged in
if (getCookie("s").length > 0) {
document.write("<a href='http://www.yourdomain.com/cgi-bin/user.cgi?logout=1'>Logout</a>");
} else {
document.write("<a href='http://www.yourdomain.com/cgi-bin/user.cgi'>Login</a>");
}

if (getCookie("s").length > 0) {
// Display members only feature here
}

-->
</script>

Last edited by:

Jobu: Jul 16, 2006, 5:32 PM
Quote Reply
Re: [Jobu] Simple login with cookie fix (static build) In reply to
THis version works with Glinks...

Code:
<script language="JavaScript" type="text/javascript">
<!--

function getCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

// Tests if the session id value is set and if the length is over zero the user is logged in
if (getCookie("s").length > 0) {
document.write("<a href='http://www.yourdomain.com/cgi-bin/user.cgi?logout=1'>Logout</a>");
} else {
document.write("<a href='http://www.yourdomain.com/cgi-bin/user.cgi'>Login</a>");
}

if (getCookie("s").length > 0) {
// Display members only feature here
}

-->
</script>



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Quote Reply
Re: [Chas-a] Simple login with cookie fix (static build) In reply to
OK, so I have the following code in my static pages now. When the user is logged in, the script properly displays "Logout." However, when a user is not logged in, nothing is displayed. This means to me that the script must be partially working, but I can't figure out why it's not writing Login/Register when the user is logged out! Any ideas?

Code:
<script language="JavaScript" type="text/javascript">
<!--

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}


// Tests if the session id value is set and if the length is over zero the user is logged in
if (getCookie("s").length > 0) {
document.write("<a href='http://www.WiredBIZ.com/cgi-bin/links/user.cgi?logout=1'>Logout</a>");
} else {
document.write("<a href='http://www.WiredBIZ.com/cgi-bin/links/user.cgi'>Login/Register</a>");
}

if (getCookie("s").length > 0) {
// Display members only feature here
}

-->
</script>

Last edited by:

Jobu: Jul 21, 2006, 4:44 PM
Quote Reply
Re: [Jobu] Simple login with cookie fix (static build) In reply to
OK, so it looks like this is the error I am getting:

Error: 'length' is null or not an object

Last edited by:

Jobu: Jul 21, 2006, 6:27 PM
Quote Reply
Re: [Jobu] Simple login with cookie fix (static build) In reply to
Ok try this...

Code:


<script language="JavaScript" type="text/javascript">
<!--

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}


// Tests if the session id value is set and if the length is over zero the user is logged in
if (getCookie("s") != null) {
document.write("<a href='http://www.WiredBIZ.com/cgi-bin/links/user.cgi?logout=1'>Logout</a>");
} else {
document.write("<a href='http://www.WiredBIZ.com/cgi-bin/links/user.cgi'>Login/Register</a>");
}

if (getCookie("s") != null) {
// Display members only feature here
}

-->
</script>


Instead of checking the length of the s value (the session ) in the cookie the change above checks if the getCookie function returns null, if it doesn't return null the s value exists - in other words the user is logged in.

BTW if you're going to use this method to detect if a user is logged in or not it's good practise to offer a non-Javascript version using the noscript element. ie.

Code:
<noscript>
<a href='http://www.WiredBIZ.com/cgi-bin/links/user.cgi'>Login/Register</a>
</noscript>


More info here on noscript element:
http://www.w3.org/TR/html4/interact/scripts.html#h-18.3.1



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Last edited by:

Chas-a: Jul 22, 2006, 8:29 AM
Quote Reply
Re: [Chas-a] Simple login with cookie fix (static build) In reply to
Thanks for the help, I really do appreciate it.

This, however, is now showing both "Logout" and "Login/Register" -- both elements of the if/else are returning true. How does that make sense?!
Quote Reply
Re: [Jobu] Simple login with cookie fix (static build) In reply to
In the Mozilla browser (I'm using Firefox) open your Javascript console (from the Tools menu), Clear any error messages that may be there and with a page on your site that's using the JS above refresh the page. What error do you see?

You can also try adding an alert function to the script to print out what the getcookie function is returning, for example...

alert(getCookie("s"));

When a user is logged in the alert should return a string of characters for example 'de3wwsq34rree' <- this is the session id. Or if the user is not signed in it should return 'null'.
Quote:
Thanks for the help, I really do appreciate it.

No problem :-)




Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Quote Reply
Re: [Chas-a] Simple login with cookie fix (static build) In reply to
Never mind, I am an idiot. I think this works.

Thanks!

Last edited by:

Jobu: Jul 22, 2006, 11:33 AM
Quote Reply
Re: [Jobu] Simple login with cookie fix (static build) In reply to
I take it you had the user_sessions set to URL instead of cookies...



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Quote Reply
Re: [Chas-a] Simple login with cookie fix (static build) In reply to
Dumber, the reason why both Login and Logout where showing is that I had kept the hardcoded Login/Register in!

Here's the interesting thing... this is now working great in IE, but not in Firefox. In Firefox, on all of my static pages, logged in or not, "Logout" shows. Here is the code I am using:

Code:

<script language="JavaScript" type="text/javascript">
<!--

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}


// Tests if the session id value is set and if the length is over zero the user is logged in
if (getCookie("s") != null) {
document.write("<a href='http://www.WiredBIZ.com/cgi-bin/links/user.cgi?logout=1'>Logout</a> ");
} else {
document.write("<a href='http://www.WiredBIZ.com/cgi-bin/links/user.cgi'>Login/Register</a> ");
}

if (getCookie("s") != null) {
// Display members only feature here
}

-->
</script>

<noscript> <a href='http://www.WiredBIZ.com/cgi-bin/links/user.cgi'>Login/Register</a> </noscript>


I think it is because wheN i "logout" the cookie isn't set to null, but just as a 0 length... so maybe I need to add another "if" command in... I will try that.

I also notice that with FireFox, my dynamic pages always show Login/Register, and not Logout (when when I am logged in). The code is the standard:

Code:

<a href="<%config.db_cgi_url%>/user.cgi<%if user.Username%>?logout=1<%endif%>"><%if user.Username%>Logout<%else%>Login/Register<%endif%></a>

Last edited by:

Jobu: Jul 23, 2006, 7:22 AM