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
  • 2 A HTTP request starts with HTTP headers, followed by the body/reponse, which usually contains the HTML. The moment anything is output even if it's an empty space PHP will send HTTP headers telling the browser to expect content and the opportunity to send new HTTP headers ends. Any attempt to send more HTTP headers once the HTTP body has started will fail with this error. Also note that PHP sessions are turned off on a lot of WP hosts, have security issues, and rely on a cookie clientside so aren't a workaround for cookie laws either. The cause could be as simple as a ?> <?php somewhere – Tom J Nowell Commented Jun 28, 2023 at 13:40
  • 2 are there any associated notices/warnings before those 2? I see session_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
  • Looks like there is a 500 error for one of the templates being called that may or may not be related. Exploring that now. In other news, my host, Pair Networks, has no prohibition on sessions. – breadwild Commented Jun 29, 2023 at 14:32
  • IIRC, you can't set cookies after any output; cookies are part of the HTTP header. So, your code needs to happen 'earlier' in the process. Perhaps changing the priority of the add_action might help. – Rick Hellewell Commented Jun 29, 2023 at 19:16
Add a comment  | 

1 Answer 1

Reset to default 0

It 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