CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: 302 Found issue: Edit Log



nevadajim
New User

Dec 6, 2010, 8:09 AM


Views: 688
302 Found issue

I have a autosurf script which at sometimes gives me a 302 Found issue.

What is confusing is I can view a site in top frame great one time and next time I get the 302 Found instead. This is whats confusing me.

Here is part pl file that delivers the site in top frame.

Any help would be great

<c>

#!/usr/bin/perl

require "common.pl";

&Init;
#Check for member userlevel.
if ($userlevel >= 1) {

#Get user details.
$sth = $dbh->prepare(qq{
SELECT sitesviewed, sitesviewedstats, creditsearned, credits
FROM ${prefix}members WHERE loginid = '$loginid'
});
$sth->execute;
my ($sitesviewed, $sitesviewedstats, $creditsearned, $credits) = $sth->fetchrow_array();
$sth->finish;

$sth2 = $dbh->prepare(qq{
SELECT ratio
FROM ${prefix}account_types WHERE userlevel = $userlevel
});
$sth2->execute;
my (@arrs) = $sth2->fetchrow_array();
$sth2->finish;

my $ratio=$arrs[0];

#Get the ratio split up properly.
(my $r_sv, my $r_cred) = split(/:/, $ratio);

#Increment sites viewed.
$sitesviewed ++;

#We can maintain the ratio simply by asking the question "does the member earn any credits
#by viewing this site?" If the answer is yes, then we deliver a member site. If the
#answer is no, we deliver a paid site.
my $added_credit = sprintf("%.1f", $r_cred/$r_sv);

$creditsearned += $added_credit;
$credits += $added_credit;


#Build the common parts of the query.
my $categoryquery = "";
my $lastsitequery = "";
if ($formdata{'c'} > 0) {
$categoryquery = "AND ${prefix}sites.categoryid = $formdata{'c'}";
}
if ($formdata{'l'} != 0) {
$lastsitequery = "AND ${prefix}sites.url <> '$formdata{'l'}'";
}

#Build a list of sites to deliver.
my @sitelist = ();

################
### get weighting number
my $select_weighting=5;
my $weighting_selected=0;

my $sth = $dbh->prepare(qq{
SELECT ${prefix}history.viewhistory
FROM ${prefix}history
WHERE ${prefix}history.hloginid = '$loginid'
});
$sth->execute;

my $w1, $w2, $w3, $w4, $w5;

if ((my $viewhistory) = $sth->fetchrow_array()) {
($w1, $w2, $w3, $w4, $w5) = split(/:/, $viewhistory);
}else{
my $sth4 = $dbh->prepare(qq{
insert into ${prefix}history (hloginid,viewhistory,viewtime)
values ('$loginid','0:0:0:0:0', now())
});
$sth4->execute;
$w1=0;
$w2=0;
$w3=0;
$w4=0;
$w5=0;
}

while($weighting_selected ne 1){
if($w5<5){
$w5++;
$select_weighting=5;
}elsif($w4<4){
$w4++;
$select_weighting=4;
}elsif($w3<3){
$w3++;
$select_weighting=3;
}elsif($w2<2){
$w2++;
$select_weighting=2;
}elsif($w1<1){
$w1++;
$select_weighting=1;
}else{
$w1=0;
$w2=0;
$w3=0;
$w4=0;
$w5=1;
$select_weighting=1;
}

my $num_s = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM ${prefix}sites WHERE ${prefix}sites.weighting = $select_weighting")->[0];
if($num_s>0){
$weighting_selected=1;
my $sth4 = $dbh->prepare(qq{
update ${prefix}history
set ${prefix}history.viewhistory='$w1:$w2:$w3:$w4:$w5', ${prefix}history.viewtime=now()
WHERE ${prefix}history.hloginid = '$loginid'
});
$sth4->execute;
}
}


################
### check member sites and paid site number
$num_paid = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM ${prefix}sites WHERE ${prefix}sites.memberid = 'paid' and ${prefix}sites.weighting=$select_weighting")->[0];
$num_total = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM ${prefix}sites where ${prefix}sites.weighting=$select_weighting")->[0];
$num_member = $num_total - $num_paid;


if($num_paid<$num_member){


#Get sites.
$sth = $dbh->prepare(qq{
SELECT ${prefix}sites.id, ${prefix}sites.weighting
FROM ${prefix}sites
WHERE ${prefix}sites.memberid = 'paid' and ${prefix}sites.weighting=$select_weighting $categoryquery $lastsitequery
});
$sth->execute;

while ((my $site_id, my $site_weight) = $sth->fetchrow_array()) {
$sitelist[@sitelist] = $site_id;
$paid[$site_id]=1;
}
$sth->finish;

#####Paid Sites
(my $member_v, my $paid_v) = split(/:/, $settings{'member_paid_ratio'});

my $member_paid_ratio = $member_v/$paid_v;

my $member_site_limit = int($num_paid*$member_paid_ratio);

if($member_site_limit<1){
$member_site_limit=1;
}


my $limitquery="";
if (@sitelist) {
if($member_site_limit>$settings{'max_member_site'}){
$member_site_limit=$settings{'max_member_site'};
}
$limitquery="LIMIT $member_site_limit";
}


#####Member Sites
$sth = $dbh->prepare(qq{
SELECT distinct ${prefix}sites.id, ${prefix}sites.weighting, ${prefix}sites.numviews
FROM ${prefix}sites INNER JOIN ${prefix}members ON ${prefix}sites.memberid = ${prefix}members.loginid
WHERE ${prefix}sites.memberid NOT IN('paid', '$loginid') AND ${prefix}members.credits > 0
AND ${prefix}sites.approved = 'Y' and ${prefix}sites.weighting=$select_weighting $categoryquery $lastsitequery
ORDER BY ${prefix}sites.numviews ASC
#instead of commenting the above line out i changed it, not sure if it will work
$limitquery
});


$sth->execute;
while ((my $site_id, my $site_weight) = $sth->fetchrow_array()) {
$sitelist[@sitelist] = $site_id;
$paid[$site_id]=0;
}
$sth->finish;



}else{


#####Member Sites
$sth = $dbh->prepare(qq{
SELECT distinct ${prefix}sites.id, ${prefix}sites.weighting, ${prefix}sites.numviews
FROM ${prefix}sites INNER JOIN ${prefix}members ON ${prefix}sites.memberid = ${prefix}members.loginid
WHERE ${prefix}sites.memberid NOT IN('paid', '$loginid') AND ${prefix}members.credits > 0
AND ${prefix}sites.approved = 'Y' and ${prefix}sites.weighting=$select_weighting $categoryquery $lastsitequery
ORDER BY ${prefix}sites.numviews ASC
#instead of commenting the above line out i changed it, not sure if it will work
LIMIT $settings{'max_member_site'}
});
$sth->execute;
while ((my $site_id, my $site_weight) = $sth->fetchrow_array()) {
$sitelist[@sitelist] = $site_id;
$paid[$site_id]=0;
}
$sth->finish;


#####Paid Sites
(my $member_v, my $paid_v) = split(/:/, $settings{'member_paid_ratio'});

my $member_paid_ratio = $member_v/$paid_v;

my $paid_site_limit = int($settings{'max_member_site'}/$member_paid_ratio);

if($paid_site_limit<1){
$paid_site_limit=1;
}

my $limitquery="";
if (@sitelist) {
$limitquery="LIMIT $paid_site_limit";
}

#Get sites.
$sth = $dbh->prepare(qq{
SELECT ${prefix}sites.id, ${prefix}sites.weighting
FROM ${prefix}sites
WHERE ${prefix}sites.memberid = 'paid' and ${prefix}sites.weighting=$select_weighting $categoryquery $lastsitequery
$limitquery
});
$sth->execute;

while ((my $site_id, my $site_weight) = $sth->fetchrow_array()) {
$sitelist[@sitelist] = $site_id;
$paid[$site_id]=1;
}
$sth->finish;


}

#############
#If nothing is found, show the default site.
if (!@sitelist) {
$sitelist[0] = $settings{'defaultsite'};
}


#Select a site to show.
if (@sitelist == 0) {
print "Content-type: text/html\n\n";
print "No sites to show.\n";
close STDOUT;
exit;
}
my $random=rand();
my $random_num = int((@sitelist) * $random);

my $selsite = $sitelist[$random_num];

#Get site details.
my $url, $numviews, $viewedstats, $omloginid;
if ($paid[$selsite]==1) {
$sth = $dbh->prepare(qq{
SELECT ${prefix}sites.url, ${prefix}sites.numviews, ${prefix}sites.viewedstats
FROM ${prefix}sites
WHERE ${prefix}sites.id = $selsite
LIMIT 1
});
$sth->execute;
($url, $numviews, $viewedstats) = $sth->fetchrow_array();
$sth->finish;
} else {
$sth = $dbh->prepare(qq{
select ${prefix}sites.url, ${prefix}sites.numviews, ${prefix}sites.viewedstats, ${prefix}members.loginid
FROM ${prefix}sites, ${prefix}members
WHERE ${prefix}sites.memberid = ${prefix}members.loginid and ${prefix}sites.id = $selsite
LIMIT 1
});
$sth->execute;
($url, $numviews, $viewedstats, $omloginid) = $sth->fetchrow_array();
$sth->finish;
}

if($url eq ""){
$sth = $dbh->prepare(qq{
SELECT url
FROM ${prefix}sites WHERE id = $settings{'defaultsite'}
});
$sth->execute;
my (@arrs) = $sth->fetchrow_array();
$sth->finish;

$url=$arrs[0];
}

#Send user to site and close output channels. Also pass current site cookie.
print "Set-Cookie: currentsite=$url\; path=/\;\n";
print "Location: $url\n\n";
close STDOUT;

#Update site stats.
$numviews ++;
$viewedstats = statsinc($viewedstats);
$dbh->do(qq{
UPDATE ${prefix}sites
SET ${prefix}sites.numviews = $numviews,
${prefix}sites.viewedstats = '$viewedstats'
WHERE id = $selsite
LIMIT 1
});

#Update site owner's credits if it's not a paid site.
if ($paid[$selsite]==0) {
$dbh->do(qq{
UPDATE ${prefix}members
SET credits = credits - 1
WHERE loginid = '$omloginid'
LIMIT 1
});
}

#check the last update credit time of the member, have to wait above the counter limit
$sth = $dbh->prepare(qq{
SELECT count(*) as update_flag
FROM ${prefix}members WHERE now() - update_credit_timestamp >= $settings{'rotatetime'} and loginid = '$loginid'
});
$sth->execute;
(my $update_flag) = $sth->fetchrow_array();
$sth->finish;

if($update_flag>0){


#Update viewing member stats.
$sitesviewedstats = statsinc($sitesviewedstats);
$dbh->do(qq{
UPDATE ${prefix}members
SET sitesviewed = $sitesviewed,
sitesviewedstats = '$sitesviewedstats',
creditsearned = $creditsearned,
credits = $credits,
update_credit_timestamp=now()
WHERE loginid = '$loginid'
LIMIT 1
});
}

} else {
#Redirect to login page.
&GotoLogin;
}

&Destruct;

</c>


(This post was edited by nevadajim on Dec 6, 2010, 8:10 AM)


Edit Log:
Post edited by nevadajim (New User) on Dec 6, 2010, 8:10 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives