So I’m now trying my hand at creating a member system using PHP, and Sessions, and I can get sessions working once a person is logged in, but once they navigate away from the page they land on after login, they lose the session. I don’t know anything about sessions, so I’m not sure where I’m going wrong…but here’s my code (i know it may not be proper, but again, it’s just a development script)…
[php]
// login script
$error = “”;
$username = “”;
if (isset($_POST[‘username’]) || isset($_POST[‘password’])) {
if ($_POST[‘username’] == “” && $_POST[‘password’] == “”) {
$error = “Invalid Username and Password”;
} elseif ($_POST[‘username’] == “”) {
$error = “Invalid Username”;
} elseif ($_POST[‘password’] == “”) {
$error = “Invalid Password”;
} else {
// check login
$un = $_POST[‘username’];
$pw = md5($_REQUEST[‘password’]);
//$pw = $_POST[‘password’];
$query = mysqli_query($con,“SELECT * FROM users WHERE username=’$un’ AND password=’$pw’”);
$row = mysqli_fetch_array($query);
if(is_array($row)) {
$_SESSION[“user_id”] = $row[‘id’];
$_SESSION[“user_name”] = $row[‘username’];
} else {
// if not user
$error = “We couldn’t find an account linked with that email address. Please check your login details and try again.”;
}
if(isset($_SESSION[“id”])) {
header(“Location:members.php”);
}
}
}
if (isset($_GET[‘action’]) && $_GET[‘action’] == “logout”) {
session_destroy(‘cookiename’);
header(‘Location: /?loggedout’);
}
[/php]
Any idea how I can continue this session? I’d prefer to do it as a page include on pages that require a login to view…if that makes any sense to anyone.
lol