Jump to content

[SOLVED] Echoing a variable before its declaration.


play_

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

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.