Trium918 Posted April 8, 2007 Share Posted April 8, 2007 Simple Session Example I implemented a set of 3 pages. // page1.php /* The output to this should be "Hello world!" which it is*/ <? session_start(); session_register("sess_var"); $sess_var = "Hello world!"; echo "The content of \$sess_var is $sess_var<br>"; echo "<a href =\ "page2.php\">Next page</a>"; ?> // page2.php /* The output to this should be "Hello world!" also but some how $sess_var looses it value*/ <? session_start(); echo "The content of \$sess_var is $sess_var<br>"; session_unregister("sess_var"); echo "<a href =\ "page3.php\">Next page</a>"; ?> // page3.php /* The output to $sess_var in this script is nothing because session_unregister("sess_var"); is called in page2.php and destroyed in page3.php*/ <? session_start(); echo "The content of \$sess_var is $sess_var<br>"; session_destroy(); ?> I donnot understand why the variable in page2.php looses it's value. Could someone shead some light on this? Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/ Share on other sites More sharing options...
pocobueno1388 Posted April 8, 2007 Share Posted April 8, 2007 Try creating the session the new way. <?php session_start(); $_SESSION['sess_var'] = "Hello World!"; $sess_var = $_SESSION['sess_var']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224342 Share on other sites More sharing options...
Trium918 Posted April 8, 2007 Author Share Posted April 8, 2007 Try creating the session the new way. <?php session_start(); $_SESSION['sess_var'] = "Hello World!"; $sess_var = $_SESSION['sess_var']; ?> In which page? Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224344 Share on other sites More sharing options...
wildteen88 Posted April 8, 2007 Share Posted April 8, 2007 You using old coding techniques In order for your code to work you will need to turn register_globals on. Turning this setting on is not recommended though as there is a reason why it is off by default. In stead what you should do is this: page1.php <?php session_start(); /* old and depreciated session_register("sess_var"); $sess_var = "Hello world!"; */ // New way $_SESSION['sess_var'] = 'Hello World'; echo "The content of \$_SESSION['sess_var'] is $_SESSION['sess_var']<br>"; echo "<a href=\"page2.php\">Next page</a>"; ?> page2.php <?php session_start(); $sess_var = $_SESSION['sess_var']; echo "The content of \$sess_var is $sess_var<br>"; /* old and depreciated session_unregister("sess_var"); */ unset($_SESSION['sess_var']); echo "<a href=\"page3.php\">Next page</a>"; ?> page3.php <?php session_start(); $sess_var = $_SESSION['sess_var']; echo "The content of \$sess_var is $sess_var<br>"; session_destroy(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224345 Share on other sites More sharing options...
pocobueno1388 Posted April 8, 2007 Share Posted April 8, 2007 Trium - Wildteen made a small mistake. In her code change this line: $_SESSION['sess_var'] = 'Hello World'] to this: $_SESSION['sess_var'] = 'Hello World'; Just in case you didn't catch it ;] Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224348 Share on other sites More sharing options...
Trium918 Posted April 8, 2007 Author Share Posted April 8, 2007 You using old coding techniques In order for your code to work you will need to turn register_globals on. Turning this setting on is not recommended though as there is a reason why it is off by default. In stead what you should do is this: page1.php <?php session_start(); /* old and depreciated session_register("sess_var"); $sess_var = "Hello world!"; */ // New way $_SESSION['sess_var'] = 'Hello World'; echo "The content of \$_SESSION['sess_var'] is $_SESSION['sess_var']<br>"; echo "<a href=\"page2.php\">Next page</a>"; ?> page2.php <?php session_start(); $sess_var = $_SESSION['sess_var']; echo "The content of \$sess_var is $sess_var<br>"; /* old and depreciated session_unregister("sess_var"); */ unset($_SESSION['sess_var']); echo "<a href=\"page3.php\">Next page</a>"; ?> page3.php <?php session_start(); $sess_var = $_SESSION['sess_var']; echo "The content of \$sess_var is $sess_var<br>"; session_destroy(); ?> Thanks Pocobueno, Wildteen88 What would be a good book. I would like a book on the lastest php4 because I already have one on php5 Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224357 Share on other sites More sharing options...
pocobueno1388 Posted April 8, 2007 Share Posted April 8, 2007 Why would you want to learn php4 techniques when you can just learn php5? It is always good to stay as current as possible. Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224360 Share on other sites More sharing options...
Trium918 Posted April 8, 2007 Author Share Posted April 8, 2007 I will but I don't like it. Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224361 Share on other sites More sharing options...
wildteen88 Posted April 8, 2007 Share Posted April 8, 2007 Trium - Wildteen made a small mistake. In her code change this line: $_SESSION['sess_var'] = 'Hello World'] to this: $_SESSION['sess_var'] = 'Hello World'; Just in case you didn't catch it ;] Umm, I'm a her now lol! Why would you want to learn php4 techniques when you can just learn php5? It is always good to stay as current as possible. It doesn't matter what version you learn as... there is no difference between the two languages. Except PHP5 has better OOP functionality. Thanks Pocobueno, Wildteen88 What would be a good book. I would like a book on the lastest php4 because I already have one on php5 If you have learnt PHP5 then you don't need to learn PHP4. Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224374 Share on other sites More sharing options...
pocobueno1388 Posted April 8, 2007 Share Posted April 8, 2007 -dies laughing- Sorry wildteen88...Whenever I go across your name I automatically think of those commercials for the "girls gone wild" videos, HAHA. Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224379 Share on other sites More sharing options...
wildteen88 Posted April 8, 2007 Share Posted April 8, 2007 -dies laughing- Sorry wildteen88...Whenever I go across your name I automatically think of those commercials for the "girls gone wild" videos, HAHA. I get that a lot on a few communities I go to. Quote Link to comment https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/#findComment-224382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.