Jump to content

Sessions question (OOP)


brianbayer1

Recommended Posts

i wanna start a session per user from the time they login. i'm learning so i wanna write this myself. So, if i use a class to generate the page a member logs into, can i assume i have to $_POST the session id from page to page or is there a way i can do that in the class? I am afraid when generating new pages that the class will generate a new session id per instance of the class and i don't want that. here is the class i am using(basic i know), and then the code for the page created from it.

 

page_class.php

 

<?php
// Page class
class Page {
// Declare a class member variable
var $page;
// declare session variable
var $PHPSESSID;
// The constructor function
function Page()
{
$this->page = '';
}
// Generates the top of the page
function addHeader($title)
{
$this->page .= <<<EOD
<html>
<head>
<title>$title</title>
</head>
<body>
<table width="800" border="1" cellspacing="2" align="center">
  <tr>
    <td colspan="2"><p>
<h1 align="center">$title</h1>
</td>
  </tr>
EOD;
}
// add navigation
function addNav($page1, $page2, $page3, $page4, $page5)
{
$this->page .= <<<EOD
<tr>
<td align='center' colspan='2'>

<a href='$page1.php'>About Us</a> / <a href='$page2.php'>Contact</a> / <a href='$page3.php'>Register</a> / <a href='$page4.php'>Login</a> / <a href='$page5.php'>home</a>
</td></tr>
EOD;
}
// Adds some more text to the page
function addContent($content)
{
$this->page .= <<<EOD
<td>$content</td>
</tr>
EOD;
}
// adds an image
function addImage($attribute, $image, $height, $width)
{
$this->page .= <<<EOD
<tr>
<td>
<div align='$attribute'><p><img src='../images/$image.jpg' height='$height.px' width='$width.px'><p>
</td>
EOD;
}
// Generates the bottom of the page
function addFooter($year, $copyright)
{
$this->page .= <<<EOD
<tr><td colspan="2">
<div align="center">© $year $copyright</div>
</td></tr></table>
</body>
</html>
EOD;
}
// Gets the contents of the page
function get()
{
return $this->page;
}
}

?>

 

index.php created from page class

 

<?php
session_start();
// does session_start() carry the sess_id from page to page or 
// do i need to $_POST it from page to page???????


$username = $_SESSION['username'];
$password = $_SESSION['password'];

// members only area

if ($_POST['logged'] = true) {
		echo "<b>Members Only Area</b><p>";
require_once 'page_class.php';
// Instantiate the Page class
$webPage = new Page();
// Add the header to the page
$webPage->addHeader('Know Your Enemy');
// add menu
$webPage->addNav(about, contact, register, login, page);
// add image
$webPage->addImage(center, me, 200, 100);
// Add something to the body of the page
$webPage->addContent("<p align='center'>Welcome $username</p>\n"); 
// Add the footer to the page
$webPage->addFooter(date('Y'), 'Shamrock Productions LLC');
// Display the page
echo $webPage->get();
	} else {
		$_POST['logged'] = !true;
		header("Location: http://localhost/test/login.php");
	}

?>

Link to comment
Share on other sites

on your login page, when you call on the database table for the users info

 

$query = "SELECT user_id, first_name, userlevel FROM tablenamehere WHERE (username='$un' AND password=SHA('$p'))";
$_SESSION['user_id'] = $row[0];
$_SESSION['first_name'] = $row[1];
$_SESSION['userlevel'] = $row[2];

session_name('whateveryouwanthere'); // this allows you to specify the sessions name and as long as you have both of these lines all the $_SESSION['']; data is accessible.
session_start();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.