0

I have login.php and mainpage.php files and I want to access login.php's user's data from mainpage.php.

Should I use require or include? How to connect those files?

require login.php;
include login.php;
LihO
  • 41,190
  • 11
  • 99
  • 167
el_Pueblo_13
  • 41
  • 10

2 Answers2

6

Best would be to write object-oriented codes, i.e. mainpage.php would

require('login.php');

just to get definition of let's say class LoginForm and then in mainpage.php you would do:

$loginForm = new LoginForm();

Alternatively you could place your logic into functions and let mainpage.php call something like handleLogin() defined in login.php. In both of mentioned cases mainpage.php just needs to know the definition of function/class, therefore you should use require or require_once.

LihO
  • 41,190
  • 11
  • 99
  • 167
0

You can put all your data into $GLOBALS.

You can check great examples here How to declare a global variable in php?

Community
  • 1
  • 1
Randomius
  • 252
  • 1
  • 2
  • 12