Session problems

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
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Session problems

Post by Luis Almeida »

I have this two pages

test3.php

Code: Select all

<?php
	session_start();
	session_register("valor");
	$valor=session_id();
	echo $valor;
	echo '<br><br><a href="test4.php">next page</a>'; 
?>
and

test4.php

Code: Select all

<?php
	session_start();
	echo $valor;

?>
Shouldn´t it write the same value for "$valor" on page test4.php as on page test3.php

this is because on page test3.php it returns (for example):

1e5aa925c880a752d8a8a5f52aea5434

next page

And when I click "next page" Link it returns nothing

Can anyone help me please

thanks in advance
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You shouldn't be using session_register() - it's a deprecated function which does not work with register_globals off.

Instead try:

test3.php

Code: Select all

<?php
session_start();
$_SESSION['valor'] = session_id();

echo $_SESSION['valor'];

?>
<p><a href="test4.php">Test Session</a></p>
and

test4.php

Code: Select all

<?php
session_start();

echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>
Mac
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Post by Luis Almeida »

It Worked jut fine
thank´s twigletmac

But now there is another question

Does it mean that if, for example, I need to make an aritmethic operation on a session variable like :

Code: Select all

<?php
session_start();
$_SESSION['valor'] =10; 
$_SESSION['valor'] =$_SESSION['valor']+1;
?>
Do I have to use always the '$_SESSION' to refer to a session variable???
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

For the example you gave you could rewrite the code:

Code: Select all

$_SESSION['valor'] =$_SESSION['valor']+1;
as

Code: Select all

++$_SESSION['valor'];
which saves a bit of typing.

If, however, you need to do a lot of work with a session variable you can easily define it as a new variable before you start and then when you're finished with the calculations set the resulting value back to the session variable, e.g.:

Code: Select all

<?php 
session_start();

// create the temporary variable
$valor = $_SESSION['valor'];

// code working with $valor

// finished calculations set $_SESSION['valor'] to new value
$_SESSION['valor'] = $valor;
?>
Mac
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Session problems

Post by Luis Almeida »

Thank's a lot
You have been a great help
Post Reply