FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » simple session question
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: simple session question [message #175696 is a reply to message #175695] Fri, 21 October 2011 18:19 Go to previous messageGo to previous message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma:
Senior Member
cerr schrieb:

> if(!isset($_SESSION['stp'])) {
> session_start();
> $_SESSION['stp'] = 0;
> echo "session started<br>\n";
> }

You need to call session_start() on *every* page request, not just when
you initialize the session (think of it as
"session_start_or_continue()"). The $_SESSION superglobal does not exist
until you have called session_start(), so your code above won't work.

> switch($_SESSION['stp']) {
> case 0: $_SESSION['stp'] = $_SESSION['stp']++;
> echo "stp:".$_SESSION['stp'];
> break;
> case 1: $_SESSION['stp'] = $_SESSION['stp']++;
> echo "stp:".$_SESSION['stp'];
> break;
> case 2: $_SESSION['stp'] = $_SESSION['stp']++;
> echo "stp:".$_SESSION['stp'];
> $_SESSION['stp'] = 0;
> break;

You're missing a } here. But even with that and the session_start()
issue fixed, it always prints 0. I'm not quite sure why, but anyway your
code is unnecessarily complicated. Here's my version:

session_start();
if ( !isset( $_SESSION['stp'] ) )
{
$_SESSION['stp'] = 0;
echo "session started<br>\n";
}

echo $_SESSION['stp'];
$_SESSION['stp'] = ( $_SESSION['stp'] + 1 ) % 3;

I use the modulo operator % to achieve the cycling through the values 0,
1, 2.

Greetings,
Thomas


--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: session cookie: client side
Next Topic: by get this format my explode file name like this through php
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 22 01:50:11 GMT 2024

Total time taken to generate the page: 0.04415 seconds