· 8 years ago · Jun 24, 2018, 01:30 AM
1<html>
2
3<head><title>Site-Spider-WEBSPR</title></head>
4
5<style>
6body {font-family: courier; color: #00FF00; background-color: #000000;}
7</style>
8
9<body>
10<h2>Spidering the database: Results/Connections</h2>
11
12<?
13//Variables
14$dir = (".");
15
16
17//Connect to MySQL, create database (table/fields) or select database if already exists
18function dB_open() {
19 mysql_connect("localhost","root") or die("Error: Could not connect to MySQL.");
20 if (mysql_query("create database index_dB")) {
21 mysql_select_db("index_dB") or die("Error: couldn't select a new database");
22 mysql_query("create table words (word varchar(40),filename varchar(100))");
23 }
24 else
25 mysql_select_db("index_dB") or die("couldn't select a old database");
26 echo "<p><b>**Database opened OK**</b><p>";
27
28
29}
30//Remove duplicates for when re-spidering
31function removeDuplicates($file) {
32 mysql_query("delete from words where filename='$file'");
33 // echo "<p><b>Bombed $file.</b><p>";
34}
35
36//Write to database
37function write2Database($word,$file) {
38 $w2dB = mysql_query("select * from words where word='$word' and filename='$file'");
39 if (is_resource($w2dB)) {
40 if (mysql_num_rows($w2dB)==0)
41 mysql_query("insert into words values ('$word','$file')");
42 }
43}
44//Close MySQL connection
45function dB_close() {
46 mysql_close();
47 echo "<p><b>**Databse closed OK**</b><p>";
48}
49
50
51//Spider the dir, echo the words within file
52function dB_crawl($file) {
53 echo "words in $file<br><br>";
54 removeDuplicates($file);
55 $words = file_get_contents($file);
56 $exSt = explode(" ", $words); //Explode the contents as mulit-line
57 //stripData($words);
58 foreach ($exSt as $word) {
59 echo strip_tags($word);
60 write2Database(rtrim($word),$file);
61 }
62 }
63 echo "<br>-------------------------------------------<p>";
64
65
66
67//Scan the directory
68function spiderDir($dir) {
69 $igFiles = array("spider.man.php","search.php","stop_list.txt","search_form.html");
70 $files = scandir($dir);
71 foreach($files as $file) {
72 if (!in_array($file, $igFiles)){ //Ignore certain files
73 if (is_file($file) && is_readable($file)) { //Check if $file is a file an if its readable
74 if (strtolower(substr($file,-4))) {
75 echo "Spidering file $file<br><br>";
76 dB_crawl($file);
77 }
78 }
79 }
80 else if (is_dir($file)) {
81 //echo "Spidering directory $file - later!<br>";
82 // spiderDir($dir.$file);
83 //echo "finished spidering directory $file - later!<br>";
84 }
85 }
86}
87
88
89
90
91//Open database, start spider on completion close connection
92dB_open();
93
94spiderDir($dir);
95
96dB_close();
97
98?>
99
100</body>
101
102</html>