Save div to a cookie

Asked

Viewed 185 times

1

I’m starting to mess with cookies, so I don’t know much. I was hoping he’d save a certain <div> but I don’t know how to do it.

Like I’m doing:

check if 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 don’t have to make another query in the database. Can anyone help me?

Or if there’s a way to save the entire page to that cookie.

  • Usually the browser itself takes care of this, keeping some data cached and updating sometimes, but if you want to save the content in a cookie even, I suggest saving only the essential in json format (with json_encode) and, when using, transforms this json into an array or object (json_decode) to use. If you want to save all the html (not recommend), create a string and add the elements in this string instead of doing echo

  • 1

    Boy, what a fright! By the title it looks like you want to save the element div inside the Cookie!

  • Oops, but hold on. You’re talking about saving the whole page. Could you explain what you intend to do with it? I’m not talking about what you want to do with the code, but the purpose of this for the end user. It makes no sense to want to put an internal page in a div.

  • @Guilhermecostamilam how do I use the cache? , Wallacemaxters n want to save the entire page in a div want the user to enter once that content is saved and next time load faster n knew q had the cache for it

  • The browser takes care of the cache, no need to enable it (but it can be disabled in dev tools). If you want to save certain content then I suggest using localStorage for this

  • @Guilhermecostamilam I’m new in this area so could you explain to me how you use this

Show 2 more comments

1 answer

4


Your intention seems to me to use a cache, to avoid database queries.

You need to think about whether this is feasible first. Is the data you are searching constantly changed? 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? ever thought that "saving the whole page" or a "div" might be impossible or unviable?

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

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

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

A small example below to see how the header usage might look Last-Modified (I have used in some cases and 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 intend to give an answer in order to make for you, but I want you to understand what, for your case, cookie does not solve.

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

Here are some answers about cache, session and the like:

  • What do you think of localStorage? Among the three (cookie, http and Storage) which one you think best?

  • 1

    @Guilhermecostamilam cookie and Storage, as I said in the reply, is in the browser. anyone can open the console and change the content. This would leave the site vulnerable to XSS (the guy can write malicious javascript and put it in the place of "div" content). I suggest doing these things by the server... i.e.: Session or Header

  • @Guilhermecostamilam as Wallace said, this opens up the possibility of attacks, not that someone can not do manually in localStorage by F12, and of course you can handle the content, but it is complicated, besides that if the intention is cache it is better to use the interface you already have, try to solve on the basis of "front-end" technologies something like this is having to write a lot, can work for a hybrid app, and may even be a good idea in this case, but when it comes to web pages is too complicated

  • When I say Storage, I imagine something saved on the client side and that does not return to the server, for example it is saved the necessary data (urls, texts, the date when the content was saved), when a certain time passes (sla 1 day), then the client asks for the content again, without sending anything that was saved in Storage, just a common request, which would be equal to using http

  • @Guilhermecostamilam then, young man... when you use the header (which is what I answered "http") you are making the browser pick up the information that it saved before on disk, through the information passed by Last-Modified.

  • @Wallacemaxters thank you very much did not know this limitation and nor this existing vulnerability, I will explain my case and if possible I would like to talk to me if it is feasible to use cache because I am new in the subject.

  • @Wallacemaxters o So and: I have a page called conteudo.php and in it I use $_GET that takes the url ID conteudo.php?id=1 I’m hoping that if the person has already accessed that page of that id he will save and the next time he opens the page he will load faster using the information already saved. for this it is recommended to use cache?

  • @Cyberhacker depends. What comes from the "id" can be changed? Or is it something you only register once? Anyway, even if the answer is "no", do you save the change date in the bank? If yes, it is your salvation.

  • @Cyberhacker summarizing: If you have a modification date saved in the bank, just change the snippet of my code where you have it DateTime and date the bank.

  • @Wallacemaxters the content of this ID and saved only 1 time and n and changed more

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.