PHP session does not work

Asked

Viewed 2,656 times

4

I have a system from which I use session to validate the user. For validation I use the code below:

<?php
session_start();
 public function validaUsuarios($loginUsuario,$senhaUsuario){
       .......
       $_SESSION['logado'] = true;
        header('location: entrar.php');
......
?>

And on the page enter.php:

<?php
session_start();
if($_SESSION['logado'] == false || $_SESSION['logado'] == ''){
    header('location: index.php');
}else{
 ...........

What I do not understand is that in other servers worked perfectly, but this server does not take first, IE, when giving a print, does not pass any value in the session.

  • The function validaUsuarios is within a class?

  • Yes. It is inside a class and at the top I put session_start(). <? php session_start(); class Extends Methods Connect{

  • what I find strange is that it returns to the login page and when soon me again, he "sees" the value and I can access.

  • Try to put exit(); below redirects. For example: header('location: entrar.php'); exit();

  • I put it, but it didn’t work. The session still doesn’t get the value right the first time, only the second time....

  • Try to see the contents of $_SESSION right after the line $_SESSION['logado'] = true; to see if you are in session. ex.: var_dump($_SESSION);die; Maybe the problem isn’t in the piece of code you posted.

  • I did it the way you asked and it appears the values, IE, the value is created, but when it arrives in the other page, the value arrive empty.

  • I would say it has something to do with Handler’s mistakes. Try changing the if($_SESSION['logado'] == false || $_SESSION['logado'] == ''){ for if(isset($_SESSION['logado']) === false){ because in fact, at the first moment there is no index 'logged in' and your server may be returning a Warning and not false or empty.

  • Hi, Gê. The problem is that in the address page there is no value in $_SESSION['logged in], that is, when I do the validation the value appears as 1, but when it arrives in the page that is directed, the value arrives empty.

  • Create a phpinfo.php file, put the <?php phpinfo(); ? > save the return and put it somewhere so we can analyze.

  • If possible send from a working server and one that doesn’t work, it may be some configuration on the server.

Show 6 more comments

2 answers

2


Check if you have write permission in the tmp folder, or in another folder in which php is configured to save the session files;

In any case, check the server’s php.ini settings and try to force settings using ini_set. Possible settings for sessions you can find in http://php.net/manual/en/session.configuration.php.

1

Hello! Dear ones, I went through a search similar to my application hosted at Locaweb, the support did not help much, I sent the example below to them and returned me another example, using form (html) and passing the value via post to a session, then sending it to another page... anyway, there was no way to use this methodology every time I had to work with the sessions, and using my method my sessions ceased to exist in pg2.php. It had the following testing structure in the domain:

index php.

<?php
    session_start();
    $_SESSION["texto"] = "teste";
    echo "<a href=\"pg2.php\">clique aqui</a>";
?>

pg2.php

<?php 
    session_start();
    echo $teste = $_SESSION["texto"];
?>

The solution I found was... Change the path of Session.save_path in php.ini; and Enable the ob_start output buffer();

The structure became this: index php.

<?php
    ob_start();
    session_start();
    $_SESSION["texto"] = "teste";
    echo "<a href=\"pg2.php\">clique aqui</a>";
?>

pg2.php

<?php
    ob_start();
    session_start();
    echo $teste = $_SESSION["texto"];
?>

Browser other questions tagged

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