-1

I have this SessionService.php file that handles session_start and session_regenerate_id functions.

I make use of an abstract class that generates a new session ID and stores it in a variable called $new_session_id. Then I return this variable as an argument of an abstract function called returnNewSessionID(), contained in the class SessionRegenerateID. This abstract function is designed to return the variable $new_session_id.

That's where the problem is. Instead of returning a string (the new session ID we generated), it returns NULL.

At createNewID(), you see I am assigning the newly generated code to a session variable called $_SESSION['new_session_id']. By echoing this session variable, I get the expected value from it. At the same time, echoing the returned value from the createNewID() function gives me NULL.

The ID is being generated. The problem most likely lies in the way this variable $new_session_id is being used inside these functions. I need to figure out why it is not being returned by the abstract function.

SessionService.php

interface SessionRegenerateInterface {

        public function createNewID();
        (... other unrelated functions ...)

}

abstract class AbstractSessionRegenerate implements SessionRegenerateInterface {

        public function createNewID() {
            $new_session_id = session_create_id();
            $_SESSION['new_session_id'] = $new_session_id;
            $this->returnNewSessionID($new_session_id);
        }

        abstract protected function returnNewSessionID($newID);

}

class SessionRegenerateID extends AbstractSessionRegenerate {

        protected function returnNewSessionID($newID) {
            return $newID;
        }

}

SessionController.php

$RegenerateSession = new SessionRegenerateID;
$newID = $RegenerateSession->createNewID();
var_dump($newID);    // outputs NULL
var_dump($_SESSION['new_session_id']);    // outputs the generated session ID

I have an InputValidation class which is logically identical to this one, except for the type of value it returns: TRUE or FALSE. It works. But now that I am passing a variable with a string value, it returns NULL. I appreciate help to understand why.

crimson_king
  • 269
  • 3
  • 12
  • `AbstractSessionRegenerate::createNewID` doesn't return anything. Adding `return` before `$this->returnNewSessionID($new_session_id);` should help :) – Jeto Apr 14 '19 at 22:08
  • Thank you. It works. Such a simple thing :) So it seems the return is not needed when the value we are returning is a boolean. As I said at the end of the question, I have another abstract class that returns a bool value, and it works without return – crimson_king Apr 14 '19 at 22:14

1 Answers1

0

Thanks to Jeto, I solved this by inserting a 'return' before $this->returnNewSessionID($new_session_id);

        public function createNewID() {
            $new_session_id = session_create_id();
            $_SESSION['new_session_id'] = $new_session_id;
            return $this->returnNewSessionID($new_session_id);
        }
crimson_king
  • 269
  • 3
  • 12
  • 2
    The fact that it is a boolean or not doesn't matter. If you don't use `return`, your function won't return anything (or, more accurately, it will always return `null`). – Jeto Apr 14 '19 at 22:38
  • I am pretty sure the boolean value is returned without using `return`. That's how I do InputValidation, and I echoed this to confirm. There is even an example where the value being returned is not even a boolean: [See the render() function inside the abstract class](https://stackoverflow.com/a/1343881/9811172), so perhaps we're both missing something...? – crimson_king Apr 14 '19 at 22:49
  • 1
    @Doug if you want your function to return something, you *must* use the `return` keyword. Otherwise, it will return `NULL`: https://3v4l.org/5rUU1 – rickdenhaan Apr 14 '19 at 22:51
  • @Doug The `render()` function in that example doesn't return anything, it writes something into the output using `echo`. Not the same thing. Note that the value isn't retrieved into any variable. – Jeto Apr 14 '19 at 23:01
  • 1
    I ran var_dump() on my code again without 'return', and the output was NULL. I swear it did return bool(true) or bool(false) with the same code before. Maybe it's opcache's slow revalidate frequency. I am really surprised it didn't work this time. Thank you for the information, I edited the answer. – crimson_king Apr 14 '19 at 23:06