Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

Using cookies to integrate W3T

Quote Reply
Using cookies to integrate W3T
Hi,

I'm attempting to fully integrate W3T into LinkSQL so that users can hop back and forth between the two sections while maintaing their user info. I've come to the conclusion that the best way to approach this is to set cookies on the links end that will be able to be read by both progs. Though I'm not sure how to approach this. Has anyone got any ideas that can help me get down to this?

Thanks
Quote Reply
Re: Using cookies to integrate W3T In reply to
CGI.pm has built in cookie support, as does Apache.

You might want to start with the CGI.pm docs, or the CGI.pm book.

Since cookes are by "domain" any program running at your domain should be able to access the cookie.

I haven't done much with cookies, but I'm sure in the next month or so I'll have to. User registration and tracking (keeping state from area to area) is more and more important for me as I try to integrate an intelligent shopping cart.



------------------
POSTCARDS.COM -- Everything Postcards on the Internet www.postcards.com
LinkSQL FAQ: www.postcards.com/FAQ/LinkSQL/








Quote Reply
Re: Using cookies to integrate W3T In reply to
I think I just succeeded...but I'll let you know after reading through 'cookilib.pl' I found out that they default to being set at the path they were set from. In this case I was braiing myself trying to understand why I couldn't get at them...for your own info, when you go to use 'em check to make sure that a path is being set in the cookies else you can't get at 'em from another directory...unless you are setting the domain...but that doesn't get used as a default.

Quote Reply
Re: Using cookies to integrate W3T In reply to
You should _really_ check those docs! There are a lot of tools in them to help you debug and see what the cookies are passing.

I don't know how WWWThreads is using cookies, or the user database, but it should be possible to modify one or the other, or to make a program that can sync the two database, such that a user ID can only be created if it doesn't already exist in both places, and it's created in both places. Any changes to one user record are passed along to another program that makes the similar changes in the other user database.

As far as reading the actual cookie -- that has to do with the cookie routines in each program.

I wonder if you could pull the cookie handling programs out of both programs and create one set of routines to deal with them. That would mean going over the code for where and how the cookies are used (making sure they are all the same) and then forcing the ones that don't go through the standard routines to go through them.....

Are you following?

I'm hedging a little because:

1) I really have put the cookies thing on the side (literally) I keep staring at the book, and finding something else to do <G>

2) There are a couple of people (Possibly even Alex and the WWWThreads author) already working on this project.
Quote Reply
Re: Using cookies to integrate W3T In reply to
Thanks for the advice...
I'll need some paperwork on this, 'cause it's got me baffled.

But I'm beginning to think it could be easier than rewriting entire pages of functions. Do you think if changed the &load_user and &authenticate routines to work off of cookies instead that the users info would get passed along?

I'm trying that now, though no success as of yet...though I have ironed it out so that I'm not getting any errors. But if I had errors I would at least be able to see if it was passing the variables through!

~rl
Quote Reply
Re: Using cookies to integrate W3T In reply to
Still a couple logical glitches...but IT WORKS! And still maintains the functionality of sessions...which I think I will use as a requirement to in order to allow access to add stuff to certain sections of the site, adding the benfit of increased security to the administrative tools!

Check it out...register an account, validate it and then jump back anf forth between the yellow pages & the Forums after you login.

http://www.mvsource.com/cgi-bin/testsite/page.cgi



[This message has been edited by phoule (edited March 08, 2000).]
Quote Reply
Re: Using cookies to integrate W3T In reply to
Everyone would be much more anxious to see the code changes you made!!! Smile

This has been a successful week for a bunch of people!

It starts to be a lot of fun, doesn't it??

Quote Reply
Re: Using cookies to integrate W3T In reply to
I'll try to get all the bits and pieces I made changes to.

