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
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");
?>