skip to Main Content

I’ve got a code that sets a cookie for every new user that gets saved to a variable. When I work on the website offline using XAMPP the code works just fine and the cookie doesn’t change, but when I upload it to my website the cookie changes with each refresh of the page. I’m using cPanel version 110.0 (build 10) and PHP version "PHP 8.2 (ea-php82)"

<?php
if (!isset($_COOKIE['clientCookie'])) {
    $uniqueId = uniqid();

    $cookieName = "clientCookie";
    $cookieValue = $uniqueId;
    $cookieExpire = time() + (60 * 60 * 24 * 365 * 10); // Cookie will expire in 10 years

    setcookie($cookieName, $cookieValue, $cookieExpire, '/');

    $clientId = $uniqueId;
} else {
    $clientId = $_COOKIE['clientCookie'];
}
?>

I’m not sure what’s wrong here, could it be because of the way that the server that the website is on is set up?

2

Answers


  1. Chosen as BEST ANSWER

    I found the problem that was causing the issue with the cookies. I turned on the error display using MultiPHP INI Editor in my cPanel with display_errors = on and it showed me "Warning: Cannot modify header information - headers already sent". I had to add the code for the cookie before any HTML was displayed and the cookie code worked just fine.

    Read here for more information.

    How to fix "Headers already sent" error in PHP

    Thanks to anyone who helped out!


  2. The code you provided is correct, the case could be domain or cache route. First validate that the path where you are validating the cookie is correct, the last parameter of setcookie(), which is ‘/’, indicates that the cookie must be valid for the entire domain. read here: https://www.php.net/manual/pt_BR/function.setcookie.php

    clear your browser’s cache before making these changes to validate that it is not cached.

    Also check the settings set in .htaccess on your apache server for cookies. In mod_headers.c disable caching for dynamic pages. Example:

    <IfModule mod_headers.c>
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires 0
    </IfModule>
    

    In cPanel, you can check and adjust some settings that may affect how cookies work.

    Go to cPanel or whm panel and look in the side search bar for PHP Settings:

    In cPanel, look for a section called "Software" or "Software and Services".
    Look for an option called "Select PHP Version", "MultiPHP Manager" or something similar. This will allow you to configure PHP options.

    Within the PHP options, you should find settings related to cookies and sessions. Look for options like "session.cookie_domain", "session.cookie_secure" and others that may affect the behavior of cookies.

    If your website is using HTTPS (SSL), make sure SSL is properly enabled in cPanel. There is often a section called "Security" or "SSL/TLS" that allows you to configure SSL.

    In addition to PHP settings, some hosts may offer options to clear the server’s cache. This can be useful to ensure that you are working with the latest version of your code.

    If you cannot find the necessary settings or if you have specific questions about your server settings in cPanel, check the cpanel forum which is not updated with more details of the tool or its official documentation at the links: Forum Doc1 Doc2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search