
brosskgm
User
Feb 26, 2013, 3:18 PM
Post #1 of 76
(52549 views)
|
Perl MySQL help with code.
|
Can't Post
|
|
I have been working on this for some time and with not really knowing Perl it’s been extremely difficult. It does seem to be connecting to MySQL because there has not been any of those errors. I can’t seem to search the database for the value needed. A short PHP header sends the information the perl script processes. Is there anyone that can help fix this perl code. I can even send a little by paypal if it’s not to much. The script is working now as long as I don’t try to use the mysql but I would have to hand code each new setup if I want to add more people to it. The php code could be on any server, the perl will only be on one machine is why I don't put it all in the php page. I only have 5 people using the setup, so it’s not all that critical, but I have been getting asked by others that would like to use it for their web site. Before I do that I would like it to stream line it to use a database. Database already exists and can already have data entered by a form page. #### PHP code that is already in use.
<?php $ip = $_SERVER['REMOTE_ADDR']; // Client IP address if(isset($_POST['referer'])) { $l_sReferer = trim($_POST['referer']); } elseif (isset($_SERVER['HTTP_REFERER'])) { $l_sReferer = base64_encode($_SERVER['HTTP_REFERER']); } else { $l_sReferer = ""; } $id = '1-YS25UHRN3N9D'; // Client id $pg = '1'; // 1=email, 2=text, 3=email/text $url = "http://192.168.4.4/cgi-bin/log_it.cgi"; // back-end url echo '[removed][removed]'; ?> Echo is removed by forum but that part works. I have tried many different samples of code, tried changing the declaration and param but still can not get it to search the database and retrieve the information on the user. The database has
username: user-email-address subscr_id: 1-YS25UHRN3N9D phone: 123456789/domain-to-send-text ########## Perl code…
#!/usr/bin/perl -w use warnings; use strict; use diagnostics; use CGI qw/param/; use POSIX qw/strftime/; use DBI; print "ContentType: text/html\n\n"; my $host = "remote_host"; my $database = "dbase"; my $tablename = "tname"; my $user = "user"; my $pw = "pw"; # Obtain items from web page. my $id = param("id"); my $ip = param("ip"); my $pg = param("pg"); # # This seems to have problem # From MySQL database my $username = param("username"); my $subscr_id = param("subscr_id"); my $phone = param("phone"); ################## # to be re-worked to make file handling better # my $file = "_iptrace.txt"; my $filea = "/tmp/"; my $fileb = $subscr_id; my $filec = ".txt"; my $filed = "/tmp/invalid_id_iptrace.txt"; my $phone; # message # 1 = email # 2 = phone text message # 3 = both my $message = param("pg"); # Is not working. Needs to be changed/removed etc... # # my $dbh = DBI->connect('DBI:mysql:$database, $host','$user','$pw') # or die "Connection Error: $DBI::errstr\n"; # # my $sql = "SELECT subscr_id,username FROM $tabelname WHERE subscr_id = 1"; # my $sth = $dbh->prepare($sql); # $sth->execute # or die "SQL Error: $DBI::errstr\n"; # my @row = $sth->fetchrow_array(); # print "subscr_id\n"; # # $sth = $dbh->finish(); # # if ($id eq 'subscr_id') { #if we have a match open the file to write. open(FILE, '>'."/tmp/$subscr_id\$file"); # lets put the time in the file. print FILE strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time) ); # put the IP address and web page visited. print FILE " -- IP Address: $ENV{REMOTE_ADDR}\n\t Accessed your page: $ENV{HTTP_REFERER}\n------\n"; print FILE " -- Here is the information we found:\n------\n"; #close the file. Finished with the first part close(FILE); # Run the IP2Location script. system("./iptrace.sh $ENV{REMOTE_ADDR} json city>>/tmp/$subscr_id$file"); # Lets send an email and or text with all the information. # Send email if($message == 1) { system("mail -s 'Web Page visited' $username \< /tmp/$subscr_id$file"); } # Send text if($message == 2) { system("mail -s 'Web Page visited' $filea$fileb \< /tmp/$subscr_id$file"); } # Send email and text message if($message == 3) { system("mail -s 'Web Page visited' $username \< /tmp/$subscr_id$file"); system("mail -s 'Web Page visited' $filea$fileb \< /tmp/$subscr_id$file"); } print "Content-type: text/javascript\n\n"; print "<!--\n"; print "var x;\n"; print "//-->\n"; # $dbh->disconnect(); exit(); } # # "To Do" send a page or popup if the id is not valid. # for now send me the notice.. # $filed = "/tmp/invalid_id_iptrace.txt"; open (FILED, '>'."$filed") or die $!; print FILED strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time) ); print FILED " -- IP Address: $ip\n\t Access Page: $ENV{HTTP_REFERER} \n From id: $id with :pg as $pg\n------\n"; print FILED " -- Database ID: $subscr_id \n------\n"; close(FILED); system("mail -s 'Page visited Unknown ID' admin\@domain.com \< $filed"); print "Content-type: text/javascript\n\n"; print "<!--\n"; print "var x;\n"; print "//-->\n"; exit(); #end of script
|