PHP to PERL

Discussion in 'Co-op Advertising Network' started by rudi, Sep 4, 2004.

  1. clickdoc

    clickdoc Guest

    Messages:
    39
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #41
    clickdoc, Jan 7, 2005 IP
  2. clickdoc

    clickdoc Guest

    Messages:
    39
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #42
    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.
     
    clickdoc, Jan 7, 2005 IP
  3. c3kids

    c3kids Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #43
    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!
     
    c3kids, Jan 7, 2005 IP
  4. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #44
    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.
     
    chachi, Jan 7, 2005 IP
  5. c3kids

    c3kids Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #45
    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).
     
    c3kids, Jan 7, 2005 IP
  6. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #46
    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.
     
    chachi, Jan 7, 2005 IP
  7. clickdoc

    clickdoc Guest

    Messages:
    39
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #47
    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};
     
    clickdoc, Jan 8, 2005 IP
  8. a389951l

    a389951l Must Create More Content

    Messages:
    1,885
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    140
    #48
    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.
     
    a389951l, Jan 9, 2005 IP
  9. c3kids

    c3kids Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #49
    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!
     
    c3kids, Jan 9, 2005 IP
  10. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #50
    Try hard-coding the full path to the ad_network_ads.txt file. It might be looking in the wrong folder.
     
    digitalpoint, Jan 9, 2005 IP
  11. c3kids

    c3kids Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #51
    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. ;)
     
    c3kids, Jan 10, 2005 IP
  12. clickdoc

    clickdoc Guest

    Messages:
    39
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #52
    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};
     
    clickdoc, Jan 17, 2005 IP
  13. webvivre

    webvivre Peon

    Messages:
    249
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #53
    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?
     
    webvivre, Apr 14, 2005 IP