Killersites.com Homepage Welcome Guest   |   Register  |  Login
Login Name Password
  Search  
  Index  | Recent Threads  | Unanswered Threads  | Who's Online  | User List  | Help


Quick Go »

No member browsing this thread
Thread Status: Active
Total posts in this thread: 4
[ Jump to Last Post ]
Post new Thread
Author
Previous Thread This topic has been viewed 1874 times and has 3 replies Next Thread
Male wolfrock
Stranger




Joined: Oct 31, 2006
Post Count: 4
Status: Offline
Reply to this Post  Reply with Quote 
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

[Nov 28, 2006 1:45:47 AM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Male Robbkore
Member




Joined: Sep 20, 2005
Post Count: 62
Status: Offline
Reply to this Post  Reply with Quote 
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.
[Nov 29, 2006 9:01:35 AM] Show Printable Version of Post    View Member Profile    Send Private Message    Hidden to Guest [Link] Report threatening or abusive post: please login first  Go to top 
Male wolfrock
Stranger




Joined: Oct 31, 2006
Post Count: 4
Status: Offline
Reply to this Post  Reply with Quote 
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'); ?>
[Nov 29, 2006 3:07:04 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Male admin
Advanced Member
Member's Avatar


Joined: Jun 14, 2003
Post Count: 2940
Status: Offline
Reply to this Post  Reply with Quote 
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
[Nov 29, 2006 3:37:05 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
[ Jump to Last Post ]
Show Printable Version of Thread  Post new Thread