Sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
OlezhkaPoi
Forum Newbie
Posts: 6
Joined: Fri Oct 24, 2003 12:25 pm
Contact:

Sessions

Post by OlezhkaPoi »

I have got an array of data to store in session. But when the data is already in session, after next action it's dissapeared.

Have some of you some decisions, please tell me. :)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Make sure you have session_start(); at the top of every page that you will be using session variables on.
jgoluch
Forum Newbie
Posts: 5
Joined: Tue Oct 21, 2003 9:38 am

Post by jgoluch »

Try this sample code to get a feel for how it works. I tried several other examples I found in the message posts and none of them worked. Remember that if you're not destroying sessions during development, it's probably a good idea to exit and restart your browser when changing session related code. Hope this helps

Code: Select all

<?php
session_start();
header("Cache-control: private");
?>

<html>
<head>
<title></title>
</head>
<body>

<?php
if($sessVar == 0)
&#123;
   session_register('sessVar');
   $sessVar = 1;
   $sArr = array(0,0,0,0);
   session_register('sArr');
&#125;
else
&#123;
   $sessVar++;
   for($i=0; $i<sizeof($sArr); $i++) &#123;
      $sArr&#1111;$i] += $i+1;
   &#125;
&#125;

for($i=0; $i<sizeof($sArr); $i++) &#123;
   echo "sArr&#1111;".$i."]= ".$sArr&#1111;$i].", ";
&#125;
echo ("<p><br>");
echo "sessVar = ".$sessVar."<p><br>";
?>
<a href="sesstest.php">Test</a>
</body>
</html>
OlezhkaPoi
Forum Newbie
Posts: 6
Joined: Fri Oct 24, 2003 12:25 pm
Contact:

Sessions

Post by OlezhkaPoi »

Thanks for advice, but it's still wiping the Arrays values. I think it must be something wrong with a php.ini. If you have some decisions, please write me!
:roll:
OlezhkaPoi
Forum Newbie
Posts: 6
Joined: Fri Oct 24, 2003 12:25 pm
Contact:

Post by OlezhkaPoi »

Who knows why does session works locality, and doesn't work remoteness?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try your script with

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
session_start();

[...remaining part of your script...]

session_write_close();
?>
if this doesn't work, what does

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);

session_start();
$sessionIdA = session_id();
@$_SESSION['counter'] += 1;
session_write_close();
// restart session
session_start();
$sessionIdB = session_id();
?>
<html>
	<head>
		<title>session test</title>
	</head>
	<body>
<?php
echo $sessionIdA,'<br />', $sessionIdB, '<br />';
echo 'session counter: ', @$_SESSION['counter'];
session_write_close();
?>
		<a href="<?php echo $_SERVER['PHP_SELF']; ?>">refresh</a>
	</body>
</html>
?
OlezhkaPoi
Forum Newbie
Posts: 6
Joined: Fri Oct 24, 2003 12:25 pm
Contact:

Sessions

Post by OlezhkaPoi »

Has worked! As soon as I have inserted the following code

error_reporting(E_ALL);
ini_set('display_errors', TRUE);

The prevention that the file in a directory/tmp cannot be read as directories do not exist has appeared, or the true way is specified not

Appeared, that I did not have in general such directory then I have created her.
I and thought, that a mistake trifling.

Thanks all for the offered decisions.
:D
Post Reply