Save div into a cookie

1

I'm starting to mess with cookies, so I do not know much. I was wanting him to save a certain <div> but I do not know how to do it.

How do I do:

Verify that that cookie exists:

$nomecoook = $_GET['cont'];
if(isset($_COOKIE[$nomecoook])){

}else{
    $sql="SELECT img FROM fotos WHERE tkm='$tkm'";
    $exe= mysqli_query($link,$sql);
    $numrow=mysqli_num_rows($exe);
    setcookie($nomecoook,'1',(time() + (24 * 3600)));
}

the div I want to save:

<div class="carousel-inner">
     <?php
        $i=0;
        while($row=MYSQLI_FETCH_ARRAY($exe)){
           $i++;
           $img=$row['img'];
           if($i==1){
      ?>
           <div class="item active">
               <img src="../_img/<?php echo e($img) ?>" alt="First Slide">
           </div>
      <?php
           }else{
      ?>
           <div class="item">
               <img src="../_img/<?php echo e($img) ?>" alt="Second Slide">
           </div>
      <?php
           }
      }
      ?>
</div>

I want to save the result of this div so I do not have to do another query in the database. Can anyone help me?

Or if you have a way to save the entire page in that cookie.

    
asked by anonymous 06.08.2018 / 20:32

1 answer

4

Your intention seems to me to be to use a cache, to avoid querying the database.

You need to think hard if this is feasible, first. Is the data you are querying constantly changing? Or never?

If the answer is "never", then we can move on to the next paragraph.

Did you know that Cookie has a size limit ? Have you ever thought that "saving the entire page" or a "div" may be impossible or impractical?

Another thing: You know that the cookie, other than the session, could have the modified values directly in the browser.

If you save the DIV in Cookie and use it to write content saved on it by PHP, for example, you knew this could leave your site vulnerable to XSS ?

In your case, it seems to me a case for using Session or even using a Header to determine a response based on a cache or not.

A small example below to see how the Last-Modified header could be used (I have used it in some cases and it works very well):

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {

    if (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) < time()) {
        header('HTTP/1.1 304 Not Modified');
        // não executará a consulta, mas dará a última resposta, que foi salva no cache do navegador
        exit;
    }
}



header(sprintf("Last-Modified: %s", (new DateTime())->format(DateTime::COOKIE)));

// faça a sua consulta aqui

The above code is very simple and you can adapt it very well to your need. I do not want to give you an answer, but I want you to understand what, in your case, cookie does not solve.

Important information : Some browsers ignore Last-Modified when used in localhost . So for testing, maybe you could have difficulties if you use it. I think I solved this by creating a dummy domain in the hosts file.

See some answers on cache, session, and the like:

06.08.2018 / 21:07