sessions within a function<

liunx

Guest
hi there,

i have a webapp on a page that is controlled by a function in a separate included file. i wish to track the user (ie. assign them a session id). i know that i need to put the session_start() function at the top of the parent page, but where do i check for the existence of the session_id - in the function or outside of the function?

here is the code for the parent page:

<?php
session_start();
header( "Cache-control: private" ) ; // IE 6 fix
?>
<html>
<head>
</head>

<body>
<?php
require_once( "webdbe.inc" );
$mydb = ds_dbopen( "database", "r" );
echo webdbe( $mydb );
?>
</body>
</html>


here is the code from the function webdbe that is defined in the file webdbe.inc:

webdbe.inc

<?php

function webdbe( $db ) {
if( !$webdbe ) {
LOAD OF PHP CODE HERE
} else {
LOAD OF PHP CODE HERE
}


where would i put the following code that assigns the session id? in the function, outside the function or in the parent page?


// Get the session variables
if( isset( $_SESSION['sid'] ) ) {
$sid = $_SESSION['sid'] ;
} else {
$sid = rand() ;
}


thanks in advance.Originally posted by tatlar
hi there,

i have a webapp on a page that is controlled by a function in a separate included file. i wish to track the user (ie. assign them a session id). i know that i need to put the session_start() function at the top of the parent page, but where do i check for the existence of the session_id - in the function or outside of the function?

here is the code for the parent page:

<?php
session_start();
header( "Cache-control: private" ) ; // IE 6 fix
?>
<html>
<head>
</head>

<body>
<?php
require_once( "webdbe.inc" );
$mydb = ds_dbopen( "database", "r" );
echo webdbe( $mydb );
?>
</body>
</html>


here is the code from the function webdbe that is defined in the file webdbe.inc:

webdbe.inc

<?php

function webdbe( $db ) {
if( !$webdbe ) {
LOAD OF PHP CODE HERE
} else {
LOAD OF PHP CODE HERE
}


where would i put the following code that assigns the session id? in the function, outside the function or in the parent page?


// Get the session variables
if( isset( $_SESSION['sid'] ) ) {
$sid = $_SESSION['sid'] ;
} else {
$sid = rand() ;
}


thanks in advance.

I'd suggest putting it in the parent page that way it assigns/checks the id either way.hmmm - I tried that and when I echo out the session id I don't get any output, just a blank.

I have also tried taking it into the function itself, placing it first in the function syntax, but when I echo out the session id I get a different random number every time I move around the application in the same session.Originally posted by tatlar
hmmm - I tried that and when I echo out the session id I don't get any output, just a blank.

I have also tried taking it into the function itself, placing it first in the function syntax, but when I echo out the session id I get a different random number every time I move around the application in the same session.

erm...sorry about that..I think I misread your post...in which case..if you leave it outside of the function do something like


global $sid;

or

$sid = $GLOBALS["sid"];

in the functionhmm, still doesn't work. I have register globals off, so used $sid = $GLOBALS['sid'].

Here is my code in the parent:


session_start();
header( "Cache-control: private" ) ; // IE 6 fix
// Get the session variables
if( isset( $_SESSION['sid'] ) ) {
$sid = $_SESSION['sid'] ;
} else {
$sid = rand() ;
}
$sid = $GLOBALS['sid'] ;


and in the function:

<?php
function webdbe( $db ) {
if( !$webdbe ) {
echo "\t<p><b>Session id: $sid</b></p>" ;
__ LOAD OF PHP CODE HERE
} else {
__ LOAD OF PHP CODE HERE
}
}
?>


Nothing is getting echoed out.
When I put $sid = $GLOBALS['sid'] ; in the top of the function I again get a different session id from within different parts of the application.

Any ideas?

Thanks for the help so far....Originally posted by tatlar
hmm, still doesn't work. I have register globals off, so used $sid = $GLOBALS['sid'].

Here is my code in the parent:


session_start();
header( "Cache-control: private" ) ; // IE 6 fix
// Get the session variables
if( isset( $_SESSION['sid'] ) ) {
$sid = $_SESSION['sid'] ;
} else {
$sid = rand() ;
}
$sid = $GLOBALS['sid'] ;


and in the function:

<?php
function webdbe( $db ) {
if( !$webdbe ) {
echo "\t<p><b>Session id: $sid</b></p>" ;
__ LOAD OF PHP CODE HERE
} else {
__ LOAD OF PHP CODE HERE
}
}
?>



Nothing is getting echoed out.
When I put $sid = $GLOBALS['sid'] ; in the top of the function I again get a different session id from within different parts of the application.

Any ideas?

Thanks for the help so far....

all I can think of right now(I'm half asleep) is:


function webdbe( $db ) {
$sid =& $GLOBALS["sid"];
if( !$webdbe ) {
echo "\t<p><b>Session id: $sid</b></p>" ;
__ LOAD OF PHP CODE HERE
} else {
__ LOAD OF PHP CODE HERE
}
}


I will think more about it later as I said to Low_Overhead I only have 1.5 eyes open at the moment :rolleyes:

edit: also...where is $webdbe set?did you try echoing without that check?I can't see anywhere it's set...

edit2: the reason $sid = $GLOBALS["sid"]; outside of the function didn't work is because it has to be globalized in the function either via the array or using the global ; call.

also...you could try defining it as:


$GLOBALS["sid"]


instead of just $sidthanks willamoose - I appreciate the help.

I did your changes, adding $sid =& $GLOBALS['sid']; within the function. Now I get a different session id on every page when I run the application. Hmmmmmm......

As for where $webdbe comes from:


function webdbe( $db ) {
// Session id
$sid =& $GLOBALS['sid'];
// Get hidden form parameters
$webdbe = $_GET['webdbe'] ;
if( !$webdbe ) {
echo "\t<p><b>Session id: $sid</b></p>" ;
LOAD OF PHP CODE
} else {
echo "\t<p><b>Session id: $sid</b></p>" ;
LOAD OF PHP CODE
}
}


any other ideas?Originally posted by tatlar
thanks willamoose - I appreciate the help.

I did your changes, adding $sid =& $GLOBALS['sid']; within the function. Now I get a different session id on every page when I run the application. Hmmmmmm......

As for where $webdbe comes from:


function webdbe( $db ) {
// Session id
$sid =& $GLOBALS['sid'];
// Get hidden form parameters
$webdbe = $_GET['webdbe'] ;
if( !$webdbe ) {
echo "\t<p><b>Session id: $sid</b></p>" ;
LOAD OF PHP CODE
} else {
echo "\t<p><b>Session id: $sid</b></p>" ;
LOAD OF PHP CODE
}
}


any other ideas?

hmm..sorry about that,I was out of town since friday night...I'll think on it and let you know :)
 
Back
Top