Sharing session variables

This is a discussion on Sharing session variables within the PHP Language forums, part of the PHP Programming Forums category; How can I share session registered variables between different processes? Here is my problem: File 1: ----------------------------------------------------------------------- <?php session_start(); $var1=&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-02-2004
Mladen Gogala
 
Posts: n/a
Default Sharing session variables

How can I share session registered variables between different processes?
Here is my problem:
File 1:
-----------------------------------------------------------------------
<?php
session_start();
$var1="This should";
$var2="be displayed";
$_SESSION[ 'var1' ]=$var1;
$_SESSION[ 'var2' ]=$var2;
if (session_is_registered('var1')) {
print "Var1 is registered<br>";
}
if (session_is_registered('var2')) {
print "Var2 is registered<br>";
}
print "<A HREF=/work/test2.php>Second File</A>";
?>


File 2:
-----------------------------------------------------------------------
<?php
if (session_is_registered('var1')) {
print "test2:Var1 is registered<br>";
}
if (session_is_registered('var2')) {
print "test2:Var2 is registered<br>";
}

$var1=$_SESSION[ 'var1' ];
$var2=$_SESSION[ 'var2' ];
print "$var1 $var2 <br>";
?>


In file test2.php neither variable is reported as a session registered
variable and nothing is displayed. File /tmp/sess* has the correct
contents:
var1|s:11:"This should";var2|s:12:"be displayed";

How can I establish real global variables, ones which will
be visible in both files? I thought that httpd was supposed
to read the sessxxxxx file, depending on the cookie received from
the browser and establish those global variables.
I apologize if it is a beginer question, but I was unable to
answer it either through the O'Reilly book or PHP online literature.
I'm at the verge of re-installing PHP 4.3.36 with shmop module enabled.
--
Trust me, I know what I'm doing. (Sledge Hammer)

Reply With Quote
  #2 (permalink)  
Old 06-02-2004
StinkFinger
 
Posts: n/a
Default Re: Sharing session variables

make sure you add:
session_start();
and the top of each page that you want to access the variables in.

"Mladen Gogala" <gogala@sbcglobal.net> wrote in message
news:pan.2004.06.02.04.49.57.186315@sbcglobal.net. ..
> How can I share session registered variables between different processes?
> Here is my problem:
> File 1:
> -----------------------------------------------------------------------
> <?php
> session_start();
> $var1="This should";
> $var2="be displayed";
> $_SESSION[ 'var1' ]=$var1;
> $_SESSION[ 'var2' ]=$var2;
> if (session_is_registered('var1')) {
> print "Var1 is registered<br>";
> }
> if (session_is_registered('var2')) {
> print "Var2 is registered<br>";
> }
> print "<A HREF=/work/test2.php>Second File</A>";
> ?>
>
>
> File 2:
> -----------------------------------------------------------------------
> <?php
> if (session_is_registered('var1')) {
> print "test2:Var1 is registered<br>";
> }
> if (session_is_registered('var2')) {
> print "test2:Var2 is registered<br>";
> }
>
> $var1=$_SESSION[ 'var1' ];
> $var2=$_SESSION[ 'var2' ];
> print "$var1 $var2 <br>";
> ?>
>
>
> In file test2.php neither variable is reported as a session registered
> variable and nothing is displayed. File /tmp/sess* has the correct
> contents:
> var1|s:11:"This should";var2|s:12:"be displayed";
>
> How can I establish real global variables, ones which will
> be visible in both files? I thought that httpd was supposed
> to read the sessxxxxx file, depending on the cookie received from
> the browser and establish those global variables.
> I apologize if it is a beginer question, but I was unable to
> answer it either through the O'Reilly book or PHP online literature.
> I'm at the verge of re-installing PHP 4.3.36 with shmop module enabled.
> --
> Trust me, I know what I'm doing. (Sledge Hammer)
>



Reply With Quote
  #3 (permalink)  
Old 06-02-2004
Mladen Gogala
 
Posts: n/a
Default Re: Sharing session variables

On Tue, 01 Jun 2004 22:17:24 -0800, StinkFinger wrote:

> make sure you add:
> session_start();
> and the top of each page that you want to access the variables in.


Thanks a lot. That was it.

--
Trust me, I know what I'm doing. (Sledge Hammer)

Reply With Quote
  #4 (permalink)  
Old 06-03-2004
FLEB
 
Posts: n/a
Default Re: Sharing session variables

Regarding this well-known quote, often attributed to Mladen Gogala's famous
"Wed, 02 Jun 2004 00:49:58 -0400" speech:

> How can I share session registered variables between different processes?
> Here is my problem:
> File 1:
> -----------------------------------------------------------------------
> <?php
> session_start();
> $var1="This should";
> $var2="be displayed";
> $_SESSION[ 'var1' ]=$var1;
> $_SESSION[ 'var2' ]=$var2;
> if (session_is_registered('var1')) {
> print "Var1 is registered<br>";
> }
> if (session_is_registered('var2')) {
> print "Var2 is registered<br>";
> }
> print "<A HREF=/work/test2.php>Second File</A>";
> ?>
>
>
> File 2:
> -----------------------------------------------------------------------
> <?php
> if (session_is_registered('var1')) {
> print "test2:Var1 is registered<br>";
> }
> if (session_is_registered('var2')) {
> print "test2:Var2 is registered<br>";
> }
>
> $var1=$_SESSION[ 'var1' ];
> $var2=$_SESSION[ 'var2' ];
> print "$var1 $var2 <br>";
> ?>
>
>
> In file test2.php neither variable is reported as a session registered
> variable and nothing is displayed. File /tmp/sess* has the correct
> contents:
> var1|s:11:"This should";var2|s:12:"be displayed";
>
> How can I establish real global variables, ones which will
> be visible in both files? I thought that httpd was supposed
> to read the sessxxxxx file, depending on the cookie received from
> the browser and establish those global variables.
> I apologize if it is a beginer question, but I was unable to
> answer it either through the O'Reilly book or PHP online literature.
> I'm at the verge of re-installing PHP 4.3.36 with shmop module enabled.


Be sure to session_start() the session in test2.php. The session_start()
function is a bit misnamed. You use it to start a new session but you also
use it to continue an old one.

http://us3.php.net/manual/en/function.session-start.php

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:47 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0