Jump to content

sessions help needed


ricky spires

Recommended Posts

hello.

 

i have 2 issues.

 

1.

im trying to create a session or a global for the pageName but im going in circles.

 

on each page i have the page name. for example:

$pageName="adminHome";

 

each page has its own variables in the db which say how the page is layed out etc..

 

so the template index looks for the pageName and then pulls the correct layout from to db..

 

i need the template (and other pages) to get the $pageName="" everytime the page changes.

 

please help.

 

 

this code might help.

 

this is a basic page:

<?PHP
require_once("../includes/initialize.php");

$pName = "adminHome";
$page = Pages::find_by_pageName($pName);
echo $page->id."<br />";

 

 

 

this is my layouts/header.php where $session = new Session(); is located and its also looking for the pageName but i cant seem to get it to see it

 

<?PHP
require_once("../includes/initialize.php");
$session = new Session();

NEEDS TO FIND PAGE NAME HERE

if($pageName == "adminHome"){
   if (!$session->is_logged_in()) { $user=""; }else{ $user = User::find_by_id($_SESSION["user_id"]);  redirect_to("home.php");}
}else{
   if (!$session->is_logged_in()) { $session->message("Access denied."); redirect_to("pages/admin_login.php"); }else{ $user = User::find_by_id($_SESSION["user_id"]); }
}
?>

 

---------------------------------------------------------------------

 

 

 

PROBLEM 2.

 

problem 1 and 2 could be solved by the same thing really.

 

on the page i have the pageName. i then get the various bits from the db like the template_id, the layout_id etc... which are needed on other pages .

 

do i session each variable or put each in to a global or is there some other way.

 

for example:

 

$pName = "adminHome";
$page = Pages::find_by_pageName($pName);

echo $page->id."<br />";
echo $page->pageName."<br />";
echo $page->layoutTemps_id.'<br/>';
//ETC

 

now i want to put these into there own sessions or globals (or something else) to pass to other pages

 

 

ANY THOUGHTS...

 

 

 

 

 

i have a session class. but im not using it for this. maybe i could use it but im not sure how.

 

<?PHP
class Session {
private $logged_in=false;
public  $user_id;
public $message;



function __construct(){
//	session_start();
	$this->check_login();
	$this->check_message();

}//end function __construct() session start

	/*
	if($this->logged_in) {
		// do something
	} else {
		// do something
	} //end if	
	*/

public function is_logged_in() {

	return $this->logged_in;

}//end function is_logged_in() 



public function login($user) {

//database should find user based on username/password

	if($user) {

	$this->user_id = $_SESSION['user_id'] = $user->id;
	$this->logged_in = true;

	}//end if

}//function login($user)


public function logout() {

	unset($_SESSION['user_id']);
	unset($this->user_id);
	$this->logged_in = false;

}//end function logout()

// you have to set the message
// this can set and get a message

public function message($msg="") {

	if(!empty($msg)) {

		//then this is "set message"
		// make sure you understand why $this->message=$msg wouldn't work
		// we have to store it in the session

		$_SESSION['message'] = $msg;
	} else { 

		// then this is "get message"
		return $this->message;

	} // end if

} // end function message($msg="") 



private function check_login() {

	if(isset($_SESSION['user_id'])) {

		$this->user_id = $_SESSION['user_id'];
		$this->logged_in = true;
	}else{
		unset($this->user_id);
		$this->logged_in = false;
	}//end if
}//end function check_login() 


private function check_message() {

//is there a message stored in the session?

	if(isset($_SESSION['message'])) {

	// Add it as an attribute and erase the stored version

		$this->message = $_SESSION['message'];
	    unset($_SESSION['message']);

	}else{

		$this->message = "";

	}//end if
}//end check_message()

}//end class session

$session = new Session(); //create a session
$message = $session->message();

?>

 

 

THANKS

rick

Link to comment
Share on other sites

Seems a little over complicated.  I see no need to save page name to session as you have a variable on the page already. See if this makes more sense.

<?php
require_once("../includes/initialize.php");
  
session_start();
IF (isset($_SESSION["user_id"]))[
$userid=$_SESSION["user_id"];
}ELSE{
header("Location:pages/admin_login.php");
exit();
}
//IF we're still here, User approved.//
$pName = "adminHome";
// Continue getting page...sample etc
$getpage = mysql_query("SELECT * FROM pages WHERE pagename='$pName'") or die(mysql_error()); 
// etc

?> 

Link to comment
Share on other sites

thanks for your reply.

 

I see no need to save page name to session as you have a variable on the page already

 

the problem is that the $pageName changes on every page and each page might have use a different layout.

 

the $pageName= is on each of the pages

 

the code below is in the template header layout and looks to see what the page is to set the layout

 

<?PHP
require_once("../includes/initialize.php");
$session = new Session();

NEEDS TO FIND PAGE NAME HERE FROM A DIFFERENT PAGE

if($pageName == "adminHome"){
   if (!$session->is_logged_in()) { $user=""; }else{ $user = User::find_by_id($_SESSION["user_id"]);  redirect_to("home.php");}
}else{
   if (!$session->is_logged_in()) { $session->message("Access denied."); redirect_to("pages/admin_login.php"); }else{ $user = User::find_by_id($_SESSION["user_id"]); }
}
?>

 

:)

Link to comment
Share on other sites

the problem is that the $pageName changes on every page and each page might have use a different layout.

 

the $pageName= is on each of the pages

So are you making physical pages or generating pages dynamically? Even so IF the variable $pageName is on every page and you're going to get the template layout and template id from the DB with a query for $pageName and use a session to store template id (even though it might change on every page).  OK sorry I'm not following the logic...  Well once you have the template_id and template_layout set these to session.

<?PHP 
$_SESSION['template_id']="$template_id variable I've looked up for $pageName";
$_SESSION['template_layout']="$template_layout variable I've looked up for $pageName";
?>

Link to comment
Share on other sites

So you need to get a variable in the header.php that is defined later on in the script? Not going to happen.

Why dont you store the physical path/filename of the page instead of a defined variable in your database?

For example: adminHome could be /admin/home.html, then it would be a matter of grabbing the script name and fetching from the database using that.

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.