简体   繁体   中英

PHP LOST SESSION

It's the first time I have that problem.

Basically, when I execute the login function, I create a session var which is passed to the index.php file.

The problem is that if I refresh the browser, or click to other menu items, I lost the session.

Here some code:

public function Login_Form()
{
    if(isset($_POST['login']))
        $this->login($_POST);
 ?>
<form action="<?php $_SERVER['PHP-SELF']; ?>" method="POST" id="form_login">
<table>
    <tr>
        <td>Username</td>
        <td><input type="text" name="username" /></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><input type="password" name="password" /></td>
    </tr>
    <td><input type="submit" name="login" value="Login" /></td>
</table>
</form>
 <?php
}

private function login($p)
{
    // OTTENGO VALORI PASSATI DAL LOGIN FORM
    $username = mysql_real_escape_string($p['username']);
    $password = mysql_real_escape_string(md5($p['password']));
    // CONTROLLO CHE ESISTA L'UTENTE
    $control = mysql_query("SELECT * FROM tareado.user WHERE username='$username' AND password='$password'");
    if(mysql_affected_rows() > 0)
    {
        while($x = mysql_fetch_array($control))
        {
            $id_utente = $x['iduser'];
            $_SESSION['idutente'] = $id_utente;
        }

        $_SESSION['username'] = $username;
        //session_write_close();            
        header("Location: index.php"); // REDIRECT TO INDEX.PHP, NOW WE ARE IN index.php?a=log
        //exit();

    }
    else
        echo "Credenziali errate";
}

I put on the top of index.php SESSION_START(), but I always lose the session.

Which could be the problem? The redirect?

I don't see a session start in the page where it get's redirected from. You could also use firebug to debug if the session even starts under the Cookies tab. You should see something like PHPSESSID when you try to login. Not sure if this is your answer but im unable to posts comments yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM