Forum Moderators: coopster

Message Too Old, No Replies

Passing variables to an include

Can variables be used when named in a file called by "include"

         

calvinmicklefinger

3:33 am on Jan 19, 2005 (gmt 0)

10+ Year Member



I am attempting to build a template page that contains content called by the include function. I would like the links in this page to contain tracking variables "exploded" from the URL. One of these variables will be be used in a link on each page, and the other will be used in a single link on a later page.

I have been attempting to use Session variables, but have recently begun to wonder if these variables can be passed to an "include" file.

Here is the code I have been using in a page called "wrapper.php" -

Many thanks.
<?
session_start();
header("Cache-control: private");
session_register("var0");
session_register("var1");
session_register("var2");
$URL_data = explode("/", getenv("REQUEST_URI"));
$_session['var0'] = $URL_data[0] ;
$_session['var1'] = $URL_data[1] ;
$_session['var2'] = $URL_data[2] ;
?>

and later in the same file -

<?php echo "Your session ID is: " . session_id();?>< br>
<?php Empty Field? = <?php echo $var0;?><br>
<?php Affiliate Num? = <?php echo $var1;?><br>
<?php Landing Page? = <?php echo $var2;?><br>
<?php include "http://mydomain.com/mycontent.php";?><br>

The following is contained in my page mycontent.php -

<?php
session_start();
?>
<?php echo 'Empty Array = ' . $_session["var0"];?><br>
<?php echo 'Affiliate Number = ' . $_session["var1"];?><br>
<?php echo 'Page Requested = ' . $_session["var2"];?><br>
The file <?php echo $REQUEST_URI;?> was requested
<br>

All of the variables are populated except the three session variables on the mycontent.php page. I have used the form shown above, and I have called the vriable in the normal manner, as in $var1 or $var2. Neither gave a result.

Will it be possible to use these variables in this manner? If so, what do I need to change?

Many thanks.

mincklerstraat

8:40 am on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem passing session variables to an included file.

You should look into using the name $_SESSION['varname'] instead of $_session['varname'] . Variable names in PHP are case-sensitive, and the docs specify the uppercase version - I'd doubt the lowercase version would work, and if it did, it might change with the next update of PHP since this would fall into the category of 'undocumented behavior'. Putting these in uppercase first, test, and see if it works (haven't looked at the other stuff really).

calvinmicklefinger

12:53 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



I have converted "seesion" to uppercase, with the same result. The variables in the "wrapper.php" document are populated, while the variables in the include file "mycontent.php" are not populated.

Are there any session functions that I have omitted?

mincklerstraat

5:25 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you've already started a session, you don't need to start a session again - your session_start() in your included file might be the culprit here if you're pretty sure that your $_SESSION variables are spelled correctly in the new version.

Remember, included files, even though they're 'new files', are just like extensions of the file you're calling. If you've already done something in the file that the browser calls, you don't have to do it in an included file.

Included files only seem to get dicey in scripts written by relative newbies where they are included inside a function, in which case all variables declared in the file included also only work inside that function, unless they are globalized, but you're not doing this here, so this should be no problem.

So try removing session_start() from the included file. If you also need to call that included file independently, and thus need the session to be started, you could set a variable when you start the session, and in the included file, check to see if that variable is set.

Happy coding.

calvinmicklefinger

5:41 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



I have removed the session_start() with the same results. Is it possible my hosting company needs to change a setting in my php.ini?

mincklerstraat

6:08 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your file probably looks different from how it's posted here, since there are some parse errors here - you don't need so many opening <?php tags - Anyways, I got rid of these, and the following works - notice that I print_r your array here just to show what's coming through -

this is a copy of the code I tried that works:

wrapper.php:


<?
session_start();
header("Cache-control: private");
session_register("var0");
session_register("var1");
session_register("var2");
$URL_data = explode("/", getenv("REQUEST_URI"));
print_r($URL_data);
$_SESSION['var0'] = $URL_data[0] ;
$_SESSION['var1'] = $URL_data[1] ;
$_SESSION['var2'] = $URL_data[2] ;
?>

//and later in the same file -

<?php echo "Your session ID is: " . session_id();?><br>
Empty Field? = <?php echo $var0;?><br>
Affiliate Num? = <?php echo $var1;?><br>
Landing Page? = <?php echo $var2;?><br>
<?php include "mycontent.php";?><br>

This is mycontent.php:


<?php echo 'Empty Array = ' . $_SESSION["var0"];?><br>
<?php echo 'Affiliate Number = ' . $_SESSION["var1"];?><br>
<?php echo 'Page Requested = ' . $_SESSION["var2"];?><br>
The file <?php echo $REQUEST_URI;?> was requested
<br>

Hope this helps, and don't pull all your hair out, everybody encounters bugs, the type of bugs you get just changes as your programming changes / matures.

A little security note: when this script gets closer to prime-time, you'll want to make sure the user input is checked - stuff coming in from the url via "REQUEST_URI" is user input, too.

calvinmicklefinger

6:17 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Thank you micklerstraat,

It seems my biggest culprit was using an absolute URL to call the include file when it was located on my own server.

Many thanks!

mincklerstraat

6:32 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to have been of some assistance, hope you have fun with your script.

mincklerstraat

10:20 am on Jan 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



afterthought, should have seen this earlier:
yes, absolute url's including the 'http' will screw you up with includes -
include "http://mydomain.com/mycontent.php";
will include the output of [mydomain.com...] - you know, what the browser gets, not the code -
you can use absolute references that work, but they need to come from the server root - these are the ones that begin with a slash - like
/home/minck/htdocs/index.php
or
/var/www/index.php

calvinmicklefinger

11:50 am on Jan 20, 2005 (gmt 0)

10+ Year Member



Again Minclerstraat, Thanks.

That helps a lot.

Thanks.