0

As the question, I can do that with $_SESSION on the below code, but I would like to do that without using $_SESSION, any suggestion ? I am new for using oop in php.

PHP (page 1):

    include './class.php';
    include './next.php';

    if(isset($_POST['submit'])){
         $data = new myData();
         $data->setTest($_POST['test']);
    }

    $content = '<form method="post" action="next.php">
      <input type="text" name="test" id="test" value="test"/>
      <input type="submit" name="submit" value="Submit"/>
    </form>';

   if(!empty($next)){
       $content = $next;
   }

   echo $content;

class.php:

class myData{
     function __construct($test){
          ***some process***
          $_SESSION["test"] = $test;
     }
}

next.php

$next = $_SESSION["test"];
Alexis
  • 37
  • 8
  • There are a lot of answers available on this for this issue already - https://stackoverflow.com/questions/1179559/how-do-i-pass-data-between-pages-in-php. Keep in mind php is stateless. – mlunt Nov 28 '19 at 01:32
  • 1
    Possible duplicate of [How do I pass data between pages in PHP?](https://stackoverflow.com/questions/1179559/how-do-i-pass-data-between-pages-in-php) – Marco Merlini Nov 28 '19 at 03:23
  • The site's solution is using ```$_GET``` on the second page, but I need to pass many data not only a single data as my question. – Alexis Nov 28 '19 at 03:29
  • [`$_COOKIE`](https://www.php.net/manual/en/reserved.variables.cookies.php) – Ritesh Khandekar Nov 28 '19 at 04:25
  • ```$_COOKIE``` is not secure – Alexis Nov 28 '19 at 08:55

2 Answers2

1

You could store the variable in the class and then use a getter method to get that data back. file1.php

if(isset($_POST['submit'])){
     $data = new MyData($_POST['test']);
}


class MyData{
     private $data = '';

     function __construct($test){
          ***some process***
          $data = $test;
     }
     function getData() {
         return $this->data;
     }
}

file2.php

    include file1.php;
    echo $data->getData();
0

I declare a function and return back to page 1 on next.php

function showContent($content){
      if(!empty($content)){
            return $content;
      }
      return false;
}

Then on the class.php, I declare a getter as Mr.Karim Aljandali's answer. On the page 1 I add a line $next = showContent($data->getData());

Alexis
  • 37
  • 8