Forum Moderators: coopster
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.
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).
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.
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.