Solved

forcing a 'hit'

Posted on 2000-02-22
13
229 Views
Last Modified: 2010-03-05
I need a perl script that will count a hit to another webpage that isn't on my server.  Basically, I'm looking for a cheater's way to generate hits.  I assure you that this is not a money making scam although it may sound like it.

When someone comes to my site, I want a perl script to be executed in a hidden frame.  This I can do.

I don't know how to do this, though:

If someone where to legitimately go to this other webpage through my site (click on their link), it would take them to the page: www.site2.com/index.cgi?id=number. Once on that page, the hit isn't counted unless the user follows a link on that page which goes to: www.site2.com/differentpage.html.  How can I use perl to go to differentpage.html and have site2.com think that the user clicked on the appropriate links?

I have the following script. However, it doesn't work.  For some reason site2.com knows better.  I think it's because the perl script is being executed from my server and site2.com is logging it as coming from my IP number instead of each user's IP number.  I know that anything is possible in Perl, so any ideas?  Thanks!

---Script that I have---

#!/usr/bin/perl
 
use HTTP::Request::Common qw(GET);
use LWP::UserAgent;
 
$ua = new LWP::UserAgent;
 
$ua->agent('Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; DigExt)');
 
$req = GET 'http://www.site2.com/differentpage.html';
$req->push_header(REFERER =>
'http://www.site2.com/index.cgi?id=number');

exit;


0
Comment
Question by:GorGor1
  • 7
  • 6
13 Comments
 
LVL 3

Accepted Solution

by:
guadalupe earned 75 total points
Comment Utility
#!/usr/local/bin/perl


#Parse form variables
if ($ENV{'REQUEST_METHOD'} eq "POST")
      {
          read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
    }
    elsif ($ENV{'REQUEST_METHOD'} eq "GET" )
      {
          $temp = $ENV{'QUERY_STRING'};
    }

@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
($key,$content)=split (/=/,$item,2);
$content=~tr/+/ /;
$content=~ s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}

