: product-specific title, description, canonical, Open Graph and JSON-LD (Product + BreadcrumbList), so Facebook / Messenger / Viber previews and Google rich results work without running JavaScript. 2. : the product's name (h1), price, photos, description, specs, breadcrumb and related-product links are rendered into #pdpRoot on the server. Google gets real content in the very first response instead of an empty "Loading…" shell — a new, low-authority site with 2,000+ JS-only pages otherwise sits in "Discovered – currently not indexed". js/product-page.js then replaces #pdpRoot with the interactive version (gallery, variants, cart) exactly as before, so visitors see the same page; the same facts are in both, which is what Google requires (no cloaking). 3. Unknown / inactive id → HTTP 404 + noindex (not a "soft 404" 200 page). SAFE FALLBACK: if the DB is unavailable, the original product.html is served unchanged (200, generic tags) — the page never breaks and a DB hiccup never produces a 404. */ $tpl = __DIR__ . '/product.html'; $html = is_file($tpl) ? file_get_contents($tpl) : ''; $ogBlock = ''; $ssr = ''; $notFound = false; $h = function ($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }; /* Admin rich HTML is sanitised on save; strip scripts / inline handlers again as defence in depth. */ $safeHtml = function ($s) { $s = preg_replace('~<(script|style|iframe|object|embed)\b[^>]*>.*?~is', '', (string)$s); $s = preg_replace('~\son\w+\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+)~i', '', $s); return preg_replace('~(href|src)\s*=\s*(["\'])\s*javascript:[^"\']*\2~i', '$1="#"', $s); }; $peso = function ($n) { $n = (float)$n; return '₱' . number_format($n, floor($n) == $n ? 0 : 2); }; try { require_once __DIR__ . '/php/config.php'; require_once __DIR__ . '/php/_db.php'; if (is_file(__DIR__ . '/php/_sale.php')) require_once __DIR__ . '/php/_sale.php'; $SITE = defined('GH_SITE_URL') ? rtrim(GH_SITE_URL, '/') : 'https://shop.goodhand88.com'; $pid = preg_replace('~[^a-z0-9-]~', '', strtolower($_GET['id'] ?? '')); $db = function_exists('gh_db') ? gh_db() : null; if ($db && $pid !== '') { $st = $db->prepare("SELECT id, pid, name, brand, cat, price+0 AS price, old_price+0 AS oldPrice, descr, long_descr, specs, specs_mode, image, gallery, warranty, stock_status, flash_price+0 AS flashPrice, flash_ends FROM gh_products WHERE pid = ? AND active = 1"); $st->execute([$pid]); $p = $st->fetch(PDO::FETCH_ASSOC); if (!$p) { $notFound = true; } else { $abs = function ($u) use ($SITE) { return preg_match('~^https?://~', $u) ? $u : $SITE . '/' . ltrim($u, '/'); }; $canonical = $SITE . '/product.html?id=' . $pid; $price = (float)$p['price']; $old = (float)$p['oldPrice']; // ── Price the customer actually pays today (same rules as php/product.php) ── $sellPrice = $price; $saleEnds = ''; if ((float)$p['flashPrice'] > 0 && !empty($p['flash_ends']) && strtotime($p['flash_ends']) > time()) { $sellPrice = (float)$p['flashPrice']; $saleEnds = substr((string)$p['flash_ends'], 0, 10); } if (function_exists('gh_sale_price_for')) { $sp = gh_sale_price_for(['pid' => $p['pid'], 'cat' => $p['cat'], 'brand' => $p['brand']], $price); if ($sp > 0 && $sp < $sellPrice) { $sellPrice = $sp; $ev = gh_sale_live(); $saleEnds = $ev ? substr((string)$ev['ends_at'], 0, 10) : ''; } } $wasPrice = $sellPrice < $price ? $price : ($old > $price ? $old : 0); // ── Photos ── $gallery = array_values(array_filter(array_map('trim', preg_split('~[\r\n,]+~', (string)$p['gallery'])))); if ($p['image'] && !in_array($p['image'], $gallery, true)) array_unshift($gallery, $p['image']); $gallery = array_values(array_unique($gallery)); $img = $gallery ? $abs($gallery[0]) : $SITE . '/images/og-cover.jpg'; // ── Breadcrumb: deepest taxonomy node the product sits in, walked up to its department ── $crumbs = []; try { $tq = $db->prepare("SELECT t.id, t.parent_id, t.level, t.name, t.slug FROM gh_product_taxonomy pt JOIN gh_taxonomy t ON t.id = pt.taxonomy_id WHERE pt.product_id = ? ORDER BY t.level DESC LIMIT 1"); $tq->execute([(int)$p['id']]); $node = $tq->fetch(PDO::FETCH_ASSOC); $up = $db->prepare("SELECT id, parent_id, level, name, slug FROM gh_taxonomy WHERE id = ?"); for ($guard = 0; $node && $guard < 5; $guard++) { array_unshift($crumbs, ['name' => $node['name'], 'url' => 'products.html?cat=' . rawurlencode($node['slug'])]); if (empty($node['parent_id'])) break; $up->execute([(int)$node['parent_id']]); $node = $up->fetch(PDO::FETCH_ASSOC); } } catch (Exception $e) { $crumbs = []; } if (!$crumbs && $p['cat']) { $labels = ['cctv' => 'CCTV Cameras', 'radios' => 'Two-Way Radios', 'locks' => 'Smart Locks & Access', 'alarms' => 'Alarm Systems', 'fence' => 'Electric Fence', 'network' => 'Networking']; $crumbs[] = ['name' => $labels[$p['cat']] ?? ucwords(str_replace('-', ' ', $p['cat'])), 'url' => 'products.html?cat=' . rawurlencode($p['cat'])]; } // ── Short description: "*a *b *c" feature strings become a real list ── $descText = trim(strip_tags((string)$p['descr'])); $shortHtml = ''; if ($descText !== '') { $parts = array_values(array_filter(array_map('trim', explode('*', $descText)), 'strlen')); if (substr_count($descText, '*') >= 2 && count($parts) >= 2) { $shortHtml = ''; } else { $shortHtml = '

' . nl2br($h($descText)) . '

'; } } // ── Specs ── $specsHtml = ''; if (($p['specs_mode'] ?? 'simple') === 'html') { if (trim(strip_tags((string)$p['specs'])) !== '') $specsHtml = $safeHtml($p['specs']); } else { $rows = ''; foreach (preg_split('~[\r\n]+~', (string)$p['specs']) as $line) { $line = trim($line); if ($line === '') continue; $kv = explode(':', $line, 2); $rows .= count($kv) === 2 ? '' . $h(trim($kv[0])) . '' . $h(trim($kv[1])) . '' : '' . $h($line) . ''; } if ($rows !== '') $specsHtml = '' . $rows . '
'; } // ── Long description (rich HTML from the product editor) ── $long = trim((string)$p['long_descr']); $longHtml = ''; if ($long !== '') { $longHtml = preg_match('~<[a-z][\s\S]*>~i', $long) ? $safeHtml($long) : '

' . nl2br($h($long)) . '

'; } // ── Related products: internal links Google can follow to the rest of the catalogue ── $related = []; try { $rq = $db->prepare("SELECT pid, name, brand, price+0 AS price FROM gh_products WHERE active = 1 AND cat = ? AND pid <> ? ORDER BY featured DESC, sort LIMIT 8"); $rq->execute([$p['cat'], $pid]); $related = $rq->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) { $related = []; } // ── Reviews (only real, approved ones feed the stars) ── $rCount = 0; $rAvg = 0; $revs = []; try { $rv = $db->prepare("SELECT COUNT(*) c, AVG(rating) a FROM gh_reviews WHERE product_pid=? AND approved=1"); $rv->execute([$pid]); $rr = $rv->fetch(PDO::FETCH_ASSOC); $rCount = (int)($rr['c'] ?? 0); $rAvg = $rCount ? round((float)$rr['a'], 1) : 0; if ($rCount > 0) { $rs = $db->prepare("SELECT author, rating, title, body, created_at FROM gh_reviews WHERE product_pid=? AND approved=1 ORDER BY created_at DESC LIMIT 3"); $rs->execute([$pid]); $revs = $rs->fetchAll(PDO::FETCH_ASSOC); } } catch (Exception $e) {} $stockMap = ['in' => 'In stock', 'low' => 'Low stock, order soon', 'out' => 'Out of stock', 'order' => 'Available to order']; $stockTxt = $stockMap[$p['stock_status']] ?? 'In stock'; // ════════════════ ════════════════ $title = $p['name'] . ($p['brand'] ? ' — ' . $p['brand'] : '') . ' | GoodHand Security'; $priceTxt = $sellPrice > 0 ? $peso($sellPrice) : 'Inquire for price'; $descFlat = trim(preg_replace('~\s*\*\s*~', ' · ', $descText), " ·\t\n\r"); $metaDesc = $priceTxt . ' · ' . ($descFlat !== '' ? $descFlat : 'Authorized Platinum Dealer — genuine units with warranty. Nationwide delivery & 20+ branches. GCash, Maya, COD & branch pickup.'); $metaDesc = trim(preg_replace('~\s+~', ' ', $metaDesc)); if (function_exists('mb_substr')) $metaDesc = mb_substr($metaDesc, 0, 300, 'UTF-8'); $ogBlock = "" . $h($title) . "\n"; $ogBlock .= '\n"; $ogBlock .= '\n"; $ogBlock .= '' . "\n"; $ogBlock .= '' . "\n"; $ogBlock .= '' . "\n"; $ogBlock .= '\n"; $ogBlock .= '\n"; $ogBlock .= '\n"; $ogBlock .= '\n"; $ogBlock .= '\n"; if ($sellPrice > 0) { $ogBlock .= '\n"; $ogBlock .= '' . "\n"; } $ogBlock .= '' . "\n"; $ogBlock .= '\n"; $ogBlock .= '\n"; $ogBlock .= ''; // Product JSON-LD $ld = [ '@context' => 'https://schema.org/', '@type' => 'Product', 'name' => $p['name'], 'image' => array_map($abs, array_slice($gallery ?: ['images/og-cover.jpg'], 0, 6)), 'description' => $descText !== '' ? preg_replace('~\s*\*\s*~', '. ', trim($descText, "* \t\n\r")) : $p['name'], 'sku' => $p['pid'], 'mpn' => $p['pid'], 'url' => $canonical, ]; if ($crumbs) $ld['category'] = implode(' > ', array_column($crumbs, 'name')); if ($p['brand']) $ld['brand'] = ['@type' => 'Brand', 'name' => $p['brand']]; if ($sellPrice > 0) { $availMap = ['in' => 'InStock', 'low' => 'LimitedAvailability', 'out' => 'OutOfStock', 'order' => 'BackOrder']; $ld['offers'] = [ '@type' => 'Offer', 'url' => $canonical, 'priceCurrency' => 'PHP', 'price' => (string)(0 + $sellPrice), 'availability' => 'https://schema.org/' . ($availMap[$p['stock_status']] ?? 'InStock'), 'itemCondition' => 'https://schema.org/NewCondition', 'seller' => ['@type' => 'Organization', 'name' => 'GoodHand Security Products Corp'], ]; if ($saleEnds !== '') $ld['offers']['priceValidUntil'] = $saleEnds; } if ($rCount > 0) { $ld['aggregateRating'] = ['@type' => 'AggregateRating', 'ratingValue' => (string)$rAvg, 'reviewCount' => (string)$rCount, 'bestRating' => '5']; $ld['review'] = []; foreach ($revs as $rvw) { $body = trim(strip_tags((string)$rvw['body'])); $ld['review'][] = [ '@type' => 'Review', 'author' => ['@type' => 'Person', 'name' => $rvw['author'] ?: 'Verified buyer'], 'reviewRating' => ['@type' => 'Rating', 'ratingValue' => (string)(int)$rvw['rating'], 'bestRating' => '5'], 'name' => $rvw['title'] ?: 'Review', 'reviewBody' => ($body !== '' ? $body : ($rvw['title'] ?: 'Great product')), 'datePublished' => substr((string)$rvw['created_at'], 0, 10), ]; } } $ogBlock .= "\n" . ''; // BreadcrumbList JSON-LD $bl = [['@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => $SITE . '/'], ['@type' => 'ListItem', 'position' => 2, 'name' => 'Products', 'item' => $SITE . '/products.html']]; foreach ($crumbs as $c) $bl[] = ['@type' => 'ListItem', 'position' => count($bl) + 1, 'name' => $c['name'], 'item' => $SITE . '/' . $c['url']]; $bl[] = ['@type' => 'ListItem', 'position' => count($bl) + 1, 'name' => $p['name'], 'item' => $canonical]; $ogBlock .= "\n" . ''; // ════════════════ : crawlable product, replaced by the interactive page on load ════════════════ $ssr = '
' . "\n"; $ssr .= '\n"; $ssr .= '
' . "\n"; $ssr .= '
'; if ($gallery) { $ssr .= '' . $h($p['name']) . ''; if (count($gallery) > 1) { $ssr .= '
'; foreach (array_slice($gallery, 1, 5) as $i => $g) $ssr .= '' . $h($p['name'] . ', photo ' . ($i + 2)) . ''; $ssr .= '
'; } } $ssr .= "
\n"; $ssr .= '
'; if ($p['brand']) $ssr .= '

' . $h($p['brand']) . '

'; $ssr .= '

' . $h($p['name']) . '

'; if ($rCount > 0) $ssr .= '

★ ' . $h($rAvg) . ' / 5 · ' . $rCount . ' review' . ($rCount > 1 ? 's' : '') . '

'; $ssr .= '

' . $h($priceTxt) . ''; if ($sellPrice > 0 && $wasPrice > $sellPrice) $ssr .= ' ' . $h($peso($wasPrice)) . ''; $ssr .= '

'; $ssr .= '

' . $h($stockTxt) . ($p['warranty'] ? ' · Warranty: ' . $h($p['warranty']) : '') . '

'; $ssr .= $shortHtml; $ssr .= '

Authorized Platinum Dealer: genuine units with valid Philippine warranty. Nationwide delivery, branch pickup at 20+ GoodHand branches, GCash, Maya & COD.

'; $ssr .= "
\n
\n"; if ($longHtml !== '') $ssr .= '

Overview

' . $longHtml . "
\n"; if ($specsHtml !== '') $ssr .= '

Specifications

' . $specsHtml . "
\n"; if ($related) { $ssr .= '

Related products

'; $last = end($crumbs); if ($last) $ssr .= '

Browse all ' . $h($last['name']) . ' →

'; $ssr .= "
\n"; } $ssr .= '

Need it installed? CCTV installation with a free site inspection · 2026 CCTV package prices

' . "\n"; $ssr .= '
'; } } } catch (Throwable $e) { $ogBlock = ''; $ssr = ''; $notFound = false; } // any runtime error → plain product.html, never a broken page // Swap the marker block for product-specific tags (only when we built them) if ($ogBlock !== '' && $html !== '' && strpos($html, '') !== false) { $html = preg_replace('~.*?~s', str_replace(['\\', '$'], ['\\\\', '\\$'], $ogBlock), $html, 1); } // Put the crawlable product where the "Loading…" placeholder was (inside #pdpRoot) if ($ssr !== '' && $html !== '') { $html = preg_replace('~
.*?
~s', str_replace(['\\', '$'], ['\\\\', '\\$'], $ssr), $html, 1); } // A product that doesn't exist: real 404 + noindex, so it drops out of Google instead of lingering as a soft 404 if ($notFound) { http_response_code(404); if ($html !== '') $html = preg_replace('~~i', "\n", $html, 1); } header('Content-Type: text/html; charset=UTF-8'); echo $html !== '' ? $html : 'GoodHand Security

Loading…

';