Logged in user

Boa tarde,

Tenho um arquivo chamado formulario.php onde é preenchido um formulario e enviado para um arquivo gerar_pdf.php , onde é gerado um arquivo PDF com todos os dados digitados no formulário, mas no final do arquivo eu coloquei um código onde ele mostra uma $_SESSION com o usuário que foi logado e criou o formulário só que na hospedagem local (localhost) ele funciona e apare usuario, data e hora mas na hora que fiz a hospedagem não mostra. Segue abaixo o código.

TRANSLATION
Good afternoon, I have a file called formulario.php where a form is filled out and sent to a file gerar_pdf.php , where a PDF file is generated with all the data entered into the form, but at the end of the file I put a code where it shows a $_SESSION with the user who was logged in and created the form but on local hosting (localhost) it works and includes user, date and time but when I did it the hosting doesn’t show it. Below is the code.

$this->Cell(0, 10, 'Viagem Emitida Por: '.$_SESSION["nome"]. ' em '.date('d/m/Y'). ' às ' .date('h:i:s'), 0, false, 'C', 0, '', 0, false, 'T', 'M');

Did you…

  1. Create the session on all pages it is used
  2. Assign something to $_SESSION[‘nome’]?

Also, please type in english or use an online translator before posting as this is an english forum

4 Likes

I included $_SESSION on all pages but it doesn’t appear which logged in user created the file or the creation date

Share the full code the the effected pages please

2 Likes

Form page.

form.php

<?php

    include('protect.php');

?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
   
<img src="img/brasao.png" width="100px">
<h3></h3>
<h4></h4>

<form action="gerar_pdf.php" method="POST">
    <fieldset>
        <legend>Dados</legend>

            <br>
            <label>Nome:</label>
            <input type="text" id="nome" name="nome" size="30" autocomplete="off">
            
            &nbsp;&nbsp;
            
            <label for="cpf">RG/CPF do Paciente:</label>
            <input type="text" id="cpf" name="cpf"  autocomplete="off">

        <p>
            <label for="nascimento">Data de Nascimento:</label>
            <input type="text" id="nascimento" name="nascimento"  autocomplete="off">
            
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <label for="idade">Idade:</label>
            <input type="text" id="idade" name="idade"  autocomplete="off">
        </p>
            <label for="endereco">Endereço:</label>
            <input type="tel" id="endereco" name="endereco"  autocomplete="off">
            &nbsp;&nbsp;
            <label for="telefone">Telefone:</label>
            <input type="tel" id="telefone" name="telefone"  autocomplete="off">
        </fieldset>

         <fieldset>
            <legend>Acompanhante</legend>

            <br>
            <label for="acompanhante">Nome do Acompanhante:</label>
            <input type="text" id="acompanhante" name="acompanhante" autocomplete="off">
            &nbsp;
            <label for="doc_acompanhante">RG/CPF do Acompanhante:</label>
            <input type="text" id="doc_acompanhante" name="doc_acompanhante" autocomplete="off">

        </fieldset>

         <fieldset>

            <legend>Dados da Viagem</legend>

            <br>
            <label for="destino">Destino:</label>
            <input type="text" id="destino" name="destino"  autocomplete="off">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label for="hospital">Hospital/Clínica/Laboratório:</label>
            <input type="text" id="hospital" name="hospital"  autocomplete="off">

            <p>
            <label for="viagem">Motivo da Viagem:</label>
            <input type="text" id="viagem" name="viagem"  autocomplete="off">
        </p>
            <label for="local">Local de Embarque:</label>
            <input type="text" id="local" name="local"  autocomplete="off">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label for="horario">Horário da Consulta:</label>
            <input type="text" id="horario" name="horario"  autocomplete="off">
            <p>
            <label for="data_consulta">Data da Consulta/Exame:</label>
            <input type="text" id="data_consulta" name="data_consulta" size="15" autocomplete="off">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label for="data_transporte">Data da Marcação do Transporte:</label>
            <input type="text" id="data_transporte" name="data_transporte" size="15"  autocomplete="off">
</p>
            
        </fieldset>

        <fieldset>
            <legend>Observação</legend>

            <br>
            <textarea id="mensagem" name="mensagem" rows="5" style="min-width: 765px;" autocomplete="off"></textarea>
        </fieldset>

       <button type="submit">Enviar</button>

       <p>Usuário Logado: <?php echo $_SESSION['nome']; ?>

       <a href="logout.php">Sair da Sessão</a></p>
    

    </form>
</body>
</html>

Page that receives the form
gerar_pdf.php

<?php

include('protect.php');

require_once('TCPDF/tcpdf.php');

$pdf = new TCPDF();

$nome = $_POST['nome'];

$cpf = $_POST['cpf'];

$nascimento = $_POST['nascimento'];

$idade = $_POST['idade'];

$endereco = $_POST['endereco'];

$telefone = $_POST['telefone'];

$acompanhante = $_POST['acompanhante'];

$doc_acompanhante = $_POST['doc_acompanhante'];

$destino = $_POST['destino'];

$hospital = $_POST['hospital'];

$viagem = $_POST['viagem'];

$local = $_POST['local'];

$horario = $_POST['horario'];

$data_consulta = $_POST['data_consulta'];

$data_transporte = $_POST['data_transporte'];

$mensagem = $_POST['mensagem'];

$html = '<br><br><br><br><br><br><P><table>

<p style="text-align: center">



<p></p>

</p>

<tr>

<td colspan="4"><strong>Nome do Paciente:</strong> '.$nome.'</td>

</tr>

<tr><td></td></tr>

<tr>

<td><strong>CPF:</strong>'.$cpf.'</td>

<td colspan="2"><strong>Data de Nascimento:</strong> '.$nascimento.'</td>

