index.php
Index

Create Date : 1398/07/29 - 21:10
Download Code View Raw

Embed Code

<?php
// Target URL
$target_url = "https://miladworkshop.ir";

// Database Setting
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysql_name = "demo";

// Dont Edit This
$dom = new DOMDocument();
@$dom->loadHTML(file_get_contents($target_url));
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

// Database Connection
$db = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_name);

$source 	= 0;
$errors 	= 0;
$duplicate 	= 0;
$report 	= array();

for($i = 0; $i < $hrefs->length; $i++)
{
	$href 	= $hrefs->item($i);
	$title 	= $href->nodeValue;
	$url 	= $href->getAttribute('href');
	$url 	= filter_var($url, FILTER_SANITIZE_URL);
	$key 	= md5($url);

	// Validate URL
	if(!filter_var($url, FILTER_VALIDATE_URL) === false)
	{
		$num_row = $db->query("SELECT * FROM `site_data` WHERE `key` = '{$key}'");

		if (mysqli_num_rows($num_row) > 0)
		{
			$duplicate++;
			$report[] = "Duplicate Data : {$key}";
		} else {
			if ($db->query("insert into `site_data` (`key`, `title`, `source_url`) values('{$key}','{$title}','{$url}')"))
			{
				$source++;
				$report[] = "Data added successfully : {$key}";
			} else {		
				$errors++;
				$report[] = "Error : ". mysqli_error($db);
			}
		}
	}
}

echo "<span style='color:green;'>{$source}</span> Success - <span style='color:orange;'>{$duplicate}</span> Duplicate</span> - <span style='color:red;'>{$errors}</span> Failed</span>";

// Report
echo "<hr /><textarea rows=16 cols=100>";
	foreach ($report as $err)
	{
		echo "{$err}\n";
	}
echo "</textarea>";
?>
Powered By Askoverflow PasteCode System