You are here: Re: Problem with session variables? « PHP Programming Language « IT news, forums, messages
Re: Problem with session variables?

Posted by Hilarion on 10/25/05 14:15

> I'm confused now as i thought
>
> $HTTP_SESSION_VARS ["country"] = $country;
>
> meant that i could refer to the session variable as $country ?

Nope. It means that you store value of $country variable under "country"
name. You are also using here an old way of accessing session values
(you are using $HTTP_SESSION_VARS instead of $_SESSION).

What is making $country refer to session is session_register function
(which will NOT work if register_globals is turned off, which means
most PHP servers).


> Also I get 'uk' from $country in the 'thiscountry.php' which i thought
> suggested that $country was refering to the session variable?

It's a result of using session_register.


> However, I did what you suggested and changed functions so it now looks
> like:
>
> <?php session_start();
> session_register ("country"); // Create a session variable called name
> $HTTP_SESSION_VARS ["country"] = $country;
> $country="UK";
> ?>
> <?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() {
> $_session['country']="US";

This should be $_SESSION, not $_session. Variable names are case sensitive
in PHP.


> echo "new country is: " .$_session['country'];
> }
> ?>
> </body>
> </html>
>
> However, I still get the same problem, I click on the link, functions.php
> displays showing 'new country is: US' and then I move automatically to
> thiscountry.php where it says 'You are in UK'
>
> which I dont understand :(

It's because you used $_session variable, which is not the one you
should.

In general you should not use session_register but use $_SESSION array:


<?php
session_start();
if (!isset($_SESSION['country'])
{
$_SESSION['country'] = 'UK';
}

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()
{
$_SESSION['country'] = 'US';
echo 'new country is: ' .$_SESSION['country'];
}
?>
</body>
</html>

I also do not understand why do you use such a complicated way of selecting
countries. I would use something like this (one single file, where
first part could be an include file included in every page):

<?php
error_reporting( E_ALL );
session_start();
$countries = array( 'US', 'UK', 'PL' );
if (isset( $_GET['country'] ) && in_array( $_GET['country'], $countries ))
{
$_SESSION['country'] = $_GET['country'];
}
else if (!isset($_SESSION['country'])
{
$_SESSION['country'] = 'UK';
}
?>
<html>
<head>
<title>Untitled Document</title>
</head>

<body>
You are in
<?php
echo htmlspecialchars( $_SESSION['country'] );
?>.<br />
<br />
Select country:
<?php
$link = $_SERVER['PHP_SELF'] . '?country=';
$output = array();
foreach( $country in $countries )
{
if ($country == $_SESSION['country'])
{
$output[] = '<b>' . htmlspecialchars( $country ) . '</b>';
}
else
{
$output[] = '<a href="' . htmlspecialchars( $link . $country ) . '">'
. htmlspecialchars( $country )
. '</a>';
}
}
echo implode( ', ', $output );
?>
</body>
</html>


Hilarion

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация