View Full Version : PHP to PERL
rudi
Sep 4th 2004, 1:45 pm
Can somebody help me write "coop ad" code on PERL to use it on CGI pages?
chachi
Sep 4th 2004, 2:59 pm
Rudi, we have a perl version. Provided Shawn has no problem with you using it.
a389951l
Sep 4th 2004, 9:00 pm
If Shawn gives the okay, then I would be interested also in the perl verion of the script!!:D
rudi
Sep 4th 2004, 11:10 pm
chachi, can You place code here?
Thank You in advance.
digitalpoint
Sep 5th 2004, 1:15 am
I don't care, so long as it works the same.
a389951l
Sep 5th 2004, 5:22 pm
chachi,
Shawn has given his okay. Can you post it and share with the rest of us?
Thanks!
clickdoc
Sep 7th 2004, 1:30 pm
Hi all,
I'm the coder behind chachi's perl version. We actually have 3 versions. One is a cgi script meant to be included with <!--#include. The second is a perl library meant to be included with 'require' in another perl script to add an ad network routine to an existing perl program. The final one is a module meant to be 'use'd, that provides the ability to add it to and existing perl program running in a mod_perl environment where a 'require' might not work properly.
Each one requires the LWP::UserAgent module to be installed, I believe it's standard these days but I am not positive. I am including the cgi version meant to be called by #include here. This is only useful if you don't have php installed as Shawns php can also be #included and should be used before our version. If you need one of the other versions let me know. This version is mod_perl safe as an include and conforms to all of Shawn's caching requirments. This code is provided as-is, use at your own risk, etc, etc.
#!/usr/bin/perl
# $Id: ad_network.cgi,v 1.3 2004/08/14 07:47:57 cvs Exp $
# $Revision: 1.3 $
use strict;
use LWP::UserAgent;
srand;
use vars qw/$ad_file $ad_url $VERSION $ad_network/;
# use ini subroutine with global vars, usefull for mod_perl
sub init {
### User Variables
# edit ad_file to set the path to the ad_network_ads.txt file
# default is to look in the same directory as where the script runs
# just create a blank file in that directory that is writable by the
# web server process and this script will set it up properly
$ad_file = 'ad_network_ads.txt';
# change type= here to the type of link you wish to serve
$ad_url = 'http://ads.digitalpoint.com/network.php?type=link';
### end User Variables
$VERSION = sprintf "%d.%03d", qq$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
$ad_network = '';
}
init();
print qq{Content-type: text/html\n\n};
if(-w $ad_file) {
if(open(FILE,"$ad_file")) {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
# if we haven't hit our 101 cache limi or
# the timestamp is less than an hour old
if(@ads < 101 || $ads[0] + 3600 < time()) {
my $ua = new LWP::UserAgent;
$ua->agent("Ad-Network-Perl/$VERSION)");
my $req = HTTP::Request->new(GET => $ad_url);
my $res = $ua->request($req);
if($res->is_success) {
my $content = qq{<!-- } . time() . qq{ -->};
$content .= $res->content;
push(@ads,$content);
}
if(@ads > 101) {
shift @ads;
shift @ads;
}
$ads[0] = time();
if(open(FILE,">$ad_file")) {
print FILE join('<ad_break>',@ads);
close(FILE);
}
$ad_network = pop(@ads) if(@ads > 1); # dont get a timestamp
} else {
$ad_network = $ads[int(rand(@ads-1))+1]; # again dont get a timestamp
}
$ad_network .= qq{<!-- an-hl -->};
} else {
warn qq { could not open file $ad_file because $! };
}
} else {
$ad_network = 'You must set the "ad_network_ads.txt" file to be writable.';
}
$ad_network =~ s/\<\!-- [0-9]* --\>//g;
print $ad_network . qq{\n};
clickdoc
Sep 7th 2004, 1:40 pm
some of the formatting got lost but it should still work just the same.
a389951l
Sep 7th 2004, 9:23 pm
Clickdoc,
Thanks!! I got it working with minimal effort.
a389951l
Oct 4th 2004, 12:06 pm
Any chance of getting updated code to allow for multiple ads for us using some perl for our sites?
clickdoc
Oct 4th 2004, 5:04 pm
Any chance of getting updated code to allow for multiple ads for us using some perl for our sites?
What did you have in mind? The code provided if put in an include can be called more than once on a page and will produce different results each time. I don't think I am clear on exactly what you are looking for but if you let me know I'll see what I can do.
clickdoc
Oct 4th 2004, 5:09 pm
Sorry didn't see that multiple ads per page are now allowed. I'll have some updated code here in a bit.
clickdoc
Oct 4th 2004, 5:38 pm
Ok here is the updated version to support multiple ads. The new config variables are ad_count (# of ads) and ad_separator (text/html to put between ads). Are there some [] tags i can put around the code to keep the formatting when posting?
#!/usr/bin/perl
# $Id: ad_network.cgi,v 1.4 2004/10/04 07:27:07 cvs Exp $
# $Revision: 1.4 $
use strict;
use LWP::UserAgent;
srand;
use vars qw/$ad_file $ad_url $VERSION $ad_network $ad_count $ad_separator/;
# use ini subroutine with global vars, usefull for mod_perl
sub init {
### User Variables
# edit ad_file to set the path to the ad_network_ads.txt file
# default is to look in the same directory as where the script runs
# just create a blank file in that directory that is writable by the
# web server process and this script will set it up properly
$ad_file = 'ad_network_ads.txt';
# change type= here to the type of link you wish to serve
$ad_url = 'http://ads.digitalpoint.com/network.php?type=link';
# set this to the number of ads you want to display on each page
$ad_count = 5;
# set this to the html you want to separate the ads ex: <br> |
$ad_separator = '<br>';
### end User Variables
$VERSION = sprintf "%d.%03d", qq$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
$ad_network = '';
}
init();
print qq{Content-type: text/html\n\n};
if(-w $ad_file) {
if(open(FILE,"$ad_file")) {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
# if we haven't hit our 101 cache limi or
# the timestamp is less than an hour old
if(@ads < 101 || $ads[0] + 3600 < time()) {
my $ua = new LWP::UserAgent;
$ua->agent("Ad-Network-Perl/$VERSION)");
my $req = HTTP::Request->new(GET => $ad_url);
my $res = $ua->request($req);
if($res->is_success) {
my $content = qq{<!-- } . time() . qq{ -->};
$content .= $res->content;
push(@ads,$content);
}
if(@ads > 101) {
shift @ads;
shift @ads;
}
$ads[0] = time();
if(open(FILE,">$ad_file")) {
print FILE join('<ad_break>',@ads);
close(FILE);
}
for(my $i=0;$i<$ad_count;$i++) {
if(@ads > 1) { # dont get a timestamp
$ad_network .= pop(@ads);
$ad_network .= qq{<!-- an-hl -->};
$ad_network .= $ad_separator if($ad_count > 1);
}
}
} else {
for(my $i=0;$i<$ad_count;$i++) {
$ad_network .= $ads[int(rand(@ads-1))+1]; # again dont get a timestamp
$ad_network .= qq{<!-- an-hl -->};
$ad_network .= $ad_separator if($ad_count > 1);
}
}
} else {
warn qq { could not open file $ad_file because $! };
}
} else {
$ad_network = 'You must set the "ad_network_ads.txt" file to be writable.';
}
$ad_network =~ s/\<\!-- [0-9]* --\>//g;
print $ad_network . qq{\n};
a389951l
Oct 4th 2004, 5:44 pm
Excellent & Thanks!
clickdoc
Oct 5th 2004, 10:25 am
if anyone is using it as an include and wants to be able to specify the number of ads to show and the separator in the include tag ie:
#include virtual="/cgi-bin/ad_network.cgi?ad_count=5;ad_separator=:"
let me know. the one here is written to be as fast as possible and hence does not parse any cgi input. however i have a version that can support changing the number of ads per page in the include without needing multiple versions of the script.
clickdoc
Oct 11th 2004, 3:26 pm
Hey guys it has come to my attention that this code may be contacting DP's server more often than it should. I have a fix on the way that will be available soon.
clickdoc
Oct 11th 2004, 9:02 pm
Ok guys. Here is the new perl version to address the problem with contacting the servers too frequently on heavily loaded sites. Also prevents the file from being clobbered by the same. Also added a little feature where you can specify the # of ads and the ad separator in the URL ex:
ad_network.cgi?ad_count=5;ad_separator=|
The defaults are count 5 and separator br (html br). You can hard code them as before with the ad_count and ad_separator variables or send them in the include paramaters.
Enjoy!
#!/usr/bin/perl
# $Id: ad_network.cgi,v 1.5 2004/10/12 02:54:29 cvs Exp $
# $Revision: 1.5 $
use strict;
use LWP::UserAgent;
use Fcntl qw(:flock);
srand;
use vars qw/$ad_file $ad_url $VERSION $ad_network $ad_count $ad_separator/;
# use ini subroutine with global vars, usefull for mod_perl
sub init {
### User Variables
# edit ad_file to set the path to the ad_network_ads.txt file
# default is to look in the same directory as where the script runs
# just create a blank file in that directory that is writable by the
# web server process and this script will set it up properly
$ad_file = 'ad_network_ads.txt';
# change type= here to the type of link you wish to serve
$ad_url = 'http://ads.digitalpoint.com/network.php?type=link';
# set this to the number of ads you want to display on each page
$ad_count = 5;
# set this to the text or html you want to separate the ads ex: <br> |
$ad_separator = '<br>';
if($ENV{QUERY_STRING}) {
my @tags = split(/;/,$ENV{QUERY_STRING});
foreach(@tags) {
my ($name,$value) = split(/=/,$_);
$ad_count = $value if($name eq 'ad_count');
$ad_separator = $value if($name eq 'ad_separator');
}
}
$ad_count = 5 if($ad_count > 5);
### end User Variables
$VERSION = sprintf "%d.%d", qq$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
$ad_network = '';
}
init();
print qq{Content-type: text/html\n\n};
if(-w $ad_file) {
if(open(FILE,"$ad_file")) {
# if we can't get a lock that means a process is writing, just serve an ad from the existing file
# and don't check the timestamp, this prevents a slow response from the DP server from hanging
# every web process waiting to read an ad, there is slim a chance we could end up with a partial ad
if(!flock(FILE, LOCK_SH|LOCK_NB)) {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
for(my $i=0;$i<$ad_count;$i++) {
$ad_network .= $ads[int(rand(@ads-1))+1]; # again dont get a timestamp
$ad_network .= qq{<!-- an-hl -->};
$ad_network .= $ad_separator if($ad_count > 1);
}
} else {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
# if we haven't hit our 101 cache limi or
# the timestamp is less than an hour old
if(@ads < 101 || $ads[0] + 3600 < time()) {
if(open(FILE,">$ad_file")) {
flock(FILE, LOCK_EX); # get an exclusive lock so we can write the ad_network_ads file
my $ua = new LWP::UserAgent;
$ua->agent("Ad-Network-Perl/$VERSION)");
my $req = HTTP::Request->new(GET => $ad_url);
my $res = $ua->request($req);
if($res->is_success) {
my $content = qq{<!-- } . time() . qq{ -->};
$content .= $res->content;
push(@ads,$content);
}
if(@ads > 101) {
shift @ads;
shift @ads;
}
$ads[0] = time();
print FILE join('<ad_break>',@ads);
close(FILE);
}
for(my $i=0;$i<$ad_count;$i++) {
if(@ads > 1) { # dont get a timestamp
$ad_network .= pop(@ads);
$ad_network .= qq{<!-- an-hl -->};
$ad_network .= $ad_separator if($ad_count > 1);
}
}
} else {
for(my $i=0;$i<$ad_count;$i++) {
$ad_network .= $ads[int(rand(@ads-1))+1]; # again dont get a timestamp
$ad_network .= qq{<!-- an-hl -->};
$ad_network .= $ad_separator if($ad_count > 1);
}
} # end else on check for 100 ads or timestamp older than one hour
} # end else on if flock
} else {
warn qq { could not open file $ad_file because $! };
} # end else on opening ad file
} else {
$ad_network = 'You must set the "ad_network_ads.txt" file to be writable.';
} # end else on writable ad file
$ad_network =~ s/\<\!-- [0-9]* --\>//g;
print $ad_network . qq{\n};
a389951l
Nov 22nd 2004, 1:36 pm
clickdoc - we need your help once again to port over the new PHP code to Perl. Thanks in advance! :D
clickdoc
Nov 29th 2004, 4:38 pm
Delete and re-create your ad_network_ads.txt file after installing this and makes ure all the permissions are set right. This version supports the 401 link caching and the 15 minute timeout, as well as the new format for DP to send down the link check code. Defaults to 5 ads, you can set the ad_count variable to serve less. As always the formatting will be lost but the code should still work fine. Let me know if anyone has any trouble.
ClickDoc
#!/usr/bin/perl
# $Id: ad_network.cgi,v 1.7 2004/11/29 23:04:16 cvs Exp $
# $Revision: 1.7 $
use strict;
use LWP::UserAgent;
use Fcntl qw(:flock);
srand;
use vars qw/$ad_file $ad_url $VERSION $ad_network $ad_count $ad_separator $ad_cache_size $ad_timeout_seconds/;
# use ini subroutine with global vars, usefull for mod_perl
sub init {
### User Variables
# edit ad_file to set the path to the ad_network_ads.txt file
# default is to look in the same directory as where the script runs
# just create a blank file in that directory that is writable by the
# web server process and this script will set it up properly
$ad_file = 'ad_network_ads.txt';
# change type= here to the type of link you wish to serve
$ad_url = qq[http://ads.digitalpoint.com/network.php?type=link&s=$ENV{'HTTP_HOST'}];
# set this to the number of ads you want to display on each page
$ad_count = 5;
# set this to the text or html you want to separate the ads ex: <br> |
$ad_separator = '<br>';
# ad cache size, current size is 401
$ad_cache_size = 401;
# timeout, current timeout is 15 minutes (900 seconds)
$ad_timeout_seconds = 900;
if($ENV{QUERY_STRING}) {
my @tags = split(/;/,$ENV{QUERY_STRING});
foreach(@tags) {
my ($name,$value) = split(/=/,$_);
$ad_count = $value if($name eq 'ad_count');
$ad_separator = $value if($name eq 'ad_separator');
}
}
$ad_count = 5 if($ad_count > 5);
### end User Variables
$VERSION = sprintf "%d.%d", qq$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
$ad_network = '';
}
init();
print qq{Content-type: text/html\n\n};
if(-w $ad_file) {
if(open(FILE,"$ad_file")) {
# if we can't get a lock that means a process is writing, just serve an ad from the existing file
# and don't check the timestamp, this prevents a slow response from the DP server from hanging
# every web process waiting to read an ad, there is slim a chance we could end up with a partial ad
if(!flock(FILE, LOCK_SH|LOCK_NB)) {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
for(my $i=0;$i<$ad_count;$i++) {
$ad_network .= $ads[int(rand(@ads-1))+1]; # again dont get a timestamp
$ad_network .= qq{<!-- an-hl -->};
$ad_network .= $ad_separator if($ad_count > 1);
}
} else {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
# if we haven't hit our cache limit or
# the timestamp is less than timeout
if(@ads < $ad_cache_size || $ads[0] + $ad_timeout_seconds < time()) {
if(open(FILE,">$ad_file")) {
flock(FILE, LOCK_EX); # get an exclusive lock so we can write the ad_network_ads file
my $ua = new LWP::UserAgent;
$ua->agent("Ad-Network-Perl/$VERSION)");
my $req = HTTP::Request->new(GET => $ad_url);
my $res = $ua->request($req);
if($res->is_success) {
my $content = qq{<!-- } . time() . qq{ -->};
$content .= $res->content;
push(@ads,$content);
}
if(@ads > $ad_cache_size) {
shift @ads;
shift @ads;
}
$ads[0] = time();
print FILE join('<ad_break>',@ads);
close(FILE);
}
for(my $i=0;$i<$ad_count;$i++) {
if(@ads > 1) { # dont get a timestamp
$ad_network .= pop(@ads);
$ad_network .= $ad_separator if($ad_count > 1);
}
}
} else {
for(my $i=0;$i<$ad_count;$i++) {
$ad_network .= $ads[int(rand(@ads-1))+1]; # again dont get a timestamp
$ad_network .= $ad_separator if($ad_count > 1);
}
} # end else on check for 100 ads or timestamp older than one hour
} # end else on if flock
} else {
warn qq { could not open file $ad_file because $! };
} # end else on opening ad file
} else {
$ad_network = 'You must set the "ad_network_ads.txt" file to be writable.';
} # end else on writable ad file
$ad_network =~ s/\<\!-- [0-9]* --\>//g;
print $ad_network . qq{\n};
a389951l
Nov 30th 2004, 4:22 pm
Tried it on one of my sites and it works fine. Thanks again!
General Grant
Dec 21st 2004, 3:31 pm
I'm trying to use this with MrRat's Amazon Product Feed (http://www.absolutefreebies.com/directory/aws/amazon-products-feed.html), and I've tried putting <!--#include virtual="http://www.clickhereforit.com/adnetwork.cgi?ad_count=5;ad_separator=|" --> in the template, but It's not working. Anything else I can do to get this to work with cgi?
a389951l
Dec 21st 2004, 3:38 pm
Can't put it in the template - need to put it in the actual .cgi file. At least that is what works for me.
General Grant
Dec 21st 2004, 3:57 pm
Well that sux, there is no html output with my cgi file, it's all template driven
a389951l
Dec 21st 2004, 4:08 pm
Doesn't have to be. I'll post later how I do it with Mr. Rat's script (AWS3 - haven't upgraded to AWS4 yet).
DFL
Dec 22nd 2004, 7:14 am
Hi everyone.
Could anyone help me out. Where do i have to put the long perl code? Do i have to do create a file called ad_network.cgi and put it there?
clickdoc
Dec 22nd 2004, 10:47 am
Grant does your template processor parse other includes? Includes are typically parsed by the web server so by the time you get to the template it's passed the SSI parser. We have a version that is designed to be called from inside a perl script as a perl routine, however we have not released it publicly, and will not till after the first of the year if we do at all.
DFL, yes you need to create a file named ad_network.cgi and place the perl code inside. Then set the variable for where your ad_networks_ads.txt file is located. Your web server process must have write privileges to this file. If the directory specified is writable by the web server process it will create the file on first load for you.
Feel free to ask any questions, I am traveling tho until after the first of the year so response time may be delayed.
DFL
Dec 22nd 2004, 11:33 am
Thanks for the reply clickdoc
I created the ad_network.cgi file and put it to the cgi-bin folder, same for ad_network.php and ad_network_ads.txt.
But how should i dispay the links. Do i have to put this to my templates (my templates are also in cgi-bin forder):
<?php
ini_set ("include_path", ini_get ("include_path") . ':../:../../:../../../:../../../../');
include ('ad_network.php');
echo $ad_network[0];
?>
Im asking because it doesnt work, so im probably doing something wrong.
General Grant
Dec 22nd 2004, 11:35 am
I have it fixed, I found the workaround on MrRat's forum
a389951l
Dec 22nd 2004, 11:57 am
Gen. Grant - can you post the link so newer members can find it?
General Grant
Dec 22nd 2004, 5:38 pm
Its in this thread here (http://www.absolutefreebies.com/phpBB2/viewtopic.php?t=7959183&highlight=)
garnetbailey
Dec 24th 2004, 10:50 am
I think this is going to solve my problems, but as a newbie at this, I am unsure of how to implement the code. Sorry for the dumb question, but should it go in a separate file in my root directory and use the php include code on my cgi generated web pages? Or does this code get included with the html of my page?
Thanks for the help!
Garnet
DFL
Dec 26th 2004, 1:08 am
Can someone please help, i have done eveything said in this thread, but i dont know how should i display the ads. What code to i have to add to my templates or cgi file?
clickdoc
Jan 7th 2005, 1:18 am
I looked into the wrapper that Shawn suggested because I didn't want people to have to rely on me whenever Shawn makes improvements and adds features. I still think sticking with his php version is the best route. With that said I could not find a way to call a php program from inside a perl script if the server is running a DSO version of php (built into the web server instead of being a binary application). Therefore here is the latest Perl version including all of Shawn's new requirements.
For people who are not running it as an include and are executing it from within a script with ` ` it should now not print the Content-type: declaration.
#!/usr/bin/perl
# $Id: ad_network.cgi,v 1.9 2005/01/07 08:03:09 cvs Exp $
# $Revision: 1.9 $
use strict;
use LWP::UserAgent;
use Fcntl qw(:flock);
use Socket;
srand;
use vars qw/$ad_file $ad_url $VERSION $ad_network $ad_count $ad_separator @ad_params/;
# use ini subroutine with global vars, usefull for mod_perl
sub init {
### User Variables
# edit ad_file to set the path to the ad_network_ads.txt file
# default is to look in the same directory as where the script runs
# just create a blank file in that directory that is writable by the
# web server process and this script will set it up properly
$ad_file = 'ad_network_ads.txt';
# change type= here to the type of link you wish to serve
$ad_url = qq[http://ads.digitalpoint.com/network.php?b=$ENV{'HTTP_HOST'}&type=link];
# set this to the number of ads you want to display on each page
$ad_count = 5;
# set this to the text or html you want to separate the ads ex: <br> |
$ad_separator = '<br>';
if($ENV{QUERY_STRING}) {
my @tags = split(/;/,$ENV{QUERY_STRING});
foreach(@tags) {
my ($name,$value) = split(/=/,$_);
$ad_count = $value if($name eq 'ad_count');
$ad_separator = $value if($name eq 'ad_separator');
}
}
$ad_count = 5 if($ad_count > 5);
### end User Variables
$VERSION = sprintf "%d.%d", qq$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
$ad_network = '';
}
init();
# only print content type if called as cgi (include) for other perl scripts
# may be called with `/path/to/file/ad_network.cgi` to get output without
# Content-type: declaration
print qq{Content-type: text/html\n\n} if($ENV{GATEWAY_INTERFACE} =~ /CGI/);
if(-w $ad_file) {
if(open(FILE,"$ad_file")) {
# if we can't get a lock that means a process is writing, just serve an ad from the existing file
# and don't check the timestamp, this prevents a slow response from the DP server from hanging
# every web process waiting to read an ad, there is slim a chance we could end up with a partial ad
if(!flock(FILE, LOCK_SH|LOCK_NB)) {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
@ad_params = split(/\|/,$ads[0]);
for(my $i=0;$i<$ad_count;$i++) {
$ad_network .= $ads[int(rand(@ads-1))+1]; # dont get control info found in position 0
$ad_network .= $ad_separator if($ad_count > 1);
}
} else {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
@ad_params = split(/\|/,$ads[0]);
# if we haven't hit our cache limit or
# the timestamp is less than timeout
if(@ads < $ad_params[3] + 1 && $ad_params[0] + $ad_params[5] < time() || $ad_params[0] + $ad_params[4] < time()) {
if(open(FILE,">$ad_file")) {
flock(FILE, LOCK_EX); # get an exclusive lock so we can write the ad_network_ads file
my $ua = new LWP::UserAgent;
$ua->agent("Ad-Network-Perl/$VERSION)");
my $req = HTTP::Request->new(GET => $ad_url);
my $res = $ua->request($req);
if($res->is_success) {
my $content = '';
$content .= $res->content;
my ($params,$newad) = split(/<ad_param>/,$content);
my @new_params = split(/\|/,$params);
for(my $i=0;$i<@new_params;$i++) {
$ad_params[$i+2] = $new_params[$i];
}
push(@ads,'') if(@ads == 0);
push(@ads,$newad);
}
if(@ads > $ad_params[3]) {
shift @ads;
shift @ads;
}
my $packedip = gethostbyname('validate.digitalpoint.com');
my $ipadr = inet_ntoa($packedip);
@ad_params[0] = time();
@ad_params[1] = $ipadr;
$ads[0] = join('|',@ad_params);
print FILE join('<ad_break>',@ads);
close(FILE);
}
for(my $i=0;$i<$ad_count;$i++) {
if(@ads > 1) { # dont get control information
$ad_network .= pop(@ads);
$ad_network .= $ad_separator if($ad_count > 1);
}
}
} else {
for(my $i=0;$i<$ad_count;$i++) {
$ad_network .= $ads[int(rand(@ads-1))+1]; # dont get control information
$ad_network .= $ad_separator if($ad_count > 1);
}
} # end else on check for 100 ads or timestamp older than one hour
} # end else on if flock
} else {
warn qq { could not open file $ad_file because $! };
} # end else on opening ad file
} else {
$ad_network = 'You must set the "ad_network_ads.txt" file to be writable.';
} # end else on writable ad file
print $ad_network . qq{\n};
chachi
Jan 7th 2005, 7:29 am
Clickdoc, you are my hero
a389951l
Jan 7th 2005, 8:52 am
Yes clickdoc you are awesome. I tested it on two of my perl sites and it works great. Just needed to flush out my ad_network_ads.txt file since it had the "ad_network.php needs updating..."
clickdoc
Jan 7th 2005, 9:17 am
Yes you should empty out your ad_network_ads.txt file to use this new script.
Also after a little testing I noticed some servers still pass the $ENV variable to the script when its run from inside another script with ` `. If you are not using it as an include and just running it in another script you may need to remove the Content-type: line. Here is how you can call it from inside another script:
my $output = `./ad_network.cgi`;
$output =~ s/Content-type: text\/html//;
print $output;
You may have to put the full path to the ad_network.cgi file.
Nick0r
Jan 7th 2005, 10:09 am
Ads not found on http://shopping.2and2.net/
Account not not yet activated. | Site failed automatic validation as of 2005-01-07 (make sure URL and site type is correct - if everything is correct, make a request in the forum for assistance). Validate/Calculate Weight Now
Clearly the ads are on there and working.
Any ideas?
a389951l
Jan 7th 2005, 11:04 am
I think there might be a problem with the .cgi script. I can see the ads showing for sure but it is failing validation for both of my sites that are using the script. :(
Nick0r
Jan 7th 2005, 11:12 am
Same with me!
chachi
Jan 7th 2005, 1:22 pm
Perl wrapper posted here (http://forums.digitalpoint.com/showthread.php?t=7517)
clickdoc
Jan 7th 2005, 1:23 pm
Everyone should move to the wrapper. You will need at least one server capable of running shawn's php script. Details here:
http://forums.digitalpoint.com/showthread.php?p=79204#post79204 (http://forums.digitalpoint.com/showthread.php?p=79204)
clickdoc
Jan 7th 2005, 1:57 pm
I see why the perl is not validating now. Everyone should move to the wrapper method however if someone really needs a working perl version let me know and I'll post the changes.
c3kids
Jan 7th 2005, 3:25 pm
Hi Clickdoc! I could use some help!!!!!!
I've spent 2 hours trying to get the Perl/PHP wrapper working and I'm getting nowhere. The reason I used the Perl version to begin with is that I don't really "get" how to use PHP.
So I propose a compromise. I believe that going to the Perl wrapper is a good, long-term move so that the main PHP code can be updated easily over time. But who knows when I'll figure out my coding problems with the PHP?
And, right now, one of my sites is unvalidated, and the second site shows only "update your ad_network.php file" ads!
Could you post corrected Perl code for now, and then I'll have some time to move over to the Perl/PHP wrapper? I'm thinking that this extra time to transition could be helpful to others as well.
Thanks so much -- your scripts have worked great for me up 'til now!
chachi
Jan 7th 2005, 3:30 pm
C3, you should use the PHP version if you can...the perl version was built for people that need a cgi version of the script. The wrapper is now going to be the only thing that gets updated and the "old" perl script is no longer going to be supported.
Does your server support PHP? If so, have you tried to implement the php version? If you are familiar with SSI, you can have it up in a few minutes.
c3kids
Jan 7th 2005, 3:44 pm
Chachi -
I think you missed the point of my message. Yes, my server supports PHP, but I cannot get the script to work after 2 hours of work. By the way, I couldn't get it to work 3 months ago either even though I have other SSI content working quite well on my site. Obviously, there's some small detail that I need to figure out -- htaccess, directory level of the script vs. the content pages... something!
I want to transition to the PHP version with the Perl wrapper since that seems smart long-term, but it seems unfair that I was given virtually no time to transition. My one site is failing validation, and I'm sure the second site will fail soon since it is now only showing Shawn's update ads as of right now.
Clickdoc offered to post another Perl update to correct the current validation problems experienced with the Perl script (post #42 above), and I was hoping that he could do that as an interim fix for me (and possibly others).
chachi
Jan 7th 2005, 5:12 pm
C3, I understood your post. ClickDoc is not going to work on 2 versions of the perl...trust me ;) If your site supports SSI and PHP, then you should have no problem getting things rolling....otherwise, just use the wrapper....it will do the same thing for you. I would be happy to help you get things working, and I am sure there are others here that are willing to do the same.
The interim fix, is to use the wrapper. If you already support php and SSI then we should be good to go. I will help you...relax, take a deep breath and let's get to it.
clickdoc
Jan 8th 2005, 1:56 pm
As chachi stated I'm not gonna work on two versions so this will be the last perl version I release publicly. This version will validate with the current setup. To support further updates move to the wrapper method. There is one big problem with the wrapper method on active sites, it basically doubles all your traffic because unless you use an offsite coop.php file it does a hit on that file for every page load. Anyway here is the final perl version that will validate as of 2005.01.08.
#!/usr/bin/perl
# $Id: ad_network.cgi,v 1.11 2005/01/08 20:52:19 cvs Exp $
# $Revision: 1.11 $
use strict;
use LWP::UserAgent;
use Fcntl qw(:flock);
use Socket;
srand;
use vars qw/$ad_file $ad_url $VERSION $ad_network $ad_count $ad_separator @ad_params/;
# use ini subroutine with global vars, usefull for mod_perl
sub init {
### User Variables
# edit ad_file to set the path to the ad_network_ads.txt file
# default is to look in the same directory as where the script runs
# just create a blank file in that directory that is writable by the
# web server process and this script will set it up properly
$ad_file = 'ad_network_ads.txt';
# change type= here to the type of link you wish to serve
$ad_url = qq[http://ads.digitalpoint.com/network.php?b=$ENV{'HTTP_HOST'}&type=link];
# set this to the number of ads you want to display on each page
$ad_count = 5;
# set this to the text or html you want to separate the ads ex: <br> |
$ad_separator = '<br>';
if($ENV{QUERY_STRING}) {
my @tags = split(/;/,$ENV{QUERY_STRING});
foreach(@tags) {
my ($name,$value) = split(/=/,$_);
$ad_count = $value if($name eq 'ad_count');
$ad_separator = $value if($name eq 'ad_separator');
}
}
$ad_count = 5 if($ad_count > 5);
### end User Variables
$VERSION = sprintf "%d.%d", qq$Revision: 1.11 $ =~ /(\d+)\.(\d+)/;
$ad_network = '';
}
init();
# only print content type if called as cgi (include) for other perl scripts
# may be called with `/path/to/file/ad_network.cgi` to get output without
# Content-type: declaration
print qq{Content-type: text/html\n\n} if($ENV{GATEWAY_INTERFACE} =~ /CGI/);
if(-w $ad_file) {
if(open(FILE,"$ad_file")) {
# if we can't get a lock that means a process is writing, just serve an ad from the existing file
# and don't check the timestamp, this prevents a slow response from the DP server from hanging
# every web process waiting to read an ad, there is slim a chance we could end up with a partial ad
if(!flock(FILE, LOCK_SH|LOCK_NB)) {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
@ad_params = split(/\|/,$ads[0]);
for(my $i=0;$i<$ad_count;$i++) {
my $randad .= $ads[int(rand(@ads-1))+1]; # dont get control info found in position 0
if($ENV{REMOTE_ADDR} eq $ad_params[1]) {
$randad =~ s/" \/>/" class="$ad_params[2]" \/>/;
} else {
$randad =~ s/" \/>/">/;
}
$ad_network .= $randad;
$ad_network .= $ad_separator if($ad_count > 1);
}
} else {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
@ad_params = split(/\|/,$ads[0]);
# if we haven't hit our cache limit or
# the timestamp is less than timeout
if(@ads < $ad_params[3] + 1 && $ad_params[0] + $ad_params[5] < time() || $ad_params[0] + $ad_params[4] < time()) {
if(open(FILE,">$ad_file")) {
flock(FILE, LOCK_EX); # get an exclusive lock so we can write the ad_network_ads file
my $ua = new LWP::UserAgent;
$ua->agent("Ad-Network-Perl/$VERSION)");
my $req = HTTP::Request->new(GET => $ad_url);
my $res = $ua->request($req);
if($res->is_success) {
my $content = '';
$content .= $res->content;
my ($params,$newad) = split(/<ad_param>/,$content);
my @new_params = split(/\|/,$params);
for(my $i=0;$i<@new_params;$i++) {
$ad_params[$i+2] = $new_params[$i];
}
push(@ads,'') if(@ads == 0);
push(@ads,$newad);
}
if(@ads > $ad_params[3]) {
shift @ads;
shift @ads;
}
my $packedip = gethostbyname('validate.digitalpoint.com');
my $ipadr = inet_ntoa($packedip);
@ad_params[0] = time();
@ad_params[1] = $ipadr;
$ads[0] = join('|',@ad_params);
print FILE join('<ad_break>',@ads);
close(FILE);
}
for(my $i=0;$i<$ad_count;$i++) {
if(@ads > 1) { # dont get control information
my $randad .= pop(@ads);
if($ENV{REMOTE_ADDR} eq $ad_params[1]) {
$randad =~ s/" \/>/" class="$ad_params[2]" \/>/;
} else {
$randad =~ s/" \/>/">/;
}
$ad_network .= $randad;
$ad_network .= $ad_separator if($ad_count > 1);
}
}
} else {
for(my $i=0;$i<$ad_count;$i++) {
my $randad .= $ads[int(rand(@ads-1))+1]; # dont get control info found in position 0
if($ENV{REMOTE_ADDR} eq $ad_params[1]) {
$randad =~ s/" \/>/" class="$ad_params[2]" \/>/;
} else {
$randad =~ s/" \/>/">/;
}
$ad_network .= $randad;
$ad_network .= $ad_separator if($ad_count > 1);
}
} # end else on check for 100 ads or timestamp older than one hour
} # end else on if flock
} else {
warn qq { could not open file $ad_file because $! };
} # end else on opening ad file
} else {
$ad_network = 'You must set the "ad_network_ads.txt" file to be writable.';
} # end else on writable ad file
print $ad_network . qq{\n};
a389951l
Jan 9th 2005, 11:25 am
I am having a validation issue with the wrapper today. I had to go back to the code above to get validated again. Not sure if anyone else is facing the same issue as me.
c3kids
Jan 9th 2005, 12:27 pm
Hi Chachi -
Thanks to you and Clickdoc for posting the one last pure Perl version of the script. This lets me at least get all of the "update script" notices off of my sites and get re-validated!
And thanks for the offer of help in getting the PHP version running. I am TRULY frustrated at this point! Right now, I'm focusing on getting the coop.php page working -- then the Perl wrapper should be able to pick it up with (hopefully) no problem.
The coop.php page currently says "The ads_network_ads.txt must be writable." I understand this error message, and I've checked the ads_network_ads.txt a dozen times -- it's definitely set at chmod 777. My .htaccess currently includes the following items (several for other scripts I'm running):
Options ExecCGI Includes
AddType text/html .html
AddHandler server-parsed .html
AddHandler application/x-httpd-php .php .htm .html
Any idea what might be making the .txt file appear unwritable?
Thanks and I'll keep plugging along until I get it!
digitalpoint
Jan 9th 2005, 12:30 pm
Try hard-coding the full path to the ad_network_ads.txt file. It might be looking in the wrong folder.
c3kids
Jan 10th 2005, 11:47 am
Wow! That was easy once you pointed it out to me! Although I've successfully installed a number of scripts over time, sometimes as a non-programmer you can't see the obvious things.
Thanks!
P.S. I wasn't just frustrated over this last step -- I did successfully correct about 3 other problems before this last one. Thus, a little temper tantrum last Friday. ;)
clickdoc
Jan 17th 2005, 6:31 pm
Once again I strongly suggest everyone move to the perl -> php wrapper method posted here http://forums.digitalpoint.com/showthread.php?t=7517&highlight=coop.php
If you have not moved yet and are using the perl version it is important that you update it to this version to abide by the hit timeouts on the DP server. If you have a busy site and are using version 1.11 you will get banned. I am happy to provide advice to anyone who wants to move to the wrapper.
Be sure to set the ad_file variable to the full path of your ad_network_ads.txt file and set that file writable by the web process. You may also want to change the ad_separator if you are using multiple ads and ad_count if you are not.
New Code:
#!/usr/bin/perl
# $Id: ad_network.cgi,v 1.12 2005/01/18 01:26:19 cvs Exp $
# $Revision: 1.12 $
use strict;
use LWP::UserAgent;
use Fcntl qw(:flock);
use Socket;
srand;
use vars qw/$ad_file $ad_url $VERSION $ad_network $ad_count $ad_separator @ad_params/;
# use ini subroutine with global vars, usefull for mod_perl
sub init {
### User Variables
# edit ad_file to set the path to the ad_network_ads.txt file
# default is to look in the same directory as where the script runs
# just create a blank file in that directory that is writable by the
# web server process and this script will set it up properly
$ad_file = 'ad_network_ads.txt';
# change type= here to the type of link you wish to serve
$ad_url = qq[http://ads.digitalpoint.com/network.php?b=$ENV{'HTTP_HOST'}&type=link];
# set this to the number of ads you want to display on each page
$ad_count = 5;
# set this to the text or html you want to separate the ads ex: <br> |
$ad_separator = '<br>';
if($ENV{QUERY_STRING}) {
my @tags = split(/;/,$ENV{QUERY_STRING});
foreach(@tags) {
my ($name,$value) = split(/=/,$_);
$ad_count = $value if($name eq 'ad_count');
$ad_separator = $value if($name eq 'ad_separator');
}
}
$ad_count = 5 if($ad_count > 5);
### end User Variables
$VERSION = sprintf "%d.%d", qq$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
$ad_network = '';
}
init();
# only print content type if called as cgi (include) for other perl scripts
# may be called with `/path/to/file/ad_network.cgi` to get output without
# Content-type: declaration
print qq{Content-type: text/html\n\n} if($ENV{GATEWAY_INTERFACE} =~ /CGI/);
if(-w $ad_file) {
if(open(FILE,"$ad_file")) {
# if we can't get a lock that means a process is writing, just serve an ad from the existing file
# and don't check the timestamp, this prevents a slow response from the DP server from hanging
# every web process waiting to read an ad, there is slim a chance we could end up with a partial ad
if(!flock(FILE, LOCK_SH|LOCK_NB)) {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
@ad_params = split(/\|/,$ads[0]);
for(my $i=0;$i<$ad_count;$i++) {
my $randad .= $ads[int(rand(@ads-1))+1]; # dont get control info found in position 0
if($ENV{REMOTE_ADDR} eq $ad_params[1]) {
$randad =~ s/" \/>/" class="$ad_params[2]" \/>/;
} else {
$randad =~ s/" \/>/">/;
}
$ad_network .= $randad;
$ad_network .= $ad_separator if($ad_count > 1);
}
} else {
my $adlist = join('',<FILE>);
close(FILE);
my @ads = split(/<ad_break>/,$adlist);
@ad_params = split(/\|/,$ads[0]);
# if we haven't hit our cache limit or
# the timestamp is less than timeout
if((@ads < $ad_params[3] && $ad_params[0] + $ad_params[5] < time()) || $ad_params[0] + $ad_params[4] < time()) {
if(open(FILE,">$ad_file")) {
flock(FILE, LOCK_EX); # get an exclusive lock so we can write the ad_network_ads file
my $ua = new LWP::UserAgent;
$ua->agent("Ad-Network-Perl/$VERSION)");
my $req = HTTP::Request->new(GET => $ad_url);
my $res = $ua->request($req);
if($res->is_success) {
my $content = '';
$content .= $res->content;
my ($params,$newad) = split(/<ad_param>/,$content);
my @new_params = split(/\|/,$params);
for(my $i=0;$i<@new_params;$i++) {
$ad_params[$i+2] = $new_params[$i];
}
push(@ads,'') if(@ads == 0);
push(@ads,$newad);
} else {
warn qq { Not sucessfull getting ad network ads from digitalpoint };
}
if(@ads > $ad_params[3]) {
shift @ads;
}
my $packedip = gethostbyname('validate.digitalpoint.com');
my $ipadr = inet_ntoa($packedip);
@ad_params[0] = time();
@ad_params[1] = $ipadr;
$ads[0] = join('|',@ad_params);
print FILE join('<ad_break>',@ads);
close(FILE);
}
for(my $i=0;$i<$ad_count;$i++) {
if(@ads > 1) { # dont get control information
my $randad .= pop(@ads);
if($ENV{REMOTE_ADDR} eq $ad_params[1]) {
$randad =~ s/" \/>/" class="$ad_params[2]" \/>/;
} else {
$randad =~ s/" \/>/">/;
}
$ad_network .= $randad;
$ad_network .= $ad_separator if($ad_count > 1);
}
}
} else {
for(my $i=0;$i<$ad_count;$i++) {
my $randad .= $ads[int(rand(@ads-1))+1]; # dont get control info found in position 0
if($ENV{REMOTE_ADDR} eq $ad_params[1]) {
$randad =~ s/" \/>/" class="$ad_params[2]" \/>/;
} else {
$randad =~ s/" \/>/">/;
}
$ad_network .= $randad;
$ad_network .= $ad_separator if($ad_count > 1);
}
} # end else on check for 100 ads or timestamp older than one hour
} # end else on if flock
} else {
warn qq { could not open file $ad_file because $! };
} # end else on opening ad file
} else {
$ad_network = 'You must set the "ad_network_ads.txt" file to be writable.';
} # end else on writable ad file
print $ad_network . qq{\n};
webvivre
Apr 14th 2005, 1:42 pm
Still need help - trying to get to work with Amazon Product feed
Tried putting it in the cgi file - where exactly should the code be inserted?
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.