Connecting Tech Pros Worldwide Help | Site Map

Problem with session variables

helloroger@iname.com
Guest
 
Posts: n/a
#1: Feb 26 '06
Hi folks

I'm new to php an currently trying to insall my first php-Session. I've
written the following code which uses the 2 variables cat and langua,
but somehow they are not correctly registered in the session. When
pressing the links to set the 2 session variables, they don't move at
all. There values are always 1 and 9.
Does anybody have an idea how I can set the value of a session variable
using a <a href ...>?

Any help appreciated.

Sunnyboy



<?php
session_start();

session_register("cat");
session_register("langua");
?>

<HTML>
<HEAD>
</HEAD>
<BODY>

<?php

echo "langua = $langua<br>";
echo "cat = $cat<br>";

// Read variable langua
if (!isset ($_SESSION["langua"]))
{
echo "langua not registerd in session<br>";
$langua = 1;
}
else
{
echo "langua registerd in session<br>";
$langua = ($_SESSION['langua']);
}

// Read variable cat
if (!isset ($_SESSION["cat"]))
{
echo "cat not registerd in session<br>";
$cat = 9;
}
else
{
echo "cat registerd in session<br>";
$cat = ($_SESSION["cat"]);
}

echo "langua = $langua<br>";
echo "cat = $cat<br>";
?>

<br><br>
<a href="?langua=2">set langua=2</a><br>
<a href="?langua=3">set langua=3</a>
<br><br>

<a href="?cat=3">set cat=3</a><br>
<a href="?cat=4">set cat=4</a>
<br><br>

<?php
echo "cat=$cat<br>";
echo "langua=$langua<br>";

session_write_close();
?>

</BODY>
</HTML>

samudasu
Guest
 
Posts: n/a
#2: Feb 27 '06

re: Problem with session variables


First of all, welcome to PHP. Before starting on sessions, i suggest
doing some more reading on variables "Chapter 12", and especially 12.9
for this particular script in the online edition of the PHP manual.
http://www.php.net/manual/en/language.variables.php

I'm not sure if you're learing from an old tutorial or book but there
have been several changes in the way PHP works compared to previous
versions. Here is one example.
When you check the value of langua after clicking on "<a
href="?langua=2">set langua=2</a>", you'll have to check it by doing
$_GET['langua'] instead of $langua.

Keep it simple when starting out.

page1.php............
<html>
<body>
send a variable to page2<br />
<a href="page2.php?color=blue">send blue</a><br />
<a href="page2.php?color=red">send red</a><br />
</body>
</html>

page2.php.............
<?php
session_start();
// assign color from page 1 into session
$_SESSION['current_color'] = $_GET['color'];
?>
<html>
<body>
<?php
print $_SESSION['current_color'];
}
?>
</body>
</html>

Closed Thread