First off in DB_Utils, you need to add 3 new subs and include them in your @EXPORT = qw/ ... /; line.
Code:
###########################################################################
# set_cookie - Sets a cookie.
# Usage : set_cookie('Name',"Value",?) ? = 0 or 1. 0 = temp, 1 = permanent
###########################################################################
sub set_cookie {

my @cookie = @_;
my ($cookie, $value, $type, $char);
my @Cenc = ('\;','\&','\+','\%','\,','\=','\:\:','\s');
my %Cenc = ('\;','%3B','\&','%26','\+','%2B','\%','%25','\,','%2C','\=','%3D','\:\:','%3A%3A','\s','+');

my $header = '';

for (my $i = 0; $i <= $#cookie; $i = $i + 3) {
($cookie, $value, $type) = @cookie[$i .. $i+2];
foreach $char (@Cenc) {
$cookie =~ s/$char/$Cenc{$char}/g;
$value =~ s/$char/$Cenc{$char}/g;
}
$header = 'Set-Cookie: ' . $cookie . '=' . $value . ';';
if ($type == 1) {
$header .= ' expires=' . 'Thu, 31-Dec-2020 23:00:00 GMT' . ';';
}
$header .= ' path=' . '/' . ';';
print "$header\n";
}
}


###########################################################################
# Get_cookie - Gets all the cookies and returns them in a hash
# Usage: my %cookie = &get_cookie;
###########################################################################
sub get_cookie {

my @cstuff = @_;
my ($cookie, $value, $char, %cookie);

my @Cdec = ('\+', '\%3A\%3A', '\%3D', '\%2C', '\%25', '\%2B', '\%26','\%3B');
my %Cdec = ('\+',' ','\%3A\%3A','::','\%3D','=','\%2C',',','\%25','%','\%2B','+','\%26','&','\%3B',';');

if ($ENV{'HTTP_COOKIE'}) {

foreach (split(/; /,$ENV{'HTTP_COOKIE'})) {

($cookie,$value) = split(/=/);

foreach $char (@Cdec) {

$cookie =~ s/$char/$Cdec{$char}/g;
$value =~ s/$char/$Cdec{$char}/g;

}

$cookie{$cookie} = $value;

}

}

return %cookie;

}

sub cookie_auth {
# ---------------------------------------------------------
# Checks the username/password to make sure it is valid, and
# returns a hash of the user info. If no parameters are provided,
# it gets the username/password from cookies. Returns undef if unable
# to validate or no username/password found.
#
my ($Username, $Password, $Language) = @_;
$Language = shift;

# print "Content-type: text/plain\n\n";
return undef unless $Username and $Password;
my $dbh = new Links: BSQL $LINKS{admin_root_path} . "/defs/Users.def";

my $query = qq!
SELECT Users.*
FROM Users
WHERE Users.Username = '$Username' AND Users.Password = '$Password'
!;
my $sth = $dbh->prepare ($query) or die "Can't prepare: $query. Reason: $!";
$sth->execute() or die "Can't execute: $query. Reason: $!";
my $user = undef;
if ($sth->rows) {
$user = $sth->fetchrow_hashref;
}
return $user;

}

I borrowed these three routines from W3T and ported them into Links, though you will need to add the "path=" in set cookie to make cookies accessible site wide. And use my version of &cookie_auth (w3t::authenticate) because we need to get it's output into a hash format.

Now in HTML_Templates change the load_user sub, to load based on cookies values rather sessions.

Code:
sub load_user {
# --------------------------------------------------------
# Add user information to hash ref by looking up session id.
#
# Commente out the original more effective way,
# that Links loads a users profile, and then
# authenticates the user...

my ($dynamic, $hash, $category_id) = @_;
return $hash unless (defined $dynamic and (ref $dynamic eq 'CGI'));

# if (($dynamic->param('s')) &#0124; &#0124; ($dynamic->cookie('s')) {
# my $s = $dynamic->param('s') &#0124; &#0124; $dynamic->cookie('s') &#0124; &#0124; return $hash;
# $USERS{$s} &#0124; &#0124;= &authenticate ($s);
# my $user = $USERS{$s};
#
# if (ref $user eq 'HASH') {
# if ($user->{Status} eq 'Editor') {
# $user->{isEditor} = $category_id ? &can_edit ($user->{User}, $category_id) : undef;
# }
# foreach (keys %$user) { $hash->{$_} = $user->{$_} unless (exists $hash->{$_}); }
# }
# return $hash;
#
# } else {

my %cookie = &get_cookie();
my $Username = $cookie{'Username'};
my $Password = $cookie{'Password'};
my $Language = $cookie{'language'};
$USERS{$Language} &#0124; &#0124;= &cookie_auth ($Username, $Password, $Language);
my $user = $USERS{$Language};
if (ref $user eq 'HASH') {
if ($user->{Status} eq 'Editor') {
$user->{isEditor} = $category_id ? &can_edit ($user->{User}, $category_id) : undef;
}
foreach (keys %$user) { $hash->{$_} = $user->{$_} unless (exists $hash->{$_}); }
}
return $hash;
# }

}
Note I left the meat of the old sessions code intact for reuse. I tried to get it so depending on wether or not $s is defined it will kick off a session based authentication but, I just keep getting errors. I also worked around defining $s in $USERS{$s} by using my $Language value as a header, this isn't a clean piece of code by any means, but it works...considering I'm not even sure what it is doing...I mean the differences betweens Array's and Hashes became real apparent while working on this!

