Problem with session variables

This is a discussion on Problem with session variables within the PHP Language forums, part of the PHP Programming Forums category; Hi folks I'm new to php an currently trying to insall my first php-Session. I've written the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-26-2006
helloroger@iname.com
 
Posts: n/a
Default Problem with session variables

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>

Reply With Quote
  #2 (permalink)  
Old 02-27-2006
samudasu
 
Posts: n/a
Default 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>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:08 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0