Hi

I'm trying to use a function to set a session variable. I have three files:

The first file has

<?php session_start(); // This connects to the existing session

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p> <a href="functions.php?FuncToExec=countrySelectUS">Execute countrySelect function</a></p>

</body>
</html>

I then have a functions.php file:

<?php session_start();
session_register ("country");
$HTTP_SESSION_VARS ["country"] = $country;
$country="UK"; //default
?>

<?php
if($FuncToExec == "countrySelectUS"){
countrySelectUS();
}?>

<html>
<head>
<meta http-equiv="refresh" content="12; URL=thiscountry.php">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
function countrySelectUS() {
$country="US";
echo "new country is: " . $country;
}
?>
</body>
</html>

And finally thiscountry.php

<?php session_start(); ?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
You are in
<?php echo $country ?>

</body>
</html>

However when i click on the link in the first page, the functions.php page displays saying
'new country is: US'

but the final page gets displayed with:
'You are in UK'

The function is obvioulsy being run, but for some reason the change in value for country isnt being 'stored' as part of the session, only the value assigned when its created.

Thats not how I was understanding they should work 🙁

Can anyone explain what I have done wrong?

Many thanks for any help given

Pete

    Hi there,

    try this code for the functions page,, should work 🙂

    <?php session_start(); 
    #session_register ("country"); 
    $_SESSION['country'] = $country; 
    $country="UK"; //default 
    ?> 
    
    <?php 
    if($FuncToExec == "countrySelectUS"){ 
    countrySelectUS(); 
    }?> 
    
    <html> 
    <head> 
    <meta http-equiv="refresh" content="12; URL=thiscountry.php"> 
    <title>Untitled Document</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    </head> 
    
    <body> 
    <?php 
    function countrySelectUS() { 
    #$country="US"; 
    echo "new country is: " . $_SESSION['country']; 
    } 
    ?> 
    </body> 
    </html> 

    HTH

    lozza

      Check to see of REGISTER_GLOBALS is disabled in your PHP config. If so you can only use the $SEESION['myval'] form for session variables. Furthermore you do not need to use session_register($name); as all you need to do is declare a session variable by reference: $SESSION['name'] = ' myname';

        Hiya

        Seemingly my host says that REGISTER_GLOBALS is on?

        Although, surely if it wasnt, would I get the 'you are in the uk' as 'uk' is being stored as a session variable?

        Pete

          Write a Reply...