Moving through pages using PHP

ok, this is may problem:
suppose I reach page ‘B’ clicking a button in page ‘A’, then reach page ‘C’ starting from page ‘B’ etc..
because I know path, in page B I can put <a href="/pathBackToPageA">Go Back </a> to go back form B to A and so on and after I could return page A going back from C to B and the to A.
Suppose now you want reach page B starting from page D obviously you would return to D going back form C to B and from B to D, but in page B the ‘Go Back’ button is pointing A . So wich is the way, if you want have only a button to go back the right page ?

I was trying this class :

class ReturnHereVM
{
    protected string $base='/';
    protected string $backWardPage='';
    protected string $forWardPage='';
    protected string $curPage='';
    protected string $curController='';

    public function __construct(
        ?string $curController='',
    ){
        $this->curController=$curController;
    }

    public function getBackWardPage()
    { 
        return $this->backWardPage;
    }
    public function getCurPage(){
        if(isset($_SESSION['returnHere']['curPage'])){
            $this->curPage= $_SESSION['returnHere']['curPage'];
        }
        return $this->curPage;
    }
    public function getForWardPage()
    {
        return $this->forWardPage;
    }
    public function getCurController()
    {
        return $this->curController;
    }
    public function setCurController($val){
        $this->curController=$val;
    }
    public function setBackWard($val){
        $_SESSION['returnHere']['backWard'] = $val;
        $this->backWardPage=$val;
    }
    public function setCurPage($val){
        $_SESSION['returnHere']['curPage']=$val;
        $this->curPage=$val;
    }
    public function setForWard($val)
    {
        $_SESSION['returnHere']['forWard'] = $val;
        $this->forWardPage=$val;
    }
    public function setBacPages(){

    }
    public function setPages(string $start,string $val,string $fval){
        if($start=='i'){
            unset($_SESSION['returnHere']);
            $this->setBackWard($this->base);
        }else{
            $this->setBackWard($start);
        }
        $this->setCurPage($val);
        $this->setForWard($fval);

}

but it goes fine only if i go from page A to B and return directly to A from B.
Steps I performed are:
($this->contrName is controller Name of the page;
$this->pagination is the object class of the paginator;)

in page A

        //because page A is the first, previous is Index = 'i'
        $thisPage= '/' . $this->contrName . '/list/pag&' . $this->pagination->getPage();
        $nextPage='/'.$this->contrName.'/detail/';//this is to reach page B
        $this->returnHere->setPages('i',$thisPage,$nextPage);

in page B

        $prevPage=  $this->returnHere->getCurPage();
        $thisPage= '/' . $this->contrName . '/detail/' . $id;
        $nextPage= '/' . $this->contrName . '/edit/'.$id;//this is to reach page C
        $this->returnHere->setPages($prevPage, $thisPage, $nextPage);

in page C

        $prevPage =  $this->returnHere->getCurPage();
        $thisPage = '/' . $this->contrName . '/edit/' . $id;
        $nextPage = '';//this is last page of the series
        $this->returnHere->setPages($prevPage, $thisPage, $nextPage);

but if I should return from C to B I never could return A!
because this isn’t right way I didn’t try from page D