Reputation: 11
I'm creating an online store using ajax and php. My index has a login section that appears with ajax and also have a navigator bar that gets the name of the user when completes the login.
So, when you login to the page the form sends you to Login.php, where you can see the content of the session and the navigator bar changes as explained before.
I only had session_start() on the index page, and it worked fine; the SESSION stored everything I wanted and the navigation bar worked, showing the name. But then when I changed something with ajax and printed out the session (print_r($_SESSION)) it turned out that the session had disappeared and it wasn't defined.
A friend told me to put session_start() in every page, so I put it in the Login.php file and now the session variable appears to be empty right after the login, so the navigator bar doesn't even work.
Also, when refreshing the index after getting logged in, it doesn't stay logged at all.
So it appears that the Session stores the variables but they don't stay stored for long.
I'm trying to use the MVC scheme (it's an assignment) so I only use the session_start on the index and the login page, not on the "controllers" and the "views"
To sum up, my session works at the beggining, it stores data, I change to the second page and still works and after loading the page the content is gone and the session undefined, but if I write down "session_start()" on the second file it doesn't even work
Here is the code where I get info from the db. (I don't think it's usefull but)
function Login()
{
// Skiping conection code
$sql = "SELECT * FROM Usuario WHERE nombre = '$username' && password = '$password'";
$resultat = mysqli_query($connexio,$sql)or die(mysqli_error($connexio));
$usuaris = array();
while ($fila = mysqli_fetch_array($resultat))
{
$usuaris = $fila;
}
$_SESSION['ID'] = $usuaris['ID'];
$_SESSION['nombre'] = $usuaris['nombre'];
$_SESSION['admin'] = $usuaris['Admin'];
mysqli_close($connexio);
return $usuaris;
}
Thanks for the attention
Upvotes: 1
Views: 1573
Reputation: 44864
Add following before session_start()
session_set_cookie_params(0, "/");
Upvotes: 0