Share Variables between PHP files

Asked

Viewed 203 times

0

Good morning, I would like to share the data entered in an input between 2 files. In the Form action I use the validation file, to check the login and password, but I wanted the data typed in these fields also to be for another file, so I show the name of the user, according to his login, for that I would need to pass the variable $login to the second file:

$login = $_POST['login'];

in the two files, in 1° for verification and in 2° for consultation in DB. Thank you already.

  • Session - http://php.net/manual/en/intro.session.php or Cookies - http://php.net/manual/en/features.cookies.php

  • in the form action file I put $_SESSION['login'] = $login; e $_SESSION['Cpf'] = $Cpf; and I made q this file that receives these variables per post redirect the browser to the 2° file in I need these variables too, and to test I gave an echo $_SESSION['login']; but this returns the error "Undefined variable: _SESSION "

  • I also tried $_SESSION[$Cpf] = $_POST['Cpf']; and echo $_SESSION[$Cpf];

  • Using Cookies it was possible to solve the problem as follows: 1° File: setcookie("Cpf", $Cpf); setcookie("login", $login); // 2° file: $Cpf = $_COOKIE["Cpf"]; $login = $_COOKIE["login"]; .

1 answer

1


On all pages you will need to declare / use a SESSION, you must sign in at the beginning of such a page, for this you need to use the following command: session_start(), once logged in, you can assign values such as $_SESSION["login"] = $login.

Now on the second page just perform the login procedure again with session_start() and then call the value you want echo $_SESSION["login"].

You can find more information about sessions Here

  • 1

    I had already managed to solve the problem with cookies, but I will use Sesssions to avoid the unnecessary accumulation of cookies that can generate some inconvenience. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.