How to send variables from one . PHP to another . PHP

Asked

Viewed 964 times

1

Tela login.php, this is the main screen where the user logs in with CNPJ and password.

<?php
if( isset($_POST['username']) and isset($_POST['password']) ) {
        include('login.php'); //code is given below (used for database connection)
        $user = $_POST['username'];
        $pass = $_POST['password'];

        $ret= pg_query( $dbconn, "SELECT * FROM pessoa WHERE cnpj = '$user' AND senha = md5('$pass')");
        $row = pg_fetch_assoc($ret);
        if($row == 0 ) {
            session_start(); #tentativa de jogar o $user para o Dashboard.php
            $_SESSION['user'] = $user; #isso faz parte da tentativa
            header("Location: Tela Login.php");
        }
        else {
            header('location: Dashboard\Dashboard.php');
        }
}
?>

Below this php has all the html of Login screen.php that I happen to give include in it in another. php file, it pulls the whole file, both html and php. Example below:

inserir a descrição da imagem aqui

Which means he gets two pages on top of each other, 'cause that’s what Dashboard looks like: inserir a descrição da imagem aqui

Below the code of login.php which connects to the database and creates an array with the table data. It already works because I previously tested in the Login screen.php

<?php 
$dbconn = pg_connect("host=NomeDoHost port=Porta dbname=NomeDoBanco user=usuario password=Senha");
$query = "SELECT cpfcnpj_formata(cnpj), nome FROM pessoa WHERE cnpj = ''";
$resultado = pg_query($dbconn,$query);
#While para tentar passar os dados pro Dashboard.php
while($linha = pg_fetch_array($resultado)) 
{
    echo "CNPJ: " .$linha['cnpj'] . "-";
    echo "Senha em md5: ".$linha['senha'] ."-194171-";
    echo md5('194171');
    $usuario = $linha['cnpj'];
    $senha = $linha['senha'];
}
pg_close($dbconn);

?>

I tried to use too $_SESSION or give include on the page Login screen.php, But they both don’t work the way I want them to. At the moment I want to pull data from the database to use them as information on the screen, for example: Show the CNPJ of the company that the user is connected.

But I pull the database information with the login that is typed in a textbox($user) in Login screen.php

Dashboard.php below

<?php
session_start();
    $cnpj = $_SESSION['user'];
    echo $cnpj;
?>

If there’s another way to do what I’m trying, I’m open to new perspectives.

  • The goal is to create a way to keep the user logged in to your system?

  • The goal is to use login(cnpj) to search for information in the database.

  • So, at the time of login, you take the information from the bank and play in the session so it will not be necessary to take this information again, I will update the answer to put this example.

  • I updated here, something that I realized that your logic was wrong, you were putting Sesssion when there was no user, so it wasn’t working, I took the liberty of changing some things, test there to see if it works, and give me a feedback.

2 answers

1

Store these variables in section Session() that they will be available on the other pages of your system.

<?php
        session_start();
        $_SESSION['test'] = 42;
        $test = 43;
        echo $_SESSION['test'];

?>
  • I tried with the $_SESSION for a while and it worked, after nothing went wrong again, stopping to think now and testing your code n makes no sense. I don’t understand what you wanted to show him.

0


I made a mini Example of how you can use the $_SESSION, divided into 3 parts, index, test, and Destroy, where index it will set the variables, test where you will view what was recorded in $_SESSION and finally destroy all of it (a possible logoff)

index php.:

<?php
session_start(); // Inicia a sessão
$_SESSION['user_data']['cnpj']= '123.13.13.13.';
$_SESSION['user_data']['id']= 12;

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Index</title>
</head>
<body>
    <br>
    <a href="test.php">test</a> <br>
    <a href="destroy.php">destroy</a> <br>
</body>
</html>

test php.

<?php
session_start(); // Inicia a sessão
echo "<pre>";
var_dump($_SESSION);
echo "</pre>";

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>test</title>
</head>
<body>
    <br>
    <a href="destroy.php">destroy</a> <br>
    <a href="index.php">index</a> <br>
</body>
</html>

Destroy.php

<?php
session_start(); // Inicia a sessão
session_destroy();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>test</title>
</head>
<body>
    <br>
    <a href="test.php">teste</a> <br>
    <a href="index.php">index</a>
</body>
</html>

You can also use the unset(); specifying which $_SESSION to delete, in my example I simply set session and opened on a second page to see how it works and the last page we destroyed it completely.

Note: I recommend working with vectors is easier to locate what you want, for example $_SESSION['user_data'] here receives an array with all user data that must be in the session, data that will always be loaded as name, and etc, data that must be dynamic, as tables and Forms these yes must come directly from the database.

Fitting the login form for your problem.

<?php
session_start(); 
if( isset($_POST['username']) and isset($_POST['password']) ) {
    include('login.php'); //code is given below (used for database connection)
    $user = $_POST['username'];
    $pass = $_POST['password'];

    $ret= pg_query( $dbconn, "SELECT * FROM pessoa WHERE cnpj = '$user' AND senha = md5('$pass')");
    $count_row = pg_num_rows($ret);
    if($count_row == 0 ) { // se for igual 0 e porque não encontrou usuario
        header("Location: Tela Login.php");
    } 
    elseif($count_row > 1 ){
        // tem algo errado com seu banco só pode ter 1 usuario 
        header("Location: Tela Login.php"); // gambiarra pra evitar uma possivel falha de segurança. 
    }elseif ($count_row == -1) {
        //deu erro no sql
        var_dump($ret);
    }
    else {         
        while ($row = pg_fetch_array($ret)){
            $_SESSION['user']['cnpj'] = $ret['cnpj'];
            $_SESSION['user']['outro campo que queria aqui'] = $ret['nome do outro cmapo'];

        }
        header('location: Dashboard\Dashboard.php');
    }
}
?>

Browser other questions tagged

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