if (($fields{url} ne "") && ($fields{url} =~ /http:\/\//))
{
print "Location: $fields{url}\n\n";
exit;
}



      


#Default if not found
open (LOG, ">>/virtual/ar/cgi-data/redirect.log");

print LOG "Error de syntax en $ENV{'HTTP_REFERER'}\n";

close LOG;

#default if all else fails
print "Location: http://www.xxx.com\n\n";

call like this

<a href="/cgi-bin/redirect.pl?
url=http://www.xxx.com">xxx</a>
0
 
LVL 1

Author Comment

by:GorGor1
Comment Utility
Could you explain to me how to use this?  Where do I put in my particular variables?  Could you give me a little explanation on how it works?  I'm a perl dummy.  Thanks!

Tim
0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
Ok the script goes in your cgi bin and make sure to give it the correct permissions:

chmod +x redirect.pl

then if you have this link:

<a href="www.altavista.com">Search</a>

and you want to capture the click change the link to:


<a href="/cgi-bin/redirect.pl?url=http://www.altavista.com">Search</a>


The script parses the query string variables where it finds the variable url which itdicates the url to which it should redirect the browser.  It uses the print "Location:" method of printing the MIME type header whcih redirects the browser...  This matains the final click to the external link within you web server log by "piping" it through the cgi... you get that last hit as a call to redirect.pl...
0
 
LVL 1

Author Comment

by:GorGor1
Comment Utility
Ok, I can kinda follow the theory behind the script.  My final questions are:

If the page that contains the link needs to be followed is "www.site.com/index.cgi?1234" and the page to redirect to (to have the hit counted) is "www.site.com/page2.html", what do I substitute in the following line?

if (($fields{url} ne "") && ($fields{url} =~ /http:\/\//))

or do I leave the line "as is"?

Also, are these next lines required for the script to work?  Will they be automatically executed?  If I need them, with my example pages given above, what would I substitute in these lines?
-------
#Default if not found
open (LOG, ">>/virtual/ar/cgi-data/redirect.log");

print LOG "Error de syntax en $ENV{'HTTP_REFERER'}\n";

close LOG;

#default if all else fails
print "Location: http://www.xxx.com\n\n";

call like this

<a href="/cgi-bin/redirect.pl?
url=http://www.xxx.com">xxx</a>

--------

Thanks again

0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
Forgive me as I'm short on time:

First quetion don't change anything the link is exactly as I explained The link you gave:

www.site.com/index.cgi?1234

is to get to the page... were talking about leaving the page, two completely different things...


This is just a error log which you can include as you which the only things to edit is the path to the log.  Don't worry if the file doesn't exist, as long as the path you give exists. perl will create the log file the first time it is called and append to it thereafter.

#Default if not found
open (LOG, ">>/COMPLETE_PATH/redirect.log");

print LOG "Error de syntax en $ENV{'HTTP_REFERER'}\n";

close LOG;


This code is a good idea.  That way if the script receives incomplete or bad info it won't just die but send the user to a default URL

#default if all else fails
print "Location: http://www.xxx.com\n\n";
0
 
LVL 1

Author Comment

by:GorGor1
Comment Utility
Adjusted points to 75
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:GorGor1
Comment Utility
I tried out the script and it seems to only be taking me to "http://www.site.com/index.cgi?1234" since I used the command "/cgi-bin/redirect.pl?url=http://www.site.com/index.cgi?1234".  I could have done this with the simple <body onload="window.location='http://www.site.com/index.cgi?1234';"> in javascript.

This is what I need to do:  
Once at "http://www.site.com/index.cgi?1234" (which is NOT on my server), I need the user to be redirected to "http://www.site.com/anotherpage.html" (in the same window) with site.com thinking that the user clicked on the appropriate link on the first page.  Is there a way to do this?  The redirect.pl script you gave me SEEMS to not be doing this.  How do I use perl to "follow links"?  Thanks again!

Tim
0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
Ok yes there is a way to do this but what you want was not clear at first... This will take me a little more time... question do you know the second like you want to actually end up at or do you have to some how parse is out from
"http://www.site.com/index.cgi?1234"
also do you have the module LWP installed or can you install moduals?

 
0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
#!/usr/local/bin/perl


use LWP::Simple;

#set second url WITH http://
$url = "http://www.site.com/anotherpage.html";

#Get the first page, causing a hit
$index_list = get("www.site.com/index.cgi?1234");

#Go to second page
print "Location: $url\n\n";

exit(0);
0
 
LVL 1

Author Comment

by:GorGor1
Comment Utility
Yes, I have LWP installed.  I'm not too sure what you are asking in your second question, but maybe this will explain it:

When you go to "http://www.site.com/index.cgi?1234", the page will have a LINK on it that goes to "www.site.com/nextpage.html".  the index.cgi is just using the variable "1234" to know that I am the one referring users to the index.cgi page of the site.  The link on "http://www.site.com/index.cgi?1234" needs to be followed (clicked) in order to count the hit to the site.

Will the script you gave me last completely replace the previous script?  Is it a fully functional script?

What does the line 'print "Location: $url\n\n";' do? Does it open a page in the same browser window?


P.S.  Thank you for all your help so far  :o)
0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
Will the script you gave me last completely replace the previous script?  

YES

Is it a fully functional script?

YES

What does the line 'print "Location: $url\n\n";' do? Does it open a page in the same browser window?

This prints the MIME type header which redirects the browser to $url.  It does it in the same window.  If you want to do it in a new window then make the target of the link to this cgi a target="_blank" and presto its a new window.
0
 
LVL 1

Author Comment

by:GorGor1
Comment Utility
Last and final quick question(s) and you get the points  :o)

the line '$index_list = get("www.site.com/index.cgi?1234");' will cause site.com to think that the user is being referred to "www.site.com/nextpage.html" by the page "www.site.com/index.cgi?1234"?

And also, to call the script, all I need is '<a href="/cgi-bin/redirect.pl">click</a>'?  I don't need to pass a variable to redirect.pl, right?  Thank you for all the help!  Points will be yours after I get your response!  :o)

Tim



0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
the line '$index_list = get("www.site.com/index.cgi?1234");' will cause site.com to think that the user is being referred to "www.site.com/nextpage.html" by the page "www.site.com/index.cgi?1234"?

No this will make the site think someone is enetering:
www.site.com/index.cgi?1234

Then we capture the returned page in $index_list (stupid name for this context change to $html_page) After we capture the returned page we still have control (that is to say we havn't "returned" to the browser yet...)
The we send the location of were we really want to go with:

#Go to second page
print "Location: $url\n\n";




And also, to call the script, all I need is '<a href="/cgi-bin/redirect.pl">click</a>'?  I don't need to pass a variable to redirect.pl, right?

RIGHT!

Thank you for all the help!  Points will be yours after I get your response!  :o)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now