0

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?

K.G
  • 11
  • 3

2 Answers2

0

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

  • I changed the path where the cookie is set to only include the folder which I'm currently working on, so I did /app for that. Also in the file manager of cpanel I opened .htaccess in public_html and added the lines of code you provided, but nothing seemed to solve the problem even after I cleared my browser's cache. Is there anywhere else I was supposed to add the .htaccess code? Thanks – K.G Aug 26 '23 at 12:12
  • Check for other code snippets, plugins or scripts that may be interacting with cookies or cache settings on your site or app. If possible, provide us with the configuration of your .htaccess and apache configuration in the sites-enable folder that ends with .conf for your site or application. to be able to better analyze this issue – Ramon Soarez Aug 26 '23 at 13:30
  • AddHandler application/x-httpd-ea-php82___lsphp .php .php8 .phtml php_flag display_errors Off php_value max_execution_time 60 php_value max_input_time 60 php_value max_input_vars 1000 php_value memory_limit 512M php_value post_max_size 516M php_value session.gc_maxlifetime 99999 php_value session.save_path "/var/cpanel/php/sessions/ea-php82" php_value upload_max_filesize 512M php_flag zlib.output_compression Off Here is the .htaccess code, i cant find a .conf file anywhere. – K.G Aug 26 '23 at 14:05
  • The .htaccess code above is without the part that you included, but I have added it too and it doesn't change anything. I looked around for a .conf file, something like httpd.conf which XAMPP has but I cant find it on my websites cpanel. – K.G Aug 26 '23 at 14:07
  • If you're using Cpanel, edit your question to say so. The php settings on a Cpanel server are quite different than a server with just apache installed. Please, when asking a question, describe all the details of the problem so that we can analyze it better and not deny your question. I'll be editing the answer as soon as I provide more details about which cpanel you use, version and php you use in it. – Ramon Soarez Aug 26 '23 at 21:03
  • The cPanel version is 110.0 (build 10) with the "jupiter" theme (if thats relevant) and using PHP version "PHP 8.2 (ea-php82)". I'll also be updating the question to include these information, thanks a lot – K.G Aug 26 '23 at 21:20
0

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!

K.G
  • 11
  • 3