1

First of all:

  • I develop a website in HTML & PHP.
  • I do my local test under Chrome, using Laragon 4.0.16 (Apache server httpd-2.4.36-win64-VC15 with PHP 7.2.19).
  • The file are written under Notepad++, as UTF-8

My aim is to save a variable shared between different pages of a website. To do this, i use setcookie. It is OK on local host but not OK on the host (ionos.fr).

I did a simple test for checking the error.

under local host

1/ On index.php, i display $_COOKIE (with only PHPSESSID the first time).

2/ When clicking on buttons 'SetChapter', i call a php (call_cookie_xx.php) to set the cookie to value 16 or 33 (depending of the button pressed). This php does only the setcookie and then open a sub page sub_index.php.

3/ On index_sub.php, I read the $_COOKIE (set by call_cookie_xx) with the call to echo <script>XXX</script>. Flush the cookie (by flush_cookie.php) or go back to main page.

4/ I have $_COOKIE[Chapter] defined when coming back from sub_index.php to index.php => it is OK - for instance:

Array ( [PHPSESSID] => pcv4f5d2fvm58ruvl9ddn59l61 [chapter] => 33 )

under remote host

both setcookie and page switching (with header...) do no actions. Even no PHPSESSID

Comparison local_host/remote

After many research on the web , i don't find the reason.

Does anyone have any ideas?

  • I checked the file encoding
  • setcookie is the first instruction of a file call_cookie_xx

Here is my sample code

index.php:

<?php 
print_r($_COOKIE);
?>

<!DOCTYPE html>
<html lang="">

<head>
  <title>test</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>

<?php
function popup_echo($text)
{
    $msg = "\"" . $text . "\"";
    $echo_cmd = '<script type="text/javascript">alert(' .$msg . ');';
    $echo_cmd .= '</script>';
    echo $echo_cmd;
}
function get_chapter()
{
    if(!isset($_COOKIE["chapter"])) 
    {
        popup_echo("_COOKIE[chapter] no set");
        return 0;
    } 
    else 
    {   
        popup_echo("_COOKIE[chapter]=".strval($_COOKIE["chapter"]));
        return intval($_COOKIE["chapter"]);
    }
}


if (isset($_POST['GetCookie']))
{
    get_chapter();
}
elseif (isset($_POST['go_index1']))
{
    popup_echo("_POST[go_index1]");
    header("Location: ./call_cookie_16.php");   //will set $_COOKIE["chapter"]=16 && call sub_index.php
}
elseif (isset($_POST['go_index2']))
{
    popup_echo("_POST[go_index2]");
    header("Location: ./call_cookie_33.php");   //will set $_COOKIE["chapter"]=33 && call sub_index.php
}
?>
<body>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group">
            <input type="submit" class="btn btn-primary" name="GetCookie" value="GetCookie[Chapter]"> <br>
            <input type="submit" class="btn btn-primary" name="go_index1" value="Set Chapter to 16 and go to subpage">
            <input type="submit" class="btn btn-primary" name="go_index2" value="Set Chapter to 33 and go to subpage">
        </div>
    </form>
</body>

</html>

sub_index.php

<?php 
print_r($_COOKIE);
?>

<!DOCTYPE html>
<html lang="">

<head>
  <title>test</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>

<?php
function popup_echo($text)
{
    $msg = "\"" . $text . "\"";
    $echo_cmd = '<script type="text/javascript">alert(' .$msg . ');';
    $echo_cmd .= '</script>';
    echo $echo_cmd;
}
function get_chapter()
{
    if(!isset($_COOKIE["chapter"])) 
    {
        popup_echo("_COOKIE[chapter] no set");
        return 0;
    } 
    else 
    {   
        popup_echo("_COOKIE[chapter]=".strval($_COOKIE["chapter"]));
        return intval($_COOKIE["chapter"]);
    }
}


if (isset($_POST['GetCookie']))
{
    get_chapter();
}
elseif (isset($_POST['FlushCookie']))
{
    header("Location: ./flush_cookie.php");
}
elseif (isset($_POST['go_index']))
{
    popup_echo("go to index");
    header("Location: ./index.php");
}
?>
<body>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group">
            <input type="submit" class="btn btn-primary" name="GetCookie" value="GetCookie[Chapter]"> <br>
            <input type="submit" class="btn btn-primary" name="FlushCookie" value="FlushCookie[Chapter]"> <br>
            <input type="submit" class="btn btn-primary" name="go_index" value="Go back to index.php">
        </div>
    </form>
</body>

</html>

On local_host, i use null instead of "cma-accordions.com"

call_cookie_16.php

<?php
$ret = setcookie('chapter', "16", time() + 365*24*3600, '/', "cma-accordions.com", false, true);
header("Location: ./sub_index.php");
?>

call_cookie_33.php

<?php
$ret = setcookie('chapter', "33", time() + 365*24*3600, '/', "cma-accordions.com", false, true);
header("Location: ./sub_index.php");
?>

flush_cookie.php

<?php
$ret = setcookie('chapter', "0", time() - 360, '/', "cma-accordions.com", false, true);
header("Location: ./sub_index.php");
?>
helvete
  • 2,455
  • 13
  • 33
  • 37
  • 1
    Your calls to `header()` in the middle of html markup are definitely causing `Headers already sent...` errors, and cookies can be affected by those too. At the top of your script always when testing and debugging `error_reporting(E_ALL); ini_set('display_errors', 1);` You will need to restructure your code such that you send no output of any kind before calling `header()`. https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Michael Berkowski Mar 04 '21 at 18:51
  • Thank you very much Michael! I understand my mistakes. – Mickael Biardeau Mar 04 '21 at 21:01

2 Answers2

0

SOLVED! Thank you very much. It solves 20 hours of tries and internet search!!

According to your advise, i modified the code to have:

  • all php code before the first HTML mark
  • remove all echo and popup before the call to header in the branches that change the page!

index.php (same on sub_index.php)

<?php 
error_reporting(E_ALL); ini_set('display_errors', 1);

if (isset($_POST['GetCookie']))
{
    get_chapter();
}
elseif (isset($_POST['go_index1']))
{
    header("Location: ./call_cookie_16.php");   //will set $_COOKIE["chapter"]=16 && call sub_index.php
}
elseif (isset($_POST['go_index2']))
{
    header("Location: ./call_cookie_33.php");   //will set $_COOKIE["chapter"]=33 && call sub_index.php
}

function popup_echo($text)
{
    $msg = "\"" . $text . "\"";
    $echo_cmd = '<script type="text/javascript">alert(' .$msg . ');';
    $echo_cmd .= '</script>';
    echo $echo_cmd;
}
function get_chapter()
{
    if(!isset($_COOKIE["chapter"])) 
    {
    popup_echo("_COOKIE[chapter] no set");
        return 0;
    } 
    else 
    {   
    popup_echo("_COOKIE[chapter]=".strval($_COOKIE["chapter"]));
        return intval($_COOKIE["chapter"]);
    }
}
?>

<!DOCTYPE html>
...
0

In addition,

  • Take care of extra text or space before <?php and after ?>
  • Remove trailing space
  • Clear the cookie manually in your browser during the test, and try to see them with embedded inspection (like Firefox / right-click / Inspect element / Storage / Cookies)