PHPBuilder - PHP4: Session Management from within your own functions



RSS Twitter
Snippets Http

PHP4: Session Management from within your own functions

by: Peter Verhagen
|
August 23, 2001

Version: 0.001a

Type: Sample Code (HOWTO)

Category: HTTP

License: BSD License

Description: I've been wanting to create session information from within a function, trying to keep code clean. This example depicts one? method of passing the current session onto the page, utilizing a seperate file to hold all session functions.



========================================================
[FILE: mysessions.php]
--------------------------------------------------------
<?php

function mysession_start($mysid)
{

 if(!$mysid)
 {
    session_start();
    $mysid = session_id();
 }

 return($mysid);

}

function mysession_register($var, $mysid)
{
  session_id($mysid);
  session_start();

  if(!stupid)
  {  $stupid = $var;
     session_register("stupid");
  }
  else
  {  session_unregister("stupid");
     $stupid = $var;
     session_register("stupid");
  }
}

?>
========================================================



========================================================
[FILE: p1.php]
--------------------------------------------------------
<?php
 include("mysessions.php");

 $id = mysession_start($id);

 print "$stupid<BR> ";

 $stupid += 1;

 mysession_register($stupid, $id);

 print "<a href=\"p2.php?$id\">on to page 2</a> <br><br>";

?>
========================================================


========================================================
[FILE: p2.php]
--------------------------------------------------------
<?php
 include("mysessions.php");

 $id = mysession_start($id);

 print "I'm supposed to have access to my stored variable: $stupid <BR> ";
 print "Lets change the variable <BR> ";

 $stupid += 1;
 mysession_register($stupid, $id);

 print "<a href=\"p1.php?$id \">back to page 1</a>";

?>
========================================================

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Peter Verhagen

Comment:



Comment:

(Maximum characters: 1200). You have characters left.