Welcome Guest, Not a member yet? Register   Sign In
Best way to insert user written metatags/scripts - a question for PHP buffs
#1

Hello! So I need an opinion from you guys, what would you say is the fastest/best way to insert metatags / scripts into my layout.
Basically I want to provide a client with a way to write their own metatags/text/scripts and insert them into the layout.

Right now my solution is make headtags.html, bodytags.html, bottomtags.html in the WRITEABLE.'tags' folder. The idea is that the client can edit these files in their admin panel and write stuff like google tag manager, googe analytics, or maybe they want to put a script tag for some kind of chat widget in the bottomtags.html (and it reads out right before the closing </body> tag in my layout file.

And I read them out with echo file_get_contents(WRITEPATH.'tags'.DIRECTORY_SEPARATOR.'headtags.html');

Well - the question is basically is file_get_contents suitable for this? Is html ok for this?? What about fopen with some .txt file?

Please don't suggest I use the database for this, because sometimes in my site I don't connect to the database, and I want this files to be completely editable, without touching the database. Also they can't be in PHP because I worry that a malicious person (like a curious employee or a teen who accidentally gets their hands on the clients admin panel) may wreak havoc on the rest of the files if its written in PHP.


Here is my layout.php file for clarification if needed:
PHP Code:
<!DOCTYPE html>
<
html lang="ru">
<
head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= $page->title ?? $cms->organization ?></title>
    <meta name="description" content="<?= $page->description ?? $page->title ?? $cms->organization ?? NULL?>">
    <meta name="keywords" content="<?= $page->keywords ?? $page->title ?? $cms->organization ?? NULL?>">
    <meta name="robots" content="<?= $cms->power == 'index, follow' 'noindex, nofollow'?>">
    <?= !empty($page->author) ? '<meta name="author" content="' $page->author '">' NULL;
    $canonical rtrim(current_url(), '/'); ?>
    <meta property="og:title" content="<?= $page->title ?? NULL?>" id="og_title">
    <meta property="og:site_name" content="<?= $cms->organization ?>">
    <meta property="og:description" content="<?= $page->description ?? $page->title ?? $cms->organization ?? NULL?>" id="og_description">
    <meta property="og:image" content="<?= $og_image ?? STATIC_FILES 'images/logo.svg' ?>"/>
    <meta property="og:type" content="website"/>
    <meta property="og:url" content="<?= $canonical ?>"/>
    <meta property="og:locale" content="ru"/>
    <?php
    
if (isset($metatags)) {
        foreach ($metatags as $tag) {
            echo $tag "\n";
        }
    }
    echo file_get_contents(WRITEPATH.'tags'.DIRECTORY_SEPARATOR.'headtags.html');
    ?>
</head>
<body>
<?= file_get_contents(WRITEPATH.'tags'.DIRECTORY_SEPARATOR.'afterheadtags.html'); ?>
<nav class="row bet max top">
    <div class="row center bet top__one">
        <a role="button" class="menu-js">☰</a>
        <a itemprop="url" href="/" id="logo">
            <div itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
                <img itemprop="contentUrl" src="<?= STATIC_FILES ?>images/logo.svg" width="100" height="48" alt="<?= $cms->organization ?>">
                <meta itemprop="representativeOfPage" content="true">
                <meta itemprop="thumbnail" content="/android-chrome-192x192.png">
            </div>
        </a>
        <div class="row b1">
            <span class="gry">Иваново</span>
            <a href="tel:<?= $cms->telephone ?>"><?= str_replace('-'' '$cms->telephone?></a>
            <span class="gry">пн-пт 8:00-22:00</span>
            <a href="#ask" class="blue js-scroll" role="button">Заказать звонок</a>
        </div>
    </div>
    <button class="row js-expand lefty">Разделы сайта/логин<span class="arrow"></span></button>
    <ul class="row center top__two">
        <li><a href="/page/o-kompanii">О компании</a></li>
        <li><a href="/page/dostavka-i-oplata">Доставка и оплата</a></li>
        <?= !empty($cms->articles) ? '<li><a href="/blog">Блог</a></li>' null?>
        <li><a href="/contacts">Контакты</a></li>
        <li><a href="/page/info">Информация</a></li>
        <li><a href="/login" class="blue username">Вход / Регистрация</a></li>
    </ul>
</nav>
<div class="row bet max top2">
    <div class="row center start top2__one lefty">
        <ul class="row center cats">
            <?= view_cell('\App\Controllers\Front::categoryList'null86400); ?>
            <?= !empty($cms->sales) ? '<li class="orange"><a href="/catalog/rasprodazha">Распродажа</a></li>' null?>
        </ul>
        <form class="row bet search" action="/poisk" method="get">
            <input type="submit">
            <input type="text" name="name" value="<?= $_GET['name'] ?? null ?>" placeholder="Поиск по товарам на сайте">
        </form>
    </div>
    <div class="row bet top2__two">
        <a rel="nofollow" href="/favorites"><span id="allSaved">0</span></a>
        <a rel="nofollow" href="/cart"><span id="allCart">0</span></a>
    </div>
</div>
<?php
echo view('front/' $view);
$notAllowed = ['order''cart''edit''login''register'];
if(isset(
$method) && !in_array($method$notAllowed)) {
    helper('form');
    echo form_open('/front/ask', ['class' => 'row max ask''id' => 'ask']);
    echo '<div class="row center">';
    echo '<div class="col start"><p>Остались вопросы?</p><p>Бесплатная консультация</p></div>';
    echo form_input('name'set_value('name'), ['placeholder' => 'Имя''class' => 'name-field']);
    echo form_input('askName'set_value('askName'), ['placeholder' => 'Имя']);
    echo form_input('askContact'set_value('askContact'), ['placeholder' => 'Телефон / E-mail']);
    echo form_input('askQuestion'set_value('askQuestion'), ['placeholder' => 'Ваш вопрос']);
    echo form_submit('askSubmit''Отправить'); ?>
    <label class="row start center cont" for="askacceptance"><input type="checkbox" name="askacceptance" placeholder="Cогласие на обработку п.д." checked>Даю согласие на обработку персональных данных в соответствием с&nbsp;<a class="blue" rel="nofollow" target="_blank" href="/page/politika-konfidencialnosti">Политикой конфиденциальности</a>&nbsp;(обязательное поле)</label>
    <?php echo '</div>';
    echo form_close();
}
?>
<footer class="row max">
    <a id="to-top" href="#logo" class="arrow js-scroll" role="button">Вверх</a>
    <div class="col start footer__info">
        <img itemprop="contentUrl" src="<?= STATIC_FILES ?>images/logo.svg" width="100" height="48" alt="<?= $cms->organization ?>">
        <p class="gry">Интернет магазин "<?= $cms->organization ?>"</p>
        <?php if (!empty($cms->addressLocality)) {
            echo '<p class="row center start" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><span class="footpics"></span>';
            echo '<span itemprop="addressLocality">' $cms->addressLocality '</span>';
            echo !empty($cms->streetAddress) ? ', <span itemprop="streetAddress">' $cms->streetAddress '</span> ' null;
            echo '</p>';
        }
        echo "<p class='row center start'><span class='footpics fp-telephone'></span>$cms->telephone</p>";
        echo "<p class='row center start'><span class='footpics fp-email'></span>$cms->email</p>";
        ?>
    </div>
    <div class="col start footer__sec">
        <p><b>Продукция</b></p>
        <ul class="col start">
            <?= view_cell('\App\Controllers\Front::categoryList'null86400); ?>
            <?= !empty($cms->sales) ? '<li><a class="orange" href="/catalog/rasprodazha">Распродажа</a></li>' null?>
        </ul>
    </div>
    <div class="col start footer__sec">
        <p><b>Покупателям</b></p>
        <ul class="col start gry">
            <li><a href="/page/o-kompanii">О компании</a></li>
            <li><a href="/page/dostavka-i-oplata">Доставка и оплата</a></li>
            <?= !empty($cms->articles) ? '<li><a href="/blog">Блог</a></li>' null?>
            <li><a href="/contacts">Контакты</a></li>
            <li><a href="/page/">Контакты</a></li>
            <li><a href="/page/politika-konfidencialnosti">Политика конфиденциальности</a></li>
        </ul>
    </div>
    <div class="col start footer__sec">
        <p><b><?= $cms->telephone ?></b></p>
        <p class="gry">пн-пт 8:00-22:00</p>
        <p class="row center gry signature">
            <?php if(current_url() === SITE_CONSTANT '/') {
                echo '<a class="row center" href="https://itart.pro">';
            } else { echo '<noindex class="row center">'; } ?>
            <img src="<?= STATIC_FILES ?>images/itartpro32x32.png" alt="itart.pro" width="32" height="32">
            <span class="col start">
                <span>&copy;2020<?=(date("Y") != 2020) ? ' - ' $current NULL?> itart.pro</span>
                <span>Создание сайтов качественно</span>
            </span>
            <?php if(current_url() === base_url()) {
                echo '</a>';
            } else { echo '</noindex>'; } ?>
        </p>
    </div>
</footer>
<?php if(isset($swipe)) {
    echo view('modules/swipe');
    $scripts $scripts ?? [];
    $scripts[] = '<script src="' STATIC_FILES 'assets/swipe/photoswipe.min.js"></script>';
    $scripts[] = '<script src="' STATIC_FILES 'assets/swipe/ui-altered.min.js"></script>';
    $scripts[] = '<script src="' STATIC_FILES 'js/init-photoswipe.js"></script>';
?>
<script type="application/ld+json">
    {
      "@context": "http://schema.org/",
      "@type": "Service",
      "serviceType": "<?= 'Архитектурные планы домов и проекты' ?>",
      "provider": {
        "@type": "Business",
        "name": "Планистрой",
        "telephone": "<?= $cms->telephone ?>",
        "email": "<?= $cms->email ?>",
        "image": "/android-chrome-192x192.png"
      }
    }

</script>
<?php
if (isset($scripts)) {
    foreach ($scripts as $script) {
        echo $script "\n";
    }
};
echo 
file_get_contents(WRITEPATH.'tags'.DIRECTORY_SEPARATOR.'bottomtags.html');
?>
</body>
</html> 
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#2

Try View Cells, https://codeigniter.com/user_guide/outgo...ight=cells.
Reply
#3

(07-26-2020, 07:12 PM)mlurie Wrote: Try View Cells, https://codeigniter.com/user_guide/outgo...ight=cells.

I think you're right. A view cell is much more performant than echo file_get_contents(....); because it is cacheable - now that I think about it. Thanks! I used view cells before I just didn't really think about it.

I can echo the contents in a public function and cache it in a view cell.
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB