play_ Posted November 7, 2007 Share Posted November 7, 2007 Before you ask what this monstrosity is about, allow me to explain. I have header.php, and footer.php. So that way on every page, i can do: <?php $page_title = 'welcome to my site'; $page_header = 'My Blog.'; include('./includes/header.php'); echo 'Welcome to my site.'; include('./includes/footer.php'); ?> Ok that's very simple. I'm sure everyone gets the idea. Notice $page_header though. in header.php, the last line is: <div class='page_header'><?php echo $page_header; ?></div> <-- $page_header is a big bold text in the page. So now that you fully get the idea ($page_header being 'My Blog', 'Contact Me', etc), here's the problem. There's a session now. i start the session in header.php. So now im trying to do something like this: <?php $page_title = 'welcome to my site'; $page_header = "Welcome back, $_SESSION[username]"; // session isnt being echoed because i start it in header.php include('./includes/header.php'); // i do session_start() here, therefore the $_SESSION['username'] in line above isn't being echoed. echo 'just some content...'; include('./includes/footer.php'); ?> Few solutions i know of: 1. do a session_start() on the page i want to echo the session and put a @ in front of session_start() in header.php 2. instead of having this line: <div class='page_header'><?php echo $page_header; ?></div> in the header, just include it in every page after the header. However, the first solution seems sort of sloppy and the second one too inconvenient. I was wondering if there's something else I could do. Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/ Share on other sites More sharing options...
pocobueno1388 Posted November 7, 2007 Share Posted November 7, 2007 You could sacrifice the personal touch using their username, and just put "Welcome Back!"...but that all depends on where exactly you use the $page_header variable. I'm not sure if there is another way to do it other than the ways you listed, and I'm not really liking either way. You could store their username in a cookie? Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/#findComment-386433 Share on other sites More sharing options...
cooldude832 Posted November 7, 2007 Share Posted November 7, 2007 look at scope of variables, you don't need the session in your header you need your session in your parent page when I build a page using php to "chop" my layout I do this: I make a shell file that has all the areas defined i.e <?php //Shell File session_start(); //Any auto declares like require_once("functions.php"); or libraries costants, etc require("header.php"); require("navi.php"); require($page_content.".php"); require("footer.php"); ?> Then when I make a page I simply write <?php $page_content= "page1"; $page_title = "Page 1"; //etc ?> Now you ask why am I saying this, because now you have your session in your parent doc (So no output issues), but secondly your session data is made before you want to access it Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/#findComment-386440 Share on other sites More sharing options...
play_ Posted November 8, 2007 Author Share Posted November 8, 2007 pocobueno: yes thats another option cooldude: Trying to understand your code here. You would have to include the 'shell' file in every page, after you declare $page_content, $page_title, etc. would you not? like <?php $page_content= "page1"; $page_title = "Page 1"; require('./shellfile.php'); ? ]/php] Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/#findComment-387274 Share on other sites More sharing options...
teng84 Posted November 8, 2007 Share Posted November 8, 2007 can you explain it a bit more ? Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/#findComment-387279 Share on other sites More sharing options...
cooldude832 Posted November 8, 2007 Share Posted November 8, 2007 pocobueno: yes thats another option cooldude: Trying to understand your code here. You would have to include the 'shell' file in every page, after you declare $page_content, $page_title, etc. would you not? like <?php $page_content= "page1"; $page_title = "Page 1"; require('./shellfile.php'); ? ]/php] [/quote] Yeah thats the idea basically, and then in the shell file the content area is loaded via that $page_content and the title is declared via the above st ated var. That or I make a 2 piece shell a top/bottom that is above/below my content area and just do [code] <?php $page_title = "title"; require_once("shell_top.php"); //Content require_once("shell_bottom.php"); ?> That method I like better as its well easier to use for me, and itshould solve the issue stated above. Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/#findComment-387309 Share on other sites More sharing options...
pocobueno1388 Posted November 8, 2007 Share Posted November 8, 2007 I think the best solution is going to be a cookie. You don't have to include anything on every page and what not. Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/#findComment-387321 Share on other sites More sharing options...
Aureole Posted November 8, 2007 Share Posted November 8, 2007 Session start needs to come before anything else, always.. Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/#findComment-387346 Share on other sites More sharing options...
play_ Posted November 9, 2007 Author Share Posted November 9, 2007 I'll go with the slopyness of inserting a @ right before session_start() thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/76323-solved-echoing-a-variable-before-its-declaration/#findComment-388031 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.