How to make a global variable in PHP?

I have a login, and if the credentials are correct it opens another PHP file and wanted the name of the person associated with the credentials used in the login to appear on a label. All information is stored in a database. How do I access the values saved in the first PHP file?

The login code is:

$email = mysqli_real_escape_string($dbconn, $_POST['login']);
    $password = mysqli_real_escape_string($dbconn, $_POST['senha']);

    if (!empty($email) && !empty($password))
    {

        $query = "SELECT email, password, id_paciente FROM Paciente WHERE email = '$email' AND password = '$password'";
        $data = mysqli_query($dbconn, $query);
        $result = mysqli_num_rows($data);

    if ($result == 1) 
    {
                $row = mysqli_fetch_array($data);
                 $_SESSION['id_paciente'] = $row['id_paciente'];
                 $_SESSION['nome'] = $row['nome'];
                header("location: paciente_marcar_consulta_online.php");
        } 

And wanted to pass the id_patient to the file " patiente_marcar_consulta_online.php " in which it would then present the name of this patient ID. Can you help me?

Author: Isabel Sousa, 2017-07-01

1 answers

You can use $_SESSION to save various cookies!

At the beginning of the file, right at the top, enter this code:

<?php session_start(); ?>

This will allow you to save variables in $_SESSION. Then add this to your code:

if (!empty($email) && !empty($password))
    {

        $query = "SELECT email, password, id_paciente FROM Paciente WHERE email = '$email' AND password = '$password'";
        $data = mysqli_query($dbconn, $query);
        $result = mysqli_num_rows($data);

    if ($result == 1) 
    {
                $row = mysqli_fetch_array($data);
                $_SESSION['id_paciente'] = $row['nome_da_variavel'];
                header("location: paciente_marcar_consulta_online.php");
        } 

As you could see, I just changed the variable you wanted to send, but change the name if you wish.

Now on the page paciente_marcar_consulta_online.php, at the beginning of the page enter the same code <?php session_start(); ?> and then set a variable and go get the value sent by SESSION, i.e.

<?php $id_paciente = $_SESSION['id_paciente']; ?>

More Information

Once with the patient id, make a query to fetch the patient data.

$query = "SELECT FROM Paciente WHERE id_paciente= '$id_paciente'";
$data = mysqli_query($dbconn, $query);
$row = mysqli_fetch_array($data);

Now just print the name

<label><?=$row['nome']; ?></label>
 0
Author: I_like_trains, 2017-07-01 10:35:28