ricky spires Posted October 5, 2011 Share Posted October 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248497-sessions-help-needed/ Share on other sites More sharing options...
Drummin Posted October 5, 2011 Share Posted October 5, 2011 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/248497-sessions-help-needed/#findComment-1276142 Share on other sites More sharing options...
Drummin Posted October 5, 2011 Share Posted October 5, 2011 Obviously my shift slipped on the last one. require_once("../includes/initialize.php"); session_start(); IF (isset($_SESSION["user_id"])){ $userid=$_SESSION["user_id"]; }ELSE{ header("Location:pages/admin_login.php"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/248497-sessions-help-needed/#findComment-1276151 Share on other sites More sharing options...
ricky spires Posted October 5, 2011 Author Share Posted October 5, 2011 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"]); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248497-sessions-help-needed/#findComment-1276186 Share on other sites More sharing options...
Drummin Posted October 5, 2011 Share Posted October 5, 2011 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/248497-sessions-help-needed/#findComment-1276265 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248497-sessions-help-needed/#findComment-1276287 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.