admin管理员组文章数量:1122832
I've read all the similar threads on the Exchange and nothing is resonating.
I recently migrated this site from another dedicated server to a new dedicated server. This function, in functions.php, fires a session needed for a shopping cart, but has stopped working in this new environment. The version of PHP is the same: 7.4xx
The entire error is:
Cannot change session cookie parameters when headers already sent in line 118
PHP Warning: session_start(): Cannot start session when headers already sent in line 120
The code in question is:
function start_session() {
if (!session_id()) {
$time = 86400;
$session = 'DGPSESSION'; // <- line 118
session_set_cookie_params($time);
session_name();
session_start(); // <-- line 120
// Reset the expiration time upon page load
if (isset($_COOKIE[$session])) {
setcookie($session, $_COOKIE[$session], time() + $time, "/");
}
}
add_action('init', 'start_session', 1);
The InterWebs have not shed much light on this, other than something about the PHP or WordPress version (6.2.2) may come into play.
Browsers inspectors have not lent any clues, either.
Thoughts?
I've read all the similar threads on the Exchange and nothing is resonating.
I recently migrated this site from another dedicated server to a new dedicated server. This function, in functions.php, fires a session needed for a shopping cart, but has stopped working in this new environment. The version of PHP is the same: 7.4xx
The entire error is:
Cannot change session cookie parameters when headers already sent in line 118
PHP Warning: session_start(): Cannot start session when headers already sent in line 120
The code in question is:
function start_session() {
if (!session_id()) {
$time = 86400;
$session = 'DGPSESSION'; // <- line 118
session_set_cookie_params($time);
session_name();
session_start(); // <-- line 120
// Reset the expiration time upon page load
if (isset($_COOKIE[$session])) {
setcookie($session, $_COOKIE[$session], time() + $time, "/");
}
}
add_action('init', 'start_session', 1);
The InterWebs have not shed much light on this, other than something about the PHP or WordPress version (6.2.2) may come into play.
Browsers inspectors have not lent any clues, either.
Thoughts?
Share Improve this question asked Jun 28, 2023 at 12:03 breadwildbreadwild 3815 silver badges22 bronze badges 4 |1 Answer
Reset to default 0It turns out not all hosts treat sessions alike. I ended up scraping altogether the "session" code in functions.php previously referred to which was doing nothing.
Instead, I added session_start()
at the top of every template adding or retrieving a value from a name/value hash in $_SESSION. So,
<?php
session_start();
$_SESSION['order_id'] = $order_id;
. . .
$order_id = $_SESSION['order_id'];
本文标签: phpquotCannot start session when headers already sentquot when attempting session
版权声明:本文标题:php - "Cannot start session when headers already sent" when attempting session 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736291861a1928812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
?> <?php
somewhere – Tom J Nowell ♦ Commented Jun 28, 2023 at 13:40session_name
is being used but it's not being assigned to anything and no name is passed to it – Tom J Nowell ♦ Commented Jun 28, 2023 at 13:41