[SOLVED] PHP5, OOP, and Sessions problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

[SOLVED] PHP5, OOP, and Sessions problem.

Post 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]
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Post 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?
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Re: PHP5, OOP, and Sessions problem.

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply