2013-02-20 58 views
-1

不理解Perl/MySQL足夠我希望有人在這裏可以伸出援助之手。 該代碼顯然不起作用,但這是我迄今爲止,我仍在學習。PHP/MySQL表搜索Perl

我想打開並搜索從php頁面獲得的$ticket_id的數據庫,並獲取電子郵件地址和/或電子郵件中的一個或兩個電子郵件地址(如果有的話)$ticket_id。至少會有一個。

因爲我似乎每隔一段時間都會問這個問題。 (這不是一個學校項目,我從75歲起就失學了)。

我有腳本工作與信息的數據庫查找,但我必須手工編碼每個設置與ticket_id和電子郵件地址(S)。很高興我只有5個人使用它,但一直被其他人想要使用它。所以我認爲現在應該是一個很好的時間來使用MySQL數據庫的流程,如果我忙於使用它,除了填寫表單頁面來添加他們的信息之外,我不需要太多工作。

在需要搜索的數據庫中有3個表格,我需要搜索ticket_id,然後獲取作爲其電子郵件地址和/或電話號碼的用戶名,但根據通知類型可能是空的他們要。

無論如何這都可以改變。它只有一個記錄用於處理這個新的設置。

username: email(at)address(dot)com 
ticket_id: 1-YS25UHRN3N9D 
phone: 1234567890(at)cellphone(dot)com 

有人告訴我,這可以用JavaScript來實現的window.onload因此更改HTML頁面,PHP就沒有必要了,但不知道的JavaScript的。

回顯腳本可能是錯誤的。 $pg此時沒有被echo所需的腳本收到。

頂級PHP頁面

<?php 
    $ip = $_SERVER['REMOTE_ADDR']; // Client IP address 
    $pg = $_SERVER['HTTP_REFERER']; // What page did they view 
    $ticket_id = '1-YS25UHRN3N9D'; // Ticket id 
    $message = '1'; // 1=email, 2=text, 3=email & text 
    $url = "/cgi-bin/log_it.cgi"; // back-end url 
echo '<script type="text/javascript" src="' . $url . '?id=' . $id . '&amp;ip=' . $ip 
. '?ticket_id=' . $ticket_id . ' . '?message=' . $message . '"></script>'; 
?> 

有人告訴我,把數據庫的登錄信息的CGI很糟糕,但我不知道如何將它工作到一個PHP文件,並從perl的訪問文件或另一種方式。

#!/usr/bin/perl -W 

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

# database 
$host = "localhost"; 
$database = "users"; 
$tablename = "tickets"; 
$user = "username"; 
$pw = "password"; 

# Obtain $id & $ip from web page. 
$id = param("ticket_id"); 
$ip = param("ip"); 

# message 
# 1 = email 
# 2 = phone 
# 3 = both 
$message = param("message"); 

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

# DB 
$connect->selectdb($database); 

# MySQL QUERY 
$myquery = "SELECT ticket_id FROM user"; 

# QUERY 
$myquery = "SELECT ticket_id FROM $tablename"; 

# Need to be worked on. 
# If record not found. 
# 
# "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"; 
# close(FILE); 
# system("mail -s 'Page visited Unknown' admin\@mydomain.com \< /var/log 
# /invalid_id_iptrace.txt"); 
# exit(); 

#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 IP 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>>/var/log/$ticket_id_iptrace.txt"); 

# Lets send an email and or text with all the information. 

# Send email 
if($message =~ /1/) { 
    system("mail -s 'Web Page visited' $username \< /var/log/$ticket_id_iptrace.txt"); 
} 

# Send text 
if($message =~ /2/) { 
    system("mail -s 'Web Page visited' $phone \< /var/log/$ticket_id_iptrace.txt"); 
} 

# Send email and text message 
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"); 
} 
exit(); 
} 

exit(); 
#end of script 

謝謝

更新:二(清理)版本的代碼:

#!/usr/bin/perl 
use warnings; 
use strict; 
use CGI qw/param/; 
use DBI(); 

# database 
my $host = "remotehost"; 
my $database = "database"; 
my $tablename = "table"; 
my $user = "user"; 
my $pw = "pwd"; 

# Obtain $id & $ip from web page. 
my $id = param("id"); 
my $ip = param("ip"); 
my $ticket_id = "ticket_id"; 
my $file = "/tmp/_iptrace.txt"; 
my $fileb = "/tmp/invalid_id_iptrace.txt"; 
my $fh = "FILE"; 
my $phone = "phone"; 
my $username = "username"; 
# message 
# 1 = email 
# 2 = phone 
# 3 = both 
my $message = param("message"); 

# Connect to database 
my $connect = Mysql->connect($host, $database, $user, $pw); 

# DB 
$connect->selectdb($database); 

# QUERY 
my $myquery = "SELECT ticket_id FROM $tablename"; 

