Connecting Tech Pros Worldwide Help | Site Map

Problems passing variables to include file.

Newbie
 
Join Date: Oct 2008
Location: USA
Posts: 14
#1: Nov 3 '08
I don't know why I cannot get this to work. I want to include an external file in a script that produces an HTML table and is populated based on a few variables from the calling script. I thought php compiled in line, but after having no luck I tried executing the script by calling it by itself and it worked correctly filling itself with the correct SESSION variables. Maybe it's my syntax but I have tried Session variables, I think I used global correctly but I am not sure. I need to pass two items,
[PHP]
$imagenumber; // an image number in a variable called
$_SESSION["directorylist"]; // (Which I split to get the correct path to the image)
[/PHP]
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Nov 3 '08

re: Problems passing variables to include file.


If you're including a file, that file inherits the variables of it's parent. Consider this:

file1.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. define("HELLO", "Hello from file1");
  4.  
  5. include("file2.php");
  6.  
  7. ?>
  8.  
and in file2.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. echo HELLO;
  4. // would print: Hello from file1
  5. ?>
  6.  
Newbie
 
Join Date: Oct 2008
Location: USA
Posts: 14
#3: Nov 3 '08

re: Problems passing variables to include file.


file1.php
[PHP]
define("IMAGENUMBERCONST",$_GET["imagenumber"]);[/PHP]

And what's going on with the SESSION variable? I initialized my session but it doesn't want to pass the $_SESSION["home"] variable?

include("file2.php");
[PHP]
<? ini_set("session.cookie_domain",substr($_SERVER[HTTP_HOST],3));
session_start();
$_SESSION["home"];
$dirlist = split ('\/', $_SESSION["home"]);

// ***ABBREVIATED******
echo'<img HEIGHT="100", WIDTH="60" src="http://www.website.com/';
echo "$dirlist[1]/$dirlist[2]/tn_";
echo IMAGENUMBERCONST;
echo'">';
?><html>
</table>
[/PHP]
This gives me the incorrect path to the image:
http://www.website.com///tn_IMAGENUMBERCONST
As you can see the directories aren't present and IMAGENUMBERCONST is a literal.
Newbie
 
Join Date: Oct 2008
Location: USA
Posts: 14
#4: Nov 3 '08

re: Problems passing variables to include file.


IMPORTANT THING TO REMEMBER ABOUT THE include("xxx"); function!
I was using the URL as a path to call the script when I should have been using the relative server path ie:
[PHP] include("/home/4/4/a/2769/2769/public_html/file2.php"); [/PHP]

NOT [PHP]("http://www.website.com/file2.php");[/PHP]
The relative path works fine for including html for headers and footers and such but I guess all SESSION variables are not available when using this path even if you use [PHP]<? session_start();[/PHP]
Which, by the way, I DID NOT have to use (session_start();)in the included file's head.
Thanks for the insight!
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#5: Nov 3 '08

re: Problems passing variables to include file.


I don't see the point in using SESSION, as you're not actually leaving the page. But I think I'm not understanding something here..
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439
#6: Nov 3 '08

re: Problems passing variables to include file.


could you please first change your include script path
from this:
Expand|Select|Wrap|Line Numbers
  1. include("/home/4/4/a/2769/2769/public_html/file2.php");
to this:
Expand|Select|Wrap|Line Numbers
  1. include("file2.php");
As I can see, you are using a shared hosting server right? (may not be too)
anyway no need to setup your path like /home/.... just give the relative path as i suggested.

let me have a closer look to your problem, as I also not sure about your question.
Reply