Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

أرشفة المواقع المبنية بلغة php بشكل أسرع

  • خطوات إنشاء ملف site-map و تضمينه
  • طريقة تعريف موقعك في أدوات مشرفي محركات البحث جوجل
  • طريقة أرشفة المعلومات آلياً

من أهم الأشياء التي عليك الإهتمام بها إن كان موقعك يعرض مقالات, أخبار, معلومات حصرية إلخ.. هو أرشفة محتوى الموقع مباشرةً عند نشر المقالات أو أرشفة المحتوى بعد فترة قصيرة.

المقصود بأرشفة المحتوى, إعلام محركات البحث مثل Google و غيرها بأن محتواك قد تم تحديثه لأن محركات البحث تستغرق على الأقل 24 ساعة حتى تقوم بأرشفة الموقع إن لم ترسل لها طلب مباشر بذلك.

لأرشفة كامل محتوى الموقع و بشكل تلقائي, يجب أن تفعل الخطوات التالية:

  • تضمين الملفات المطلوبة في المجلد الرئيسي للموقع.
  • تشغيل ملف map.php على المتصفح ليتم إنشاء ملف بصيغة XML ليتم التعرف عليه في أدوات مشرفي محركات البحث جوجل.
  • إنشاء Cron Job لملف map.php ليتم تحديث معلومات الملف المرتبط بأدوات مشرفي محركات البحث جوجل بشكل تلقائي خلال مدة أنت تحددها حتى يتم أرشفة المحتوى كلما صار الوقت الذي حددته أنت لإعادة الأرشفة بدل إنتظار محركات البحث أن تفعل ذلك.

 

خطوات إنشاء ملف site-map و تضمينه

1- قم بإنشاء ملف بإسم site_map.php و قم بوضع الكود التالي فيه كما هو.

