Killersites.com Homepage

Print at May 22, 2013 12:53:16 AM
Posted by wolfrock at Nov 28, 2006 1:45:47 AM
Recalling variable in a separate .php section?
I've been working with multiple different pages (.php) that all utilize global navigation and footer .php files, since they are essentially the same for every page on the site.

I'm trying to define a simple numeric variable within the content of the main page(s) (basically one variable that might be different from page to page), and then recall/print that variable within the "footer" section (a separate PHP file). This seemed to work OK using the most basic variable commands when I was testing the site on my computer, but after uploading to a server, the basic commands for defining & printing a variable don't seem to be doing the trick.

Any advice is greatly appreciated... just trying to get the numeric variable to print within the footer .php file, even though it was defined elsewhere.

Thanks a lot! cool

Posted by Robbkore at Nov 29, 2006 9:01:35 AM
Re: Recalling variable in a separate .php section?
That all depends on how you plan on using the variable.

You could create a constant (value that DOES NOT CHANGE) using the define function:
define(STATE, 'pa');

You could use a regular php variable if you wanted to apply some logic or CHANGE the value of the variable on a page basis:
$state = 'pa';

OR if you wanted to do something like a visit counter or the like you could use a Database or flat file (text file) to store a value and edit it.


Ideally you would include a file with the variables ABOVE where you wanted to reference them in the code. When defining a variable or constant in an INCLUDED file the variable becomes accessible to ANY file which includes it.

This can be a bit tricky so I'll give you a quick example.
Say we have index.php it looks like this:

include('vars.php');
include('header.php';
include('main.php');
include('footer.php');


Now IN THE INDEX.PHP PAGE, any of the included files can access your vars from vars.php, HOWEVER, if you tried pulling up just the footer and did not include the vars.php file in the footer itself you could not access the vars anymore. I, hope that makes sense.




Hope that helps.

Posted by wolfrock at Nov 29, 2006 3:07:04 PM
Re: Recalling variable in a separate .php section?
Thanks for the reply!
I ended up getting this to work by defining a session variable.

used this code at the very top of both the index pages & footer:
<?php session_start(); ?>

defining the variable within the index page's code:
<?php $_SESSION['variable'] = whatever; ?>

recalling & displaying it inside the footer:
<?php echo $_SESSION['variable']; ?>

Also, the primary way I resolved the problem of the variable not showing up at all was by putting the footer.php on the same server as the rest of the index pages. The variable would show up every time if the footer.php was included like this:

<?php @include('footer.php'); ?>

but I couldn't get it to show up if it was included like this:

<?php @include('http://www.yourserver.com/footer.php'); ?>

Posted by admin at Nov 29, 2006 3:37:05 PM
Re: Recalling variable in a separate .php section?
Yep,

If you want to persist data between pages for a particular user then using sessions is the way to go - that's why the were invented in the first place.
----------------------------------------
Stefan Mischook

Video Tutorial Store | Web Templates