<td><strong>Idade:</strong>'.$idade.'</td>

</tr>

<tr><td></td></tr>

<tr>

<td colspan="2"><strong>Endereço:</strong> '.$endereco.'</td>

<td colspan="2"><strong>Telefone:</strong> '.$telefone.'</td>

</tr>

<tr><td></td></tr>

<tr>

<td colspan="2"><strong>Nome do Acompanhante:</strong> '.$acompanhante.'</td>

<td colspan="2"><strong>RG/CPF do Acompanhante:</strong> '.$doc_acompanhante.'</td>

</tr>

<tr><td></td></tr>

<tr>

<td colspan="1"><strong>Destino:</strong> '.$destino.'</td>

<td colspan="3"><strong>Hospital/Clínica/Laboratório:</strong> '.$hospital.'</td>

</tr>

<tr><td></td></tr>

<tr>

<td colspan="3"><strong>Motivo da Viagem:</strong> '.$viagem.'</td>

</tr>

<tr><td></td></tr>

<tr>

<td colspan="2"><strong>Local de Embarque:</strong> '.$local.'</td>

<td colspan="2"><strong>Horário da Consulta:</strong> '.$horario.'</td>

</tr>

<tr><td></td></tr>

<tr>

<td colspan="2"><strong>Data da Consulta/Exame:</strong> '.$data_consulta.'</td>

<td colspan="2"><strong>Data da Marcação do Transporte:</strong> '.$data_transporte.'</td>

</tr>

<tr><td></td></tr>

<tr><td></td></tr>

<tr><td></td></tr>

<tr>

<td colspan="4"><strong>Observação:</strong> '.$mensagem.'</td>

</tr>

<tr><td></td></tr>

<tr><td></td></tr>

<tr><td></td></tr>

<tr><td></td></tr>

<tr>

<td colspan="4" style="text-align: center">______________________________________________</td>

</tr>

<tr>

<td colspan="4" style="text-align: center">Assinatura do Paciente ou Responsável</td>

</tr>

</table></p>';

// Extend the TCPDF class to create custom Header and Footer

class MYPDF extends TCPDF {

// Page footer

public function Footer() {

// Position at 15 mm from bottom

$this->SetY(-15);

// fonte referente ao texto do rodapé

$this->SetFont('helvetica', '', 10);

// rodapé

$this->Cell(0, 10, 'Viagem Emitida Por: '.$_SESSION["nome"]. ' em '.date('d/m/Y'). ' às ' .date('h:i:s'), 0, false, 'C', 0, '', 0, false, 'T', 'M');

}

}

// create new PDF document

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information

$pdf->SetCreator(PDF_CREATOR);

$pdf->SetAuthor('Dênis Pereira Borges');

$pdf->SetTitle('FAA Transporte');

// ---------------------------------------------------------

$pdf->AddPage();

$pdf->setJPEGQuality(75);

$pdf->Image('img/brasao.jpg', 90, 20, 30, 30);

//escrever o conteudo html no pdf

$pdf->writeHTML($html, true, false, true, false, '');

//saida do PDF para o navegador ou arquivo

$pdf->Output('paciente.pdf', 'I');

?>

Session page

protect.php

<?php

if(!isset($_SESSION)) {
    session_start();
}

if(!isset($_SESSION['id'])) {
    die("Você não pode acessar esta página porque não está logado.<p><a href=\"index.php\">Entrar</a></p>");
}


?>

I don’t see any obvious issues with the code, but it’s hard to say for sure without seeing the entire website.

Looking at what @Greenreader9 wrote:

This looks OK thanks to the protect.php being included everywhere. Although, a suggestion: I would not use include here but require_once. You want the protect.php file to be loaded on every page, one time. include lets you run the code multiple times (which is useless), and PHP will just continue without protect.php if if can’t find the file, which is definitely not what you want.

Actually, that could potentially be the reason why you get this error.

Perhaps there is other code on your website responsible for this, but I don’t see any place where this session variable is set. And if the nome session variable is not set, then of course you can’t use it.

2 Likes

It’s strange because I’m using this code in xampp and it’s working perfectly. Now I’m testing it in the hosting editor and I removed the session name and put the ID and it gets the ID of the logged in user but when I try to get the name nothing appears in the document. A and replaced the include with require_once

    $this->Cell(0, 10, 'Trip Issued By: '.$_SESSION["id"]. ' in '.date('d/m/Y h:i:s'). ' at ' .date ('h:i:s'), 0, false, 'C', 0, '', 0, false, 'T', 'M');

Below is the code from the protect.php page

<?php

if(!isset($_SESSION)) {
    session_start();
}

if(!isset($_SESSION['id'])) {
    die("You cannot access this page because you are not logged in.<p><a href=\"index.php\">Log in</a></p>");
}


?>

Detail, I already changed the id to name on the protect page and it didn’t work. And the strangest thing is that it is only happening with the user name because the date and time is being taken

@Administrator
I changed the include to require_once. At first it didn’t seem to work, but after making the changes the user appeared logged in to the PDF page created. Thank you in advance for your attention

Now the correct user name and date are coming out, but the time is not. For example, I created the document at 11:10 pm at night but the document says 9:10 am. Do you have to change something in the hosting or does it have to be in PHP

It might be both.

Our servers are configured to run in EST timezone by default, that’s UTC-5. MySQL does not support setting timezones for individual databases or users, so if you ask MySQL for the current time, you’ll always get the EST time.

With PHP, it’s EST by default, but you can configure it. There are a few different ways to do it, but the PHP Options menu in the client area (found in the domain options - it’s configurable per website) is probably the easiest one.

But then you do have to make sure to always generate timestamps from PHP and not from MySQL.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.