# EXECUTE 
my $execute = $connect->query($myquery); 

#check if we have a matching $id == $ticket_id 
if ($id == $payer_id) { 
#if we have a match open the file to write. 
open(my $fh, '>', $file) or die $!; 

# lets put the time in the file. 
print $fh strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time)); 

# put the IP address and web page visited. 
print $fh " -- IP Address: $ENV{REMOTE_ADDR}\n\t Accessed your page: 
    $ENV{HTTP_REFERER}\n------\n"; 
print $fh " -- Here is the information we found:\n------\n"; 

#close the file. Finished with the first part 
close($fh); 

# Run the IP2Location script. 
system("./iptrace.sh $ENV{REMOTE_ADDR} json city>>/tmp/$ticket_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/$ticket 
      _id$file"); 
    } 
    # Send text 
    if($message == 2) { 
    system("mail -s 'Web Page visited' $phone \< /tmp/$ticket 
      _id$file"); 
} 
# Send email and text message 
if($message == 3) { 
    system("mail -s 'Web Page visited' $username \< /tmp/$ticket 
      _id$file"); 
    system("mail -s 'Web Page visited' $phone \< /tmp/$ticket 
      _id$file"); 
    } 
exit(); 
} 
# "To Do" send a page or popup if the id is not valid. 

open($fh, ">$fileb"); 
print $fh strftime("%A %B %d, %Y - %I:%M %p %Z\n", localtime(time)); 
print $fh " -- IP Address: $ip\n\t Accessed unknown $id:\n------\n"; 
close($fh); 
system("mail -s 'Page visited Unknown' admin\@mydomain.com \< 
/tmp/invalid_id_iptrace.txt"); 

exit(); 
#end of script 
+0

爲什麼不在PHP端做所有這些?你爲什麼使用cgi腳本?爲什麼用JS調用它......你應該只使用PHP的curl。 – prodigitalson 2013-02-20 21:10:41

+0

@prodigitalson因爲讓用戶複製並粘貼超過6行的內容很容易出錯。如果我允許其他人使用設置,我不希望他們在他們的網站上擁有所有內容。 PHP是由某人給我的。所以你提出一個我沒有答案的問題。 – 2013-02-20 22:45:54

+0

因此,這一切都運行在一臺服務器上或跨多臺服務器?你最適合的語言是什麼? – prodigitalson 2013-02-20 23:48:28

回答

3

似乎有沒有在你的後一個問題。你說「代碼顯然不起作用」,但你不會告訴我們你看到的意外行爲是什麼樣的。所以這裏有一些關於你的代碼的評論。其中之一可能會解決這個問題。

1 /它應該是'-w'在shebang線上,而不是'-W'。事實上,這些日子裏,你應該刪除'-w',並將use warnings;包括在shebang行的下面。

2 /您還應該添加use strict;正好低於use warnings;。這似乎會完全破壞你的程序,因爲它會堅持要你預先聲明所有的變量 - 你使用的是mymy $host = 'localhost'等)。起初這會很痛苦,但這是一個很好的習慣。

3 /您只需要一個use CGI行。它應該是use CGI qw/param/;

4 /您正在使用一個名爲Mysql的類。但是你不會在任何地方加載該課程。這個班從哪裏來?你應該真的使用DBIDBD::mysql的組合。

5 /您將SQL查詢放入$mysquery,但您立即用另一個覆蓋它。這不會導致錯誤 - 這可能只是您的一些混淆跡象。

6 /你的if ($id =~ /$ticket_id/)幾乎肯定會寫得更好if ($id == $ticket_id)。如果您只是比較兩個數字,則無需使用正則表達式。

7 /你應該真的檢查你的電話返回值爲open

open(FILE, ">$file") or die $!; 

事實上,我會重寫使用3-arg open和autovivified文件句柄。

open(my $fh, '>', $file) or die $!; 

然後您就需要改變FILE$fh整個代碼。 8 /同樣,您的三個比較$message可以簡單地與==匹配,而不是使用正則表達式匹配。

如果你要做很多這個,那麼我真的推薦閱讀一本好書,比如「Learning Perl」或「Beginning Perl」。

+0

沒有看到清理的代碼,我無法真正幫助。也許您可以將當前的代碼發佈爲原始帖子的更新。 – 2013-02-21 17:08:54

+0

儘管我會指出,如果您只是花時間仔細閱讀,Perl的錯誤消息通常很有意義。當他們不這樣做時,嘗試在代碼中添加'use diagnostics'。 – 2013-02-21 17:35:03

+0

發佈清理後的代碼版本時,最好將其添加到帖子末尾,而不是覆蓋原始版本。這樣,其他人的評論看起來不錯。我編輯了你的帖子來糾正這個問題。 – 2013-02-22 07:56:22