Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Just Can't seem to get the variable!

Quote Reply
Just Can't seem to get the variable!
Hi:

I am working on a plug-in to limit the number of times different users can use my search function (Seems a couple big companies seem to think they can run 1000's of queries a day on my DB and not have to pay for it!)

Anyway, my thought it to track searches by IP and Username... those with no username (Guests) will get 10 searches, users 50, and anyone who "donates" will get unlimited.

So I am looking at inserting some code into search.pm, like this: (NOTE: My only changes to existing code are inside the "######## Dave Mod" comments)

Code:


sub search {

# ------------------------------------------------------------------

# Do the search and print out the results.

#

my $results = GT::Plugins->dispatch ($CFG->{admin_root_path} . '/Plugins', 'search_results', \&query, {});
if (defined $results->{error}) {

print $IN->header();
print Links::SiteHTML::display ('search', $results);

}

else {

######## Dave Mod

my ($rec) = @_;

my $username = $rec->{'Username'};
my $ip = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR} || 'None';
my $member = "No"; #$IN->param('member');
my $number;
my $count;

my $s_db = $Links::DB->table('Search_Count');


# Set date variable to today's date.

Links::init_date();
my $today = GT::Date::date_get();
my $date = $today;


# Update the search count unless this is the first search.

$s_db->add ( { IP => $ip, Date => $date, User => $username, Count => $member } );


if ($count >= $number) {
print $IN->header();

print Links::SiteHTML::display ('search_limit', $results);

}

else {

######## End Dave Mod


print $IN->header();
print Links::SiteHTML::display ('search_results', $results);

}


Note this is very simplified right now-- I am still trying to get the basics to work before I throw in all the checks! So $member is hard-coded to no, the date works, IP works, etc. I will eventually add in an update to update the count for existing records... but right now I am just trying to get the dang Username to appear!!!! (If I hard-code $username ="Guest"; or something, the above will add to database Search_Count, but trying to get the darn Username is not working)

Can anyone give me a pointer so I can get this variable to appear!

BTW, I did do a variable dump on the Search Resuts page, and both "Username" and "member" do show up, so I KNOW they there to grab- I just cannot seem to grab them!!!!

TIA!
dave

Big Cartoon DataBase
Big Comic Book DataBase

Last edited by:

carfac: Aug 24, 2003, 10:12 AM
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
Well, I finally worked through that one- took most of the day. But now I am to one final part that has me banging my head...

I created a table with 5 fields- ID, IP, Date, Username and Count. The plan was to count the new table to see if there were any rows with matching IP, Date and Username. If not, add a new record. That works fine. Having trouble getiing that record the second time and incrimenting the counter. (My short-term solution is pretty ugly... I just keep adding new records, and using count to compare the number of searches with the max allowed... It would be much cleaner to just update a single record)

Anyway, here is that part of the code... the problem seems to be getting the count variable OUT so I can increment it... the update statement works works fine! It is just that first line that gives me trouble!

my $rec = $s_db->select ( { IP => $ip, Date => $date, User => $username } , ['Count']);

$count = $rec->{Count} + 1;

$s_db->update ( { Count => $count }, { IP => $ip, Date => $date, User => $username }, { GT_SQL_SKIP_CHECK => 1 } );

###########

Once I get this licked, this is ready for prime time. So if anyone wants to limit the number of searches on their Links sites, I am happy to share!

dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
This snippet should work for you (only one update statement instead of a select and an update):
Code:
$s_db->update( { Count => \'Count + 1' }, { IP => $ip, Date => $date, User => $username });

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Just Can't seem to get the variable! In reply to
Yogi:

I owe you- thanks!
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
Yogi:

Whoops- spoke too soon. Yes, that code works FINE, but I also need to use the variable Count+1 to compare to the max allowed, so I know when to kick out a search as too many... so that was why I used select... to get that number out of the Search_Count database...
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
Hi,

Did you look at the code in jump.pm or detail_page.cgi ?

detail_page.cgi counts hits, and updates/deletes records in the hits table. Should be about the same thing.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Just Can't seem to get the variable! In reply to
Pugdog:

Yes, actually, I did... and the Ratings.pm too. Those all have the advantage of having the row ID number as input, so you just "GET" $id...

My problem here is that I do not have the row ID, I just know I want the row to match IP, Date and Username... (Well, I want Count from the row that matches those three items!) and making a call to get for $ip, $date, $username is not something I have been able to get working!
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
Thought it might help if I posted the whole code. It is messy at this point, with some of the code commented out. Right now, it continues to just add new lines for every search, so all the stuff dealing with updating a line is commented out.... but I think you can see where I would go with this...

Code:




######## Dave Mod

my $username = $USER->{Username};

my $ip = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR} || 'None';

my $member = $USER->{member};

my $number;

my $count;



if (!$username) {

$username = "Guest";

$member = "No";

$number = "10";

}

else {

if ($member eq "Yes") {

$number = "10000";

}

else {

$number = "50";

}

}

my $s_db = $Links::DB->table('Search_Count');

# Set date variable to today's date.

Links::init_date();

my $today = GT::Date::date_get();

my $date = $today;

# Update the search count unless this is the first search.

my $rows = $s_db->count ( { IP => $ip, Date => $date, User => $username } );



# Kick them out if they hit the limit

if ($rows >= $number) {

print $IN->header();

print Links::SiteHTML::display ('search_limit', $results);

}

else {

# if ($rows == 0) {

# $count = "1";

$s_db->add ( { IP => $ip, Date => $date, User => $username, Count => $rows} );

# Change $rows to $count if updating just one row...

# }

# else {



# my $rec = $s_db->select ( { IP => $ip, Date => $date, User => $username } , ['Count']);

# $rec->{Count} = $rec + 1;

# $count = $rec->{Count} + 1;

# $s_db->update( { Count => \'Count + 1' }, { IP => $ip, Date => $date, User => $username });

# }

######## End Dave Mod
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
For what it's worth, I modified the "ugly" code into a plug in now. I do not know how to make a plug-in make a new table (if someone would like to show me how, I can add that!), but as a basic plug in- you make the table- it works!

I think I will make another version to limit the number of detailed pages one can view...

But I still need to know how to make a line like:

my $rec = $s_db->select ( { IP => $ip, Date => $date, User => $username } , ['Count']);

work to pull out the Count variable- that would clean this up a LOT!


dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
Schematically, you could do something like
Code:
my $sth = $table->select( { [ your condition ] }, ['Count']);
if ($sth) {
if ($sth->fetchrow >= $number) {
[ error ]
}
else {
[ increase ]
}
}
else {
[ add new record ]
}
Basically, select does not return a record, you'll have to call 'fetchrow' (or something similar) to get the record (or in this case the value for 'Count').

I hope this helps.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Just Can't seem to get the variable! In reply to
yogi:

OK- FETCHROW!!! Cool!

I will get this working now- thank you!
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
Yogi:

Well, that ALMOST got it... the only major change (besides customizing your layout) was to remove the "[" and "]" from the condition...

Now it works great!!!!

If anyone wants to limit the number of searches, or the number of detail page views, just drop me a line! (I am sure it can be customized to limit other things, too!)

Thanks again Yogi!
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
Glad you got it working.

I put my comments in square brackets, so the [ ] were part of the comment in the select statement.... I'll be more precise next time.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Just Can't seem to get the variable! In reply to
No problem- I just noted that to help the next person.

It did start to see a problem after running it for a bit... sometimes it would re-write the same data more than once- like this:

27 66.219.219.188 2003-08-25 Guest 2
28 66.219.219.188 2003-08-25 Guest 3
dave

Big Cartoon DataBase
Big Comic Book DataBase

Last edited by:

carfac: Aug 25, 2003, 1:05 PM
Quote Reply
Re: [carfac] Just Can't seem to get the variable! In reply to
Hi,

If you have a working solution, maybe attach it, so that in the future you won't have to dig it up, and it's fresh on your mind now.

I'm going to be starting up the FAQ again, with code snippets and helps again.

Things seem to have settled down a bit, and a lot of information is still hiding in the forum, and needs to be organized.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Just Can't seem to get the variable! In reply to
Happy to:

Free for all to use- I just ask that if you make it better, you let us know!

The tarball is attached. You need to add a table called Search_Count, and it must have five fields:

ID-INT (5) Autoincrement, PRI Key

IP VARCHAR (16)

Date Date 0000-00-00

User Text or varchar (25)

Count SMALLINT (2)

I made an index on IP, Date, User to speed it up.

Also, I run a cron every night to empty the table, I would advise that!

This version has Yogi's code, but commented out, as I could not get that to work reliably (nothing personal Yogi, I am SURE the fault is mine, not yours!) So this version will put a new entry in for every search.

A bit of editing the code, and you can see where to change the threshholds for non-registered guests and users.

Oh, you also need a html page to tell users why they are not getting results... I called that search_limit.html
dave

Big Cartoon DataBase
Big Comic Book DataBase