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.
<?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>");
}
?>
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.
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
<?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
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.