perl search mysql - help

With being total new at this, for several month’s now to try and figure out how to open and search a database for the $id obtained from a web page and get the users email address and phone address associated with that id.

I have the script working now with out the search but it requires me having to hand code the perl script for each user. Would like to stream line the process to use a database.

In the database needing to search there is 3 tables, I need to search for the ticket_id and the username
that is their email address. The other field has their cell. But could be blank.

database tables

username: [email protected]
ticket_id: 1-YS25UHRN3N9D
phone: [email protected]

I was told putting the database login information in the cgi was bad, but I don’t know how to work it into
another file and access the file from perl or other way yet.

#!/usr/bin/perl -W

use CGI;
use CGI param;
use POSIX qw/strftime/;
use mysql;


$host = “localhost”;
$database = “users”;
$tablename = “tickets”;
$user = “username”;
$pw = “password”;

Obtain $id & $ip from web page.

$id = param(“id”);
$ip = param(“ip”);


1 = email

2 = phone

3 = both

$message = param(“send_me”);

Connect to database

$connect = Mysql->connect($host, $database, $user, $pw);




$myquery = “SELECT user_id FROM user”;


$execute = $connect->query($myquery);


$myquery = “SELECT ticket_id,username,phone FROM $tablename”;

#check if we have a matching $id == $ticket_id
if ($id =~ /$ticket_id/) {
#if we have a match open the file to write.
$file = “/var/log/$ticket_id_iptrace.txt”;
open(FILE, “>$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

  # Run the IP2Location script.
  system("./ $ENV{REMOTE_ADDR} json city>>/var/log/$ticket_id_iptrace.txt");

  # Lets send an email and or text with all the information.
  if($message =~ /1/) {
         system("mail -s 'Web Page visited' $username \< /var/log/$ticket_id_iptrace.txt");
  if($message =~ /2/) {
          system("mail -s 'Web Page visited' $phone \< /var/log/$ticket_id_iptrace.txt");
  if($message =~/3/) {
          system("mail -s 'Web Page visited' $username \< /var/log/$ticket_id_iptrace.txt");
          system("mail -s 'Web Page visited' $phone \< /var/log/$ticket_id_iptrace.txt");



“To Do” send a page or popup if the id is not valid.

$file = “/var/log/invalid_id_iptrace.txt”;
open(FILE, “>$file”);
print FILE strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time) );
print FILE " – IP Address: $ip\n\t Accessed unknown $id:\n------\n";
system(“mail -s ‘Page visited Unknown’ [email protected] < /var/log/invalid_id_iptrace.txt”);
#end of script


Sponsor our Newsletter | Privacy Policy | Terms of Service