[SOLVED] PHP5, OOP, and Sessions problem.
Posted: Thu Aug 19, 2004 12:51 pm
feyd | Please use
test2.php
secure_page_lib.php
test3.php
Can anyone tell my why its making 4 separate sessions?
feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Ok I am creating a reusable login script.
When you go to the main home page it checks for a session id if none found it sends you to the login and after submitting your username and password it is suppose to go back to the main page.
Probem is its creating 4 sessions besides using just one:
lib_login.phpCode: Select all
<?php
require_once('lib/database_lib.php');
class login extends db {
function login($database, $table, $error, $main_page) {
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
db::dbconnect($database);
$result = db::dbquery("SELECT username FROM $table WHERE username='$username' AND password='$password'");
$row = db::dbfetch($result);
if ($username == $row->username) {
$_SESSION['username'] = $username;
if (isset($_POST['checked'])) {
$checked = $_POST['checked'];
setcookie($session_name, $username, time()+604800);
}
header("Location:" . $main_page);
} else {
$php_self = $_SERVER['PHP_SELF'] . "?error";
header("Location:" . $php_self);
}
} elseif(isset($_GET['error'])) {
echo $error;
}
}
function logout() {
session_start();
unset($_SESSION['username']);
if (isset($_COOKIE['username'])) {
setcookie($session_name, "");
}
}
}
?>Code: Select all
<?php
session_start();
require_once('lib/database_lib.php');
class login extends db {
function login($database, $table, $error, $main_page) {
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
db::dbconnect($database);
$result = db::dbquery("SELECT username FROM $table WHERE username='$username' AND password='$password'");
$row = db::dbfetch($result);
if ($username == $row->username) {
$_SESSION['username'] = $username;
if (isset($_POST['checked'])) {
$checked = $_POST['checked'];
setcookie($session_name, $username, time()+604800);
}
header("Location:" . $main_page);
} else {
$php_self = $_SERVER['PHP_SELF'] . "?error";
header("Location:" . $php_self);
}
} elseif(isset($_GET['error'])) {
echo $error;
}
}
function logout() {
session_start();
unset($_SESSION['username']);
if (isset($_COOKIE['username'])) {
setcookie($session_name, "");
}
}
}
?>Code: Select all
<?php
class secure_page {
function secure_page($login_page) {
if ($_SESSION['username'] || $_COOKIE['username']) {
} else {
$login_page = $login_page . "?error";
header("Location:" . $login_page);
}
}
}
?>Code: Select all
<?php
session_start();
require_once('lib/secure_page_lib.php');
$secure_page = new secure_page;
$secure_page->secure_page("test2.php");
?>
<HTML>
<HEAD>
<title>Homepage</title>
</HEAD>
<BODY>
<h3>Welcome <?php echo $_SESSION['username']; ?></h3>
<h1>Homepage</h1>
</BODY>
</HTML>feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]