<?php
/**
* Sitemap
*
* This class used for generating Google Sitemap files
*
* @package Sitemap
* @author Osman Üngür <osmanungur@gmail.com>
* @copyright 2009-2015 Osman Üngür
* @license http://opensource.org/licenses/MIT MIT License
* @link http://github.com/o/sitemap-php
*/
class Sitemap {
/**
*
* @var XMLWriter
*/
private $writer;
private $domain;
private $path;
private $filename = 'sitemap';
private $current_item = 0;
private $current_sitemap = 0;
const EXT = '.xml';
const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9';
const DEFAULT_PRIORITY = 0.5;
const ITEM_PER_SITEMAP = 50000;
const SEPERATOR = '-';
const INDEX_SUFFIX = 'index';
/**
*
* @param string $domain
*/
public function __construct($domain) {
$this->setDomain($domain);
}
/**
* Sets root path of the website, starting with http:// or https://
*
* @param string $domain
*/
public function setDomain($domain) {
$this->domain = $domain;
return $this;
}
/**
* Returns root path of the website
*
* @return string
*/
private function getDomain() {
return $this->domain;
}
/**
* Returns XMLWriter object instance
*
* @return XMLWriter
*/
private function getWriter() {
return $this->writer;
}
/**
* Assigns XMLWriter object instance
*
* @param XMLWriter $writer
*/
private function setWriter(XMLWriter $writer) {
$this->writer = $writer;
}
/**
* Returns path of sitemaps
*
* @return string
*/
private function getPath() {
return $this->path;
}
/**
* Sets paths of sitemaps
*
* @param string $path
* @return Sitemap
*/
public function setPath($path) {
$this->path = $path;
return $this;
}
/**
* Returns filename of sitemap file
*
* @return string
*/
private function getFilename() {
return $this->filename;
}
/**
* Sets filename of sitemap file
*
* @param string $filename
* @return Sitemap
*/
public function setFilename($filename) {
$this->filename = $filename;
return $this;
}
/**
* Returns current item count
*
* @return int
*/
private function getCurrentItem() {
return $this->current_item;
}
/**
* Increases item counter
*
*/
private function incCurrentItem() {
$this->current_item = $this->current_item + 1;
}
/**
* Returns current sitemap file count
*
* @return int
*/
private function getCurrentSitemap() {
return $this->current_sitemap;
}
/**
* Increases sitemap file count
*
*/
private function incCurrentSitemap() {
$this->current_sitemap = $this->current_sitemap + 1;
}
/**
* Prepares sitemap XML document
*
*/
private function startSitemap() {
$this->setWriter(new XMLWriter());
if ($this->getCurrentSitemap()) {
$this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT);
} else {
$this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::EXT);
}
$this->getWriter()->startDocument('1.0', 'UTF-8');
$this->getWriter()->setIndent(true);
$this->getWriter()->startElement('urlset');
$this->getWriter()->writeAttribute('xmlns', self::SCHEMA);
}
/**
* Adds an item to sitemap
*
* @param string $loc URL of the page. This value must be less than 2,048 characters.
* @param string $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0.
* @param string $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never.
* @param string|int $lastmod The date of last modification of url. Unix timestamp or any English textual datetime description.
* @return Sitemap
*/
public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL) {
if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) {
if ($this->getWriter() instanceof XMLWriter) {
$this->endSitemap();
}
$this->startSitemap();
$this->incCurrentSitemap();
}
$this->incCurrentItem();
$this->getWriter()->startElement('url');
$this->getWriter()->writeElement('loc', $this->getDomain() . $loc);
$this->getWriter()->writeElement('priority', $priority);
if ($changefreq)
$this->getWriter()->writeElement('changefreq', $changefreq);
if ($lastmod)
$this->getWriter()->writeElement('lastmod', $this->getLastModifiedDate($lastmod));
$this->getWriter()->endElement();
return $this;
}
/**
* Prepares given date for sitemap
*
* @param string $date Unix timestamp or any English textual datetime description
* @return string Year-Month-Day formatted date.
*/
private function getLastModifiedDate($date) {
if (ctype_digit($date)) {
return date('Y-m-d', $date);
} else {
$date = strtotime($date);
return date('Y-m-d', $date);
}
}
/**
* Finalizes tags of sitemap XML document.
*
*/
private function endSitemap() {
if (!$this->getWriter()) {
$this->startSitemap();
}
$this->getWriter()->endElement();
$this->getWriter()->endDocument();
}
/**
* Writes Google sitemap index for generated sitemap files
*
* @param string $loc Accessible URL path of sitemaps
* @param string|int $lastmod The date of last modification of sitemap. Unix timestamp or any English textual datetime description.
*/
public function createSitemapIndex($loc, $lastmod = 'Today') {
$this->endSitemap();
$indexwriter = new XMLWriter();
$indexwriter->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . self::INDEX_SUFFIX . self::EXT);
$indexwriter->startDocument('1.0', 'UTF-8');
$indexwriter->setIndent(true);
$indexwriter->startElement('sitemapindex');
$indexwriter->writeAttribute('xmlns', self::SCHEMA);
for ($index = 0; $index < $this->getCurrentSitemap(); $index++) {
$indexwriter->startElement('sitemap');
$indexwriter->writeElement('loc', $loc . $this->getFilename() . ($index ? self::SEPERATOR . $index : '') . self::EXT);
$indexwriter->writeElement('lastmod', $this->getLastModifiedDate($lastmod));
$indexwriter->endElement();
}
$indexwriter->endElement();
$indexwriter->endDocument();
}
}
<?php /** * Sitemap * * This class used for generating Google Sitemap files * * @package Sitemap * @author Osman Üngür <osmanungur@gmail.com> * @copyright 2009-2015 Osman Üngür * @license http://opensource.org/licenses/MIT MIT License * @link http://github.com/o/sitemap-php */ class Sitemap { /** * * @var XMLWriter */ private $writer; private $domain; private $path; private $filename = 'sitemap'; private $current_item = 0; private $current_sitemap = 0; const EXT = '.xml'; const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9'; const DEFAULT_PRIORITY = 0.5; const ITEM_PER_SITEMAP = 50000; const SEPERATOR = '-'; const INDEX_SUFFIX = 'index'; /** * * @param string $domain */ public function __construct($domain) { $this->setDomain($domain); } /** * Sets root path of the website, starting with http:// or https:// * * @param string $domain */ public function setDomain($domain) { $this->domain = $domain; return $this; } /** * Returns root path of the website * * @return string */ private function getDomain() { return $this->domain; } /** * Returns XMLWriter object instance * * @return XMLWriter */ private function getWriter() { return $this->writer; } /** * Assigns XMLWriter object instance * * @param XMLWriter $writer */ private function setWriter(XMLWriter $writer) { $this->writer = $writer; } /** * Returns path of sitemaps * * @return string */ private function getPath() { return $this->path; } /** * Sets paths of sitemaps * * @param string $path * @return Sitemap */ public function setPath($path) { $this->path = $path; return $this; } /** * Returns filename of sitemap file * * @return string */ private function getFilename() { return $this->filename; } /** * Sets filename of sitemap file * * @param string $filename * @return Sitemap */ public function setFilename($filename) { $this->filename = $filename; return $this; } /** * Returns current item count * * @return int */ private function getCurrentItem() { return $this->current_item; } /** * Increases item counter * */ private function incCurrentItem() { $this->current_item = $this->current_item + 1; } /** * Returns current sitemap file count * * @return int */ private function getCurrentSitemap() { return $this->current_sitemap; } /** * Increases sitemap file count * */ private function incCurrentSitemap() { $this->current_sitemap = $this->current_sitemap + 1; } /** * Prepares sitemap XML document * */ private function startSitemap() { $this->setWriter(new XMLWriter()); if ($this->getCurrentSitemap()) { $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); } else { $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::EXT); } $this->getWriter()->startDocument('1.0', 'UTF-8'); $this->getWriter()->setIndent(true); $this->getWriter()->startElement('urlset'); $this->getWriter()->writeAttribute('xmlns', self::SCHEMA); } /** * Adds an item to sitemap * * @param string $loc URL of the page. This value must be less than 2,048 characters. * @param string $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. * @param string $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never. * @param string|int $lastmod The date of last modification of url. Unix timestamp or any English textual datetime description. * @return Sitemap */ public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL) { if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) { if ($this->getWriter() instanceof XMLWriter) { $this->endSitemap(); } $this->startSitemap(); $this->incCurrentSitemap(); } $this->incCurrentItem(); $this->getWriter()->startElement('url'); $this->getWriter()->writeElement('loc', $this->getDomain() . $loc); $this->getWriter()->writeElement('priority', $priority); if ($changefreq) $this->getWriter()->writeElement('changefreq', $changefreq); if ($lastmod) $this->getWriter()->writeElement('lastmod', $this->getLastModifiedDate($lastmod)); $this->getWriter()->endElement(); return $this; } /** * Prepares given date for sitemap * * @param string $date Unix timestamp or any English textual datetime description * @return string Year-Month-Day formatted date. */ private function getLastModifiedDate($date) { if (ctype_digit($date)) { return date('Y-m-d', $date); } else { $date = strtotime($date); return date('Y-m-d', $date); } } /** * Finalizes tags of sitemap XML document. * */ private function endSitemap() { if (!$this->getWriter()) { $this->startSitemap(); } $this->getWriter()->endElement(); $this->getWriter()->endDocument(); } /** * Writes Google sitemap index for generated sitemap files * * @param string $loc Accessible URL path of sitemaps * @param string|int $lastmod The date of last modification of sitemap. Unix timestamp or any English textual datetime description. */ public function createSitemapIndex($loc, $lastmod = 'Today') { $this->endSitemap(); $indexwriter = new XMLWriter(); $indexwriter->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . self::INDEX_SUFFIX . self::EXT); $indexwriter->startDocument('1.0', 'UTF-8'); $indexwriter->setIndent(true); $indexwriter->startElement('sitemapindex'); $indexwriter->writeAttribute('xmlns', self::SCHEMA); for ($index = 0; $index < $this->getCurrentSitemap(); $index++) { $indexwriter->startElement('sitemap'); $indexwriter->writeElement('loc', $loc . $this->getFilename() . ($index ? self::SEPERATOR . $index : '') . self::EXT); $indexwriter->writeElement('lastmod', $this->getLastModifiedDate($lastmod)); $indexwriter->endElement(); } $indexwriter->endElement(); $indexwriter->endDocument(); } }


2- قم بإنشاء ملف بإسم map.php و قم بذكر المعلومات التي ذكرتها لك ضمن الكود.

<?php
// قم بتبديل الإسم config هنا يتفرض أن تقوم بتضمين ملف الاتصال بقاعدة البيانات, في حال لم يكن إسمه
include_once "config.php";
// والذي قمنا بإنشئه سابقا site_map.php هنا قمنا بتضمين الملف
include_once "site_map.php";
$sitemap = new Sitemap("رابط الموقع");
$sitemap->setPath(dirname(__FILE__).'/');
$sitemap->setFilename('اسم المجلد');
// تقوم بتكرار آخر سطر برمجي وإضافة جميع الصفحات التي ترغب بأرشفتها
$sitemap->addItem('/page_link', '1.0', 'daily', 'Today');
// الأن طريقة أرشفة المحتوى مثل المقالات والأخبار ... الخ
$get_articles = $conn->query("SELECT * FROM articles");
foreach ($get_articles as $value) {
$sitemap->addItem('/articles', '1.0', 'daily',$value['title']);
}
// و بقية المعلومات الموجودة في قاعدة البيانات يتم التعامل معها بالطريقة السابقة
?>
<?php // قم بتبديل الإسم config هنا يتفرض أن تقوم بتضمين ملف الاتصال بقاعدة البيانات, في حال لم يكن إسمه include_once "config.php"; // والذي قمنا بإنشئه سابقا site_map.php هنا قمنا بتضمين الملف include_once "site_map.php"; $sitemap = new Sitemap("رابط الموقع"); $sitemap->setPath(dirname(__FILE__).'/'); $sitemap->setFilename('اسم المجلد'); // تقوم بتكرار آخر سطر برمجي وإضافة جميع الصفحات التي ترغب بأرشفتها $sitemap->addItem('/page_link', '1.0', 'daily', 'Today'); // الأن طريقة أرشفة المحتوى مثل المقالات والأخبار ... الخ $get_articles = $conn->query("SELECT * FROM articles"); foreach ($get_articles as $value) { $sitemap->addItem('/articles', '1.0', 'daily',$value['title']); } // و بقية المعلومات الموجودة في قاعدة البيانات يتم التعامل معها بالطريقة السابقة ?>


طريقة تعريف موقعك في أدوات مشرفي محركات البحث جوجل

1- أدخل لموقع أدوات مشرفي محركات البحث جوجل.

2- عند الدخول للموقع, قم بتسجيل الدخول بواسطة Gmail الخاص بك أو بالذي ترغب بربطه بموقع الأرشفة.


3- قم بإختيار النطاق الذي يظهر بجانبه عبارة “ميزة جديدة”.

4- قم بإدخال إسم الموقع (أي النطاق) و من ثم أنقر متابعة ليتحقق من ملكيتك لنطاق الموقع.


5- قم بنسخ النص الذي أعطاه لك و الذي يبدأ بعبارة google-site-verification

إنتبه: لا تنقر على عبارة تأكيد الآن لأنك ستفعل خطوة أخرى قبل النقر على الزر.


6- أضف الوسم التالي في هيدر الصفحة, أي في الوسم <head>..</head>

<meta name="google-site-verification" content="ضع هنا النص الذي قمت بنسخه قبل قليل">
<meta name="google-site-verification" content="ضع هنا النص الذي قمت بنسخه قبل قليل">


7- بعد إضافة الوسم في الهيدر, أنقر على زر تأكيد الذي يظهر في الصورة السابقة. 

8- بعد إثبات ملكيتك للنطاق ستفتح الصفحة التالية.

9- في القائمة الجانبية, أنقر على فهرس, ثم إختر ملفات Sitemap.

10- ضع مسار ملف map.php , ثم أنقر على إرسال.


طريقة أرشفة المعلومات آلياً

عند الدخول للمجلد الذي فيه صفحة map.php ستلاحظ أنه تم إنشاء ملف جديد بصيغة XML، هذا الملف هو الملف الذي ستكون فيه خريطة الأرشفة و سوف يتم معاينته من قبل موقع أدوات مشرفي محركات جوجل لأخذ المعلومات منه ثم أرشفتها على محرك البحث، و لكن عند إضافة معلومات جديدة في الموقع يتطلب عليك في كل مرة تشغيل صفحة map.php على المتصفح لنقل المعلومات إلى الملف الذي بصيغة XML و هذا الأمر يعد شاقاً و مملاً لأنك ما بين حين و آخر تقوم بتشغيل الملف، إذا كان لديك في إستضافة الموقع لوحة Cpanel يمكنك إنشاء مهمة من السيرفر تقوم بفتح الصفحة تلقائياً في أوقات أنت تتحدها. 

لتفعيل المهام من Cpanel إتبع الخطوات التالية:

1- قم بالدخول على Cpanel ثم إختيار Cron jobs.


2- حدد كم مره ترغب بتشغيل الملف (مرة يومياً، أسبوعياً، شهرياً، إلخ.. ).

3- قم بكتابة curl ثم مسار ملف map.php

4- اضغط على Add New Cron Jobs

آخر تحديث في 06-01-2024

الكاتب

علاء نجمي

رئيس قسم المطوّرين في نُضج الرقمية. حاصل على الشهادات الاحترافية HCIA-AI,PCAP,PCEP,CLE. كاتب ومُقدم دورات بمجال التقنية والبرمجة. مهتم بالأنظمة وحمايتها وتطويرها

تعليقات 1

أضف تعليق

يجب تسجيل الدخول حتى تتمكن من إضافة تعليق أو رد.