Page 1 of 1

[SOLVED] PHP5, OOP, and Sessions problem.

Posted: Thu Aug 19, 2004 12:51 pm
by bdeonline
feyd | Please use

Code: Select all

and

Code: 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.php

Code: 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, "");
		}
	}
}
?>
test2.php

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, "");
		}
	}
}
?>
secure_page_lib.php

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);
		}
	}
}
?>
test3.php

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>
Can anyone tell my why its making 4 separate sessions?


feyd | Please use

Code: Select all

and

Code: 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]

Posted: Thu Aug 19, 2004 1:30 pm
by bdeonline
Ok I did a much simpler test:

session1.php

Code: Select all

<?php
session_start();
$username = "Chris";
$_SESSION&#1111;'username'] = $username;
?>
<a href="session2.php">Session 2</a>
session2.php

Code: Select all

<?php
session_start();
echo $_SESSION&#1111;'username'];
?>
And it still creates 2 sessions. Am I doing something wrong or is it php?

Posted: Thu Aug 19, 2004 1:35 pm
by bdeonline
Figured it out just something wrong with php.ini replaced it works now.
The best way to fix things it to think them out yourself.

Posted: Thu Aug 19, 2004 1:36 pm
by feyd
if you have trans_id's off, and your browser doesn't accept the cookie, or the cookie is set up wrong (for whatever reason), it could create this problem..

Re: PHP5, OOP, and Sessions problem.

Posted: Thu Aug 19, 2004 1:49 pm
by bdeonline
bdeonline wrote:feyd | Please use

Code: Select all

and

Code: 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][/quote]
Why it just adds [url=http://www.w3.org/TR/REC-html40/present/graphics.html#edef-FONT]deprecated font tags[/url] for color coding and adds more junk into mysql.

Posted: Thu Aug 19, 2004 1:53 pm
by feyd
we'd like to see the code with highlighting, so it's a bit easier to read. It adds less to mysql actually.