Cookie problem

Asked

Viewed 75 times

0

I would like my script to verify the existence of the informed Cookie and if it did not exist enter,.

My code is this:

$protocol    = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === true) ? 'https' : 'http';
$host        = $_SERVER['HTTP_HOST'];
$uri        = $_SERVER['REQUEST_URI'];
$current_uri = $protocol . '://' . $host . $uri;

$current_uri = $protocol . '://' . $host . $uri;
$current_url = md5($current_uri); // SOLUÇÃO

if(isset($_COOKIE[$current_url])) { 
    echo "O usuário já tem o cokkie.";
} else {
    setcookie($current_url, date("d-m-Y H:i:s"), time()+3600); // 3600 => 1 hour
    echo "Cookie inserido com sucesso";
}

But it hasn’t worked! The cookie is actually entered. The problem is that it is always renewed and the message that the Cookie already exists is not passed. Someone could help me?

  • In fact, I apologize, I had not performed the test and put the current_url as "testing". Only it actually represents the URL of the browser. I updated the code, and you can see that it’s not working. Could you please review?

  • Just out of curiosity, what’s the point of putting a full URL with protocol and everything instead of a simple name? Cookies are set by default in the current domain anyway, this information is redundant and still gets confused if you change protocol (apart from the special characters to mess up the parse depending on how you use it).

  • You are not accepting the value as a URL

  • @Bacco The reason why you use URL as a value to save the cookie is to have control of users visiting every page they visit on the website. That is, I will count the user visits per page and know how many pages he visited. When the cookie is entered, this data is also entered in the database.

  • @Fydellys would be better if you do everything on DB, and use a user identifier only. There instead of you storing the information in the client you keep only on your side (every client access you store the URL in the DB, with the cookie ID) - will make your life a lot easier. And your application will become more portable too, in addition to lowering the volume of traffic.

  • If the user has 200 cookies from your domain, each request to your server will transmit the 200 every time, to all pages, images, scripts and everything else, even if they are not used.

  • Got @Bacco, actually, in my script, I already store user data in my DB. The problem is that it has not yet come into mind how to make the visit of the page that user visited, not come count again if he visits within an hour. Do you have any idea? Taking into account that the script that will record such visits would be in require_once in the header to be able to pick up all the pages he visits. Currently already caught, IP, URL, City, State, Time.

  • For this reason I wanted to use the URL to pick as page identification. Understand? And also to save in DB.

  • You could just store "userid", 278 in the cookie, being that the 278 would be the ID of that user in your DB (if not, you create a new one and store the ID in the cookie) - you can store something like userid, 278 - usertoken, 323793 and the Token is an Random any stored user’s DB, thus prevents someone from editing the cookie on the client’s side and impersone another user.

  • @Bacco You opened my mind in a PALLIATIVE way. In fact my application has no user (except for website administration). The way to identify the page the visitor visited. I generated an MD5 token based on the URL and then I was able to pass as a value to the Cookie without losing URL identification. I am grateful for the attention. I will improve in the future this application following your advice, but served as a palliative. The Solution is in the question.

  • If you don’t want to lose the information, you can use a Base64 - in this case, you have how to reverse the format.

  • 1

    Another way: Make A "Visited" cookie and put a JSON in the value. Then in this JSON you put all the Urls. (retrieves the JSON and creates a key with URL and Data. If it is repeated, it will be updated, or created) - Still palliative, pq continues the problem of unnecessary data traffic, but makes it easier to organise.

  • Got it! I’m grateful for the clarification. My application is with graphics of visits, dates, visits per pages and got very good, where it is being filtered only human visit, bots are rejected in the script.

Show 8 more comments

1 answer

0

Name the pages

$nome_pagina = "pagina1";

setcookie($nome_pagina, $nome_pagina, time() + 30, "/");

if(!isset($_COOKIE[$nome_pagina])) {
    echo "Cookie inserido com sucesso";
} else {
    echo "O usuário já tem o cokkie.";
}

If you don’t want to keep putting names on pages, use pathinfo - Returns information about a file path

$path_parts = pathinfo( __FILE__ );
$nome_pagina = $path_parts['filename'];

setcookie($nome_pagina, $nome_pagina, time() + 30, "/");

if(!isset($_COOKIE[$nome_pagina])) {
    echo "Cookie inserido com sucesso";
} else {
    echo "O usuário já tem o cokkie.";
}

Browser other questions tagged

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