Hi to everyone,
Please i would be grateful if you help me with this. This is a tutorial i am practising .When i create a session variable on a page , I find it difficult to show it on another page. Please check also to see whether the scripts are correct and if you could offer me alternatives.


Here are the excepts for the code:

First script for first page: register_session_var.php

[B]<?php

session_start();

$my="blue";

session_register("my");
echo "<a href='show_session_var.php'>Click here to go to next page</a>";
?>[/B]

Second page: show_session_var.php

[B]<?php
session_start();

echo "My favouite color is...." . $my;
?>[/B]

Hope to get solution soon.Thanks.

Recommended Answers

All 4 Replies

Hi Papa Awortwe,

Looks like you can do it a couple ways... The way I would recommend (mostly because I use it and I find it easier) is as follows:


The first page:

<?php

session_start();
$_SESSION['my'] = 'blue';

echo '<a href="show_session_var.php">Click here to go to next page</a>';

?>


The second page:

<?php

session_start();
echo 'My favouite color is....' . $_SESSION['my'];

?>

I hope this works for you.

Thanks for the great question Papa,

Chrelad

in first page:

<?php

session_start();

$my="blue";
session_unregister("my");
session_register("my");
$_SESSION['my']=$my;
echo "<a href='show_session_var.php'>Click here to go to next page</a>";
?>

in second page:

<?php
session_start();
echo $_SESSION['my'];

?>

in first page:

<?php

session_start();

$my="blue";
session_unregister("my");
session_register("my");
$_SESSION['my']=$my;
echo "<a href='show_session_var.php'>Click here to go to next page</a>";
?>

in second page:

<?php
session_start();
echo $_SESSION['my'];

?>

You don't have to register a session variable if you are using it. I mean, session_register("my") is not necessary if you are using $_SESSION.
Also, note from php.net

Caution
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

source: http://in2.php.net/session_register

ok naveen....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.