0

In my PHP application users are able to log in but new users are not able to register due to the following error after PHP 7 upgrade. The error is : PHP message: PHP Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in I am hosting my server on Cloudways. Could anyone help me with that. I am trying to find the solution for two days.

Functions.php code :

<?php 
session_start();
// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 1209600); 
// each client should remember their session id for EXACTLY 1 hour
session_set_cookie_params(1209600);

function logged_in () {
    if (isset($_COOKIE['usnm']) || !empty($_COOKIE['usnm']) && isset($_COOKIE['usid']) || !empty($_COOKIE['usid'])) {
        $_SESSION['user_id'] = $_COOKIE['usid'];
        $_SESSION['username'] = $_COOKIE['usnm'];
        return true;
         setcookie("usnm",$mobile, time() + (10 * 14 * 24 * 60 * 60));
         setcookie("usid",$_SESSION['user_id'], time() + (10 * 14 * 24 * 60 * 60));
          setcookie("usnm",$user['mobile'], time() + (10 * 14 * 24 * 60 * 60));
         setcookie("usid",$user['userid'], time() + (10 * 14 * 24 * 60 * 60));

    } else {
        return false;
    }
}

function redirect_to ($url) {
    @header("Location: {$url}");
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
NJR
  • 37
  • 2
  • 8
  • 1
    So SWAP line 1 and line 2 .... and _BOB's Yer proverbial male relative on your fathers side_ – RiggsFolly Jan 08 '20 at 13:57
  • 1
    And probably place line 3 above the `session start()` as well – RiggsFolly Jan 08 '20 at 13:58
  • @RiggsFolly I did as you said but its not working. it shows the error' PHP message: PHP Notice: Undefined index: user_id ' and automatically redirect to index page – NJR Jan 08 '20 at 14:02
  • THats a completely different error – RiggsFolly Jan 08 '20 at 14:04
  • I dont see how an upgrade to PHP7 caused the login function to fail, that can never have worked. When you run a `return` control is imediately returned to the calling line. So the lines of code AFTER a return will never get run – RiggsFolly Jan 08 '20 at 14:10
  • You also seem to be duplicating data in the cookies, if I am following the code correctly – RiggsFolly Jan 08 '20 at 14:12
  • You also have a SCOPE issue in `logged_in()`!! `$mobile` and `$user` do not exists in the scope of the function !!! – RiggsFolly Jan 08 '20 at 14:13

1 Answers1

0

Swap line 2 and 3 to above the session_start()

<?php 
// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 1209600); 
// each client should remember their session id for EXACTLY 1 hour
session_set_cookie_params(1209600);

session_start();
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149