Jump to content

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/46154-solved-using-session-control-in-php/
Share on other sites

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();
?>

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

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! :P

 

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. :o

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.