Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session question (requrire_once, sesson_start) 1

Status
Not open for further replies.

petersJazz

Programmer
Jan 28, 2002
222
EU
hi,

could you help me with this:

I have a three frame site with header, menu and content. In all frame pages there is:

Code:
require_once('include.php');

and in the include.php there is like:

Code:
if (!is_object($sy1)) {
 session_start();
 ....
 $sy1=new Something();
 write to log file
 ....
}

The problem is that it seams that a new session is started every time I click in a menu item and writes new content. Dont understand why, do you?
 
couple of potential issues.

1. the something class MUST be declared (i.e. known to php) before the session is started if you are storing the something object in the session. this might be a red herring.

2. that's not a great way to start the session!

so try this

Code:
if (!is_class('Someting')){
 require '/path/to/file/with/class/in/it';
}
if (empty(session_id())){ 
 session_start();
}
 
Thank you, I will continue testing this evening. I thought session_start() should be the first command and after that definitions should be placed. So:

if (!is_class('Something'){
require .... for all classes and general functions
}
if (empty(session_id())){
session_start();
$sy1=new Something();
$xy4=new Somethingelse();
}

What I want is the $sy1 and $xy4 to be stable for the whole session. Both in the menu frame and content frame.
 
generally you are right, session_start must be called before you output to the browser and before you use any session functions.
but it also must be AFTER you have referenced any classes, the instances of which you have stored in the session.

there is plenty on this in the manual.


 
I tried to put do this but:

1. is_class is not a known
2. empty() dont understand
3. used is_object() and another check but still session_start is called a lot of times
 
sorry, i used one of my own function calls rather than the built in variant.

use this code instead

Code:
if (!class_exists('Something'){
 require .... for all classes and general functions
}
if (empty(session_id())){
 session_start();
 $sy1=new Something();
 $xy4=new Somethingelse();
}

the empty() function will return true if the variable testes is NULL, is not set, is an empty string, is set to FALSE, is an empty array, is zero or has no value.

session_id() will return the current session_id string. if session_start has not been called then it will return an empty string.
 
Still have problems. How about the different frames, it seams that the act like there own scope.

First index.php is called and it opens up two frames:

1. menu.php
2. page.php

I all three php files there is a require_once 'include.php'.

If I remove it from page.php I get an error.
 
you'd need to show us the code you are using. there is no reason these issues show.

however, i cannot think of a single instance in which i would counsel the use of frames.
 
hi,

here is some code (I have tried to isolate the problem):

[first.php]
<?
require_once 'include.php';
echo('<html><body>');
echo('-- CONFIG = -- '); print_r($config);
echo('<br>-- OB1 = -- '); print_r($ob1);
echo('<br>-- _SESSION = -- '); print_r($_SESSION);
echo('<br>-- _COOKIE = -- '); print_r($_COOKIE);
echo('<br><br><a href="second.php">GO TO SECOND</a>');
echo('</body></html>');
?>

[second.php]
<?
//require_once 'include.php';
echo('<html><body>');
echo('-- CONFIG = -- '); print_r($config);
echo('<br>-- OB1 = -- '); print_r($ob1);
echo('<br>-- _SESSION = -- '); print_r($_SESSION);
echo('<br>-- _COOKIE = -- '); print_r($_COOKIE);
echo('<br><br><a href="first.php">GO TO FIRST</a>');
echo('</body></html>');
?>

[include.php]
<?
require 'object1.php';
session_start();
$config['name']='ysite';
$config['id']=rand(10000,99999);
$ob1=new Object1();
$_SESSION['obj']='FIRST-YYY';
?>

[object1.php]
<?
class Object1 {
var $id;
function Object1() {
$this->id=rand(10000,99999);
}
function getid() {
return $this->id;
}
}
?>

Now I start by opening first.php and the output is:
-- CONFIG = -- Array ( [name] => ysite [id] => 89856 )
-- OB1 = -- object1 Object ( [id] => 14944 )
-- _SESSION = -- Array ( [obj] => FIRST-YYY )
-- _COOKIE = -- Array ( [PHPSESSID] => 560fe18351744c3ae92670428151d2e0 )
GO TO SECOND

I click the link GO TO SECOND and the output is:
-- CONFIG = --
-- OB1 = --
-- _SESSION = --
-- _COOKIE = -- Array ( [PHPSESSID] => 560fe18351744c3ae92670428151d2e0 )
GO TO FIRST

Shouldnt the ob1, config and SESSION follow to second.php?
 
you are not instantiating the session in secondl,php, you have commented out the include.


 
but of course the value of the obj->id will be different since you are creating a NEW object in the include file.

if you want the object to pervade you need to do something like this

Code:
require 'object1.php';
session_start();
$obj = (isset($_SESSION['obj'])) ? $_SESSION['obj'] : new Object1();
$_SESSION['obj'] = $obj;
 
You are of course right. I must have missunderstod the scoping in PHP. What I want to do is to use the same object in first.php and second.php. Do I have to make it $_SESSION['ob1'] to make it global? And another thing, do I have to include the definition of the class on every page for it to be understod?

I really thought that you do session_start(), and after that function and classes are stored in your session and you dont need to redefine them. And I also thought that for example $config['id']='XXX' is a global if it is defined in include.php. And I thought that require_once meant that it is opened just one time per session. I dont find this totally logical and its not well described on php.net.
 
the answer to both questions is yes.

for a variable to persist you need to attach it to the $_SESSION superglobal.

you can assign an object to a session varable, but the class declaration must be known to php before you re-open the session otherwise php will not know what to do with the object. there is a way around this. you can serialise the object and attach the serialisation to the session instead.

require_once and its include friends are a per script rather than per session thing. anything else would lead to date integrity issues.
 
ok, now I works. Is it possible to start mysql connection only once and save connection in session variable. Is it worth doing, will it save time?
 
no. you cannot store resource handles persistently in a persistent session. this includes mysql connection handles, directory handles, file handles etc. you also cannot store circular objects in a session.

the manual sets all this out in the warning on the session introduction
link
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top