Right now in user.cgi we need to set a cookie at login so we can make our users info available to the W3T script, as well as to our Links templates, seeing as we no longer use the sessions. So in the sub login_user just before:

Code:
# Create and insert a Session, but first remove sessions older then 1 day.
$db->do (qq!

Add in:
Code:
# Let's initialize some variables to be passed in to our cookie
my $User = $user_r->{'Username'};
my $Pass = $user_r->{'Password'};
my $Lang = $user_r->{'Language'};
# NOW LETS GET COMPATIBLE WITH W3T and set a usable cookie so if
# they wanna go see what's up at the fair grounds they'll already have
# paid to get in!

&set_cookie( 'Username', "$User", 1, 'Password', "$Pass", 1, 'language', "$Lang",1);

If you want other Users variables to be passed in to the cookie, just add them in using the same format. Well that's the bulk of it...I did notice that a few lines down, from where I added in the call to set the cookie at login, there is another cookie being set for the value of $s, at first I tried to use that to print my Username/Password & Language preference into a cookie, but I couldn't get it to print my values along with it, I'm sure it can be done but I just don't know how? SO I added in an extra sub. I'd call this a temporary patch until I fgiure out how to print it in the same $dynamic->cookie. The rest of the mod is basically getting your W3T to work off the same database as Links (tedious chore of renaming all your SQL queries, in all files) then you need to fill out your User Table with whatever info you want to collect from your visitors, making changes to any file that allows them to modify these fields. Then in your templates you need to have pretty much two separate files...one if your visitor is registered and one for non registered. Add to that the side board items that are SSI driven and in each template you actually contain 3 different files. One if it's a static page, one for dynamic users and one for unregistered dynamic users. And if you really want to get nifty...one for visiting Robots, give them their own special page so they can do a better job of placing your site on the search engines!

For an example of what one of my template pages look like send me an email at writecon@mvsource.com , there's probably alot I left out on account of the fact that I've made so many tiny changes to both programs to get them working as one but these are the major ones, to interconnect their usage of registered users without them having to login twice.
Quote Reply
Re: Using cookies to integrate W3T In reply to
phoule:

I wanted to do a similar thing. I have not yet installed wwwthreads on my server, but some questions come to my mind...

Why did you choose to make wwwthreads read the user database of links and not the other way round, which is what I thougth was easier (as I said I dont know much about wwwthreads)?

Do you think what you did would work with links 2.0? I guess it has to be the other way round in this scenario. I wanted to make it work with 2.0 as I have to make several modifications to links to make it work the way I want and I am more confident with 2.0 and the many mods that exist for it (I migth reconsider that though). Are there any other benefits in sql if you dont plan on having a large database?

BTW I went to your site and there are a lot of broken links but I guess its normal since its still in development. Can you keep us (or at least me) informed when you end development for your site? what you are trying to do seems very interesting.

Thanks
Quote Reply
Re: Using cookies to integrate W3T In reply to
Sorry about the broken links but yeah, I wrote the templates according to alot of different things I plan on adding on to the site, so there are lot of sections as of yet that need to have scripts coded for them.

I went the way I did, because the style of programming that Alex uses is alot easier to build upon than that of W3T and the code is cleaner.
Quote:
Why did you choose to make wwwthreads read the user database of links and not the other way round, which is what I thougth was easier
The user database is the same, if you register to LinkSQL you register to W3T and vice versa, I integrated the two programs to run off the same database structure...I also decided to go this route because the way I see my site, is there are two types of users, people who are maintaining it who will be logging in through the LinkSQL interface (which will be more secure using Sessions) and those who'll be using the site, both Islanders and Tourists who will use the W3T login, which was much easier to integrate the cookies in Links than rummaging about all of W3T's files to get it Running off of Sessions, which Rick is programming into threads anyway.

As far as making it work for Links 2.0...sorry but no, you need to be using a SQL database of some kind, unless you have a user table for Links 2.0 then I guess you could have your Links read off of W3T...but then you'd need LinkSQL anyway for all the necessary SQL statements to make it work. I highly recommend using SQL because it adds so much potential for building upon, and it's so much faster when it comes to searching. Considering I have over 1000 categories, just for the 'yellow pages' alone, I needed that extra power.

As for when I finish it up, I'll be glad to keep you informed. The idea is to create a miniturized version of all of Yahoo!, store sections and everything else they offer included...
Quote Reply
Re: Using cookies to integrate W3T In reply to
pugdog...thanks again for your thoughts with the cookies, they got me motivated to get the job done. I like the sounds of your ideas for a shopping system. I don't think mine wil be quite the same but some portions will be similar.

I tried installing minivend, but I got hung up by a bug in Term::ReadKey, which I don't have access to simply unistall as they suggest. What did you choose to use for your Shopping Cart? I was thinking about just using RediCart, but that doesn't allow me to maintain a transaction database that I can allow access to by my paying customers to help them track their sales...the store section I'm saving for last because I want to use something like DBMan to write the sales into and it's going to involve alot of marketing to get people interested in setting up shop, so I figure it can wait until when the site gets to be a highly visible one.

And when I say mini yahoo, my site is going to be like a localized yahoo for Martha's Vineyard. For now my 3 main sections are the yellow pages (which I gotta find a good name for) the real estate section, and the classifieds. My really big problem is lack of content to fill these sections with!

I definately hear you, I need like 5 or 6 clones...just for the needed data entry alone Wink
Quote Reply
Re: Using cookies to integrate W3T In reply to
Thanks phoule

Every one of you here seems to understand more than I do when it comes to these two programs integration. Can you take a look at what I am trying to do (below) and tell me if I am completely missing something? It seems logic and possible to me but who knows...

I want to have a site with a forum (wwwthreads) and links (a modified version of links 2.0, the user here adds drinks recipes, not links. there are categories like alcohol, no alcohol and subcategories like vodka, gin, etc. It works just like links. The reason I'll probably use 2.0 at first is that I seem to be able to modify it this way, while sql might be too much for me).

On the home page of my site (not links or wwwthreads home page, just a standard html page) I want to include the wwwtrheads Login, register and "you have x private messages" (probably on every page though that migth be too many sql calls from what I read) so a user can register to the forum or log in from outside the forum. The registration and log in is exactly the same as wwwthreads.

The use of links is the same: A user can search links (as I said actually recipes), browse the categories, everything except adding. When they try to add a link/recipe: If they are loged in (I guess I can pass this info in the cookies) they add a link/recipe in the standard way and a hidden field passes the username info to the database. If they are not, they get redirected to the login register page. There is no users db on links and links works just the standard way except that only if you are registered to the forum you are able to post links.

The reason I want it to make it this way is that I then want links to build the records with a field that says (as an example) "submitted by patagon" where patagon is a link to my wwwthreads profile (in the same way that a post by me is a link to my profile in the forum). So basically I want users to see wwwthreads registration as a "site registration".

I hope you understand what I mean (ask me if you dont please) and tell me if I am completely wrong, or there are some possible problems with this that I overlooked.

Thanks for reading this far...
Quote Reply
Re: Using cookies to integrate W3T In reply to
  
Quote:
mini yahoo

Sounds great! I got sidetracked today on the single page listing, by trying to add a shopping database into links.

Most people don't need the complexity of the shopping cart systems. They concentrate so heavily on security, they all lack ease of use features.

I need one I can add products to, with most products being only a qty 1,s o they get removed immediately as they are sold. That's a perfect "dynamic" use of Link SQL.

Secure transactions are great, but most people don't need or use them. Security of the user database is more important, actually.

I'm not trying to set up a fully functional store that will send out hundreds of items a day, but rather a high-end boutique, that is geared to smaller volumes of higher priced items.

For the high-quantity or volume stores, the existing systems are probably going to be much better.

But, if you think about an "auction" where the price is the "buy" price, that's closer to what I'm working on.

Because Links/perl should be able to pass information through to the transaction gateways, it should be possible for those with on-line accounts to pass the item totals and other information to the secure/transaction gateway, and have that process the charge cards, and return the user to the Links site.

Seems a lot of us are going in similar directions. I just wish I had a few clones of myself to help out to get this done!



[This message has been edited by pugdog (edited March 12, 2000).]
Quote Reply
Re: Using cookies to integrate W3T In reply to
patagon,

It sounds possible and can be done...but with Links 2 as you said there is no User database. So you would have to write your own calls to user database in the various Links files in order to accomplish this. Or add in some extra capabilities to W3T to handle what you want to do. The HTML_Templates.pm would need to be modified to link to the user database, as would all your add, modify, etc...if your not sure of SQL I would say your plan won't be feasible. I mean the User database is in a SQL table. You might manage to port some W3T routines into your Links 2 but with the DBSQL.pm module you would have a much easier time of it!

If you need this type of feature get LinkSQL and I'll share with you some of the code modifications I've made. Because what you want to do is exactly what I have done. The integration between the two is also alot easier with the free version of W3T. Though the pro version is alot cleaner and obviously has tons more features.

Don't be daunted by SQL, especially not with Links. Alex's DBSQL.pm module makes it easy to do some pretty complex stuff and is more than worth the price Wink

Quote Reply
Re: Using cookies to integrate W3T In reply to
Thanks again phoule!
I've downlpaded my version (full version because I really need the advanced features) of wwwthreads and looked at links 2.0 and you are rigth, it doesn't seem easy... (I like the fact that I wasn't so wrong theorically).

I think I will be able to pay for links sql (I already payed more for this site in time, effort and hosting bills, no regrets..). What scares me more than the fair price is the fact that I intend to use links for an entirely different purpose. I already made some experiments with 2.0 and was able to do almost everything of what I need, but using the many mods and forum documentation available for it. I really dont know about Links SQL. I've read many posts in the forum as well as the excellent info on postcards.com site but there are still a lot of things I dont understand. BTW I am a graphic designer. I consider programming a hobby that I love (but I dont fully understand). I am willing to give it a try though. Do you mind answering a few more questions? (dont worry the last ones)

I registered to your site and got an email giving me some validation code. I guess this is part of links sql but is there a way of making the registration exactly like wwwthreads? Meaning that you choose an username/password and from that screen go to your profile (if you want to fill it), withowt any validation code?

I also havent noticed a link to edit profiles but I guess this works just like wwwthreads alone and you can still display that links. Am I right?

Thanks
canitrot@interserver.com.ar
Quote Reply
Re: Using cookies to integrate W3T In reply to
Patagon,

For your two questions Yes & Yes...the Validation code is from LinkSQL, and you can easily make the User registration just like the W3T registration...I just chose to use the validation in order to make it a more secure registration process, in future I will be taking Credit Card numbers into the User table for the Merchants zone and I plan on hosting auctions...so I don't want potential problems to occur simply because one user got in under someone else's info! It is only a small precaution but well worth it. As far as the Editprofile link, I chose to make that accessible only to members who have validated their accounts, proving their email address. I could completely deny users access until they Validate their accounts but I want to give them a taste of the site. And by doing things this way, I'm just trying to keep people from registering with a real email and then changing their profile to be a completely different person, without my knowing or at least being able to prove it.

Oh yeah, and I forgot to add...don't be intimidated by SQL, I was at first too! But I took the plunge...and to be truthful, I couldn't see building any site that would hold more than 500 records, without using SQL. Especially without LinkSQL, it is a very clean and concise piece of coding...and I'll tell you I got pretty good with Links 2 but it was alot easier to get pretty good with LinkSQL!

[This message has been edited by phoule (edited March 13, 2000).]
Quote Reply
Re: Using cookies to integrate W3T In reply to
I couldn't get mini vend installed either. I had an undefined array error, and the support list is really not into helping.

Quote Reply
Re: Using cookies to integrate W3T In reply to
Hey that's funny...just as I'm editing my post to patagon...

Their support list, It's non existant! I've written to them like 3 times and still have heard nothing ;( I've also read alot about how hard it is to get acquainted with it, I don't think I'm going to use it. Though I would like still like to use MiniMate if I can get it setup as a standalone frontend Sales tracking system to a LinkSQL products table.

Though I'm holding off on the Merchants zone for now...this is one aspect I really want to make sure I get right the first time around.
Quote Reply
Re: Using cookies to integrate W3T In reply to
I also wanted to bring up another thought on Dynamic vs. Static use of Links, when you mentioned above about the quantity being only 1 and getting dropped as an example of good dynamic usage, I also coded in a way to make it work on static pages! Using SSI's it sort of works like Links 2's Hits tracking where you create a file for each Links qty and then include it where ever you want to display the number in stock, I never got around to actually getting a Cart installed nevermind getting it to rewrite the stockqty_<%ID%>.txt files whenever an item is bought, but that wouldn't be too hard to make a cart do, it's just the idea of it may be useful to others.
Quote Reply
Re: Using cookies to integrate W3T In reply to
The thing with SQL... is that you really don't need use external files -- just write directly to the database.

In links 2.0 you got a much greater performance boost by doing the updates once in a while, rather than every time.

With SQL, there is almost no reason to not do dynamic updates to the main database for hits, quantities, etc.

The Hits_Track table in LinkSQL tracks duplicate hits, not hits since last build.

I don't understand why there is a Build_Update, other than it was a logic-hold over from Links 2.0.

I've by passed it for everything I've done <G>

Since the Admin area no longer prints out all the "updated" stats, I wonder if that table will eventually be phassed out.

One thing I never did do is get the "ratings/votes" system integrated in the 1.11 release. I had it on a test copy of my 1.0 release. I was tracking all votes from 1-10, and recalculating the average vote and the hit distribution each update.

I put it on the back burner waiting for Jerry's mod, since it was much more complete than mine.

Quote Reply
Re: Using cookies to integrate W3T In reply to
So you've got all your scripts writing directly to the main Hits_Track, Rate_Track tables? That sounds like a really good idea...

Quote Reply
Re: Using cookies to integrate W3T In reply to
You need to be careful if you bypass it -- the Votes/Ratings are still updated from the build_update files.

You'd need to add a table for tracking the votes, then calculate the votes/ratings on each database pass. Not hard, if you think it out.

There might be some other obscure purpose, remember, my primary sites are "images" not external links. I've just started setting up some "links" sites so I have some databases that are similar to what most people here are running.

If Jerry doesn't show up soon, I may have to finish the program I was working on. No fancy graphics or anything, but the data is all collected, and the totals printed out.

Quote Reply
Re: Using cookies to integrate W3T In reply to
phoule:

I sent you an email about your project that you may not have seen, it's a simpe but important question so I'm posting it here. If you want to contact me by mail please do (canitrot@interserver.com.ar)

mail
--------------------
Hi Poule:

I hope you dont mind the direct contact, but we exchanged some messages on Gossamer Threads (http://www.gossamer-threads.com/scripts/forum/resources/Forum9/HTML/000762.html) about your project for links sql and wwwthreads integration.

I am ready to buy links sql (I already have wwwthreads). I wanted to know how things were going with your project before buying though. I have seen a lot of progress in your site but noticed that you didnt finished yet. Since the main reason I am going with the more expensive SQL option is the fact that you are doing the same, which means that is at least theorically possible and that I migth get some help from your experience (no compromise for that last one, of course), I wanted to know how you were doing, and most important if you encoutered any trouble that made your project impossible or harder than you thought at first.

I suspect this is not the case and I know developing a site takes time, but it would help to know if I'm wrong...

Regards
-------------------------

Thanks and I hope to hear from you!