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:
Which means he gets two pages on top of each other, 'cause that’s what Dashboard looks like:
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?
– Bulfaitelo
The goal is to use login(cnpj) to search for information in the database.
– Nicolas
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.
– Bulfaitelo
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.
– Bulfaitelo