Empty session variable when redirected

Asked

Viewed 189 times

-2

I am using session variables to check the user logged in to the site.

I have two different links to the same page, one automatically generated by a PHP script through a query in my database. The other I put on the site myself.

What is happening is the following, when I use the link generated by the query in the database the session variables I have defined keeps all their value. But when I use the link I put on the page, all session variables are empty.

Does anyone have any idea what might be going on?

  • Hello Open, no code has no way to solve your problem.

  • Is using session_start(); on all pages?

  • Hi Marcelo, I put the code summarized below

  • Use session_start(); on all pages yes

1 answer

-2

Anyway, I managed to solve my problem, but I’m going to leave things better explained here to someone who might have the same problem as mine.

On one page of the website the link to another by the following script:

<?php
//Verifica as questões do relatório disponíveis no banco de dados
$query = "SELECT idquestao, numquestao FROM questao WHERE relatorio_idrelatorio = '"
        . $_GET['id'] . "' ORDER BY numquestao";
$result = mysqli_query($link, $query);
while ($linha = mysqli_fetch_array($result)) {
    $acessar = "http://www.site.com/questao.php?id=" . $linha['idquestao'];
    echo "<li><a href='" . $acessar . "'>Atividade " . $linha['numquestao'] . "</a></li>";
}
?>

And on that same page I link to the same other page by the code:

<a href="http://www.site.com/questao.php?id=1">Reponder</a>

My problem was this, when using the link that was generated by the PHP script was redirected to the other page and my session variables kept their values.

<?php
    session_start();
    if (!$_SESSION['id'] || !$_SESSION['nome'] || !$_SESSION['usuario']) {
        //header("Location:http://www.site.com/?erro=2");
    }
    echo $_SESSION['id'];
    echo $_SESSION['nome'];
    echo $_SESSION['usuario'];
?>

So the above code displayed the values passed to the variables when the user logged in.

2 Gabriel Gabriel

But when I used the link I had put on the page myself, the session variables appeared empty.

The way the problem was solved was just removing the "www" from the beginning of the link

<a href="http://site.com/questao.php?id=1">Reponder</a>

So when I click on the link the session variables present the values correctly.

2 Gabriel Gabriel

I don’t know why I had a problem, but I solved it that way.

Apparently PHP doesn’t like "www" rs

Browser other questions tagged

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