Click to See Complete Forum and Search --> : Sessions?


Nodda4me
02-21-2007, 11:03 AM
I'm making a login system for my website and trying to use sessions. It will be using a database when it's finished.

The login part is good and everything but when I navigate to another section of the website the session is gone and it wants me to login again.

?php
if (!isset($_SESSION['Login'])) {
session_start();
}
if ($_GET['Login'] == "True") {
if ($_POST['Username'] == "User" && $_POST['Password'] == "123") {
$_SESSION['Login']="GOOD";
} else {
$_SESSION['Login']="BAD";
}
}
?>


Login box:
<?php
if ($_SESSION['Login'] != "GOOD") {
//the login form
if ($_SESSION['Login'] == "BAD") {
session_destroy();
echo "Bad Login";
}
} else if ($_SESSION['Login'] == "GOOD") {
echo "Good login!";
}
?>

papa_face
02-21-2007, 11:16 AM
You need to have session_start(); at the start of every page you want to use sessions on.

Nodda4me
02-21-2007, 11:22 AM
You need to have session_start(); at the start of every page you want to use sessions on.
It is.. I'm using index.php with switches, index.php?cat=(section)

papa_face
02-21-2007, 11:30 AM
paste the code for index.php

Nodda4me
02-21-2007, 11:31 AM
<?php
session_start();
if ($_GET['Login'] == "True") {
if ($_POST['Username'] == "User" && $_POST['Password'] == "123") {
$_SESSION['Login']="GOOD";
} else {
$_SESSION['Login']="BAD";
}
}
?>

before the html tag

Login box:
<?php
if ($_SESSION['Login'] != "GOOD") {
//the login form
if ($_SESSION['Login'] == "BAD") {
session_destroy();
echo "Bad Login";
}
} else if ($_SESSION['Login'] == "GOOD") {
echo "Good login!";
}
?>

papa_face
02-21-2007, 11:36 AM
what pages do you navigate to and it logs you out?
paste the code for one of those pages.

Nodda4me
02-21-2007, 11:38 AM
The login is a box, it's coded in the index

form name="login" id="login" method="POST" action="?cat=Login&Login=True">

darkninja
02-22-2007, 01:10 PM
I think you should get rid of this bit of code -


?php
if (!isset($_SESSION['Login'])) {
session_start();
}


You'll need the session_start(); on all pages, even for a logout page.

vivekanandb
02-23-2007, 01:00 AM
if (!isset($_SESSION['Login'])) {
session_start();
}

session_start() function restores all session variables created so far and makes available for current page. If you try to check for those variables before calling session_start() they will not be available.

so the check should be:
sesssion_start();

if(!isset($_SESSION["login"]))
{
//redirect to login page
}

HTH