Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-29-2008, 06:15 AM $_SESSION problem.
Experienced Talker

Posts: 33
Name: Toby Osbourn
Trades: 0
Basically I want to set up a simple session that will tell me what the last page someone was on was (so like $_SESSION['lastpage']="home"; would be on the homepage, or whatever).

Only for some reason the system (something I am fixing up that someone else has written) doesn't want to play ball. They use sessions and they seem to work fine, however any that I try and call myself just don't work.

Here is sessions.php which gets called on every page that uses sessions, but I don't see how this could effect what I am trying to do.

PHP Code:
<?php 
require_once("dbfactory.php");
define("STAMP_FORM""YmdHis"); // Default timestamp format.

function sess_connectDB(){
    
/*
        function to connect to the backend database with local db defined parameters.
    */
    
try  {
        
$dbh = new DBFactory(); 
        return 
$dbh;
    }
    catch(
ADODB_Exception $e){
        
header("Location: nodb.php");
        exit;
    }
}
function 
sopen($save_path$session_name){    
    
/*
        No need to do anything here.
    */
    
return true;
}
function 
sclose(){
    
/*
        No need to do anything here.
    */
    
return true;
}
function 
sread($id){
    
/*
        Reads session data from the database
    */
    
$dbh sess_connectDB();
    
$sql "SELECT * FROM `TS::Sessions` WHERE sid='$id'";
    
$dbh->SetFetchMode("ASSOC");
    
$rs $dbh->Execute($sql);
    
$row $rs[0];//->fields;    
    //print_r($row);
    
return $row['sdata'];
}
function 
swrite($id$sess_data) {
    
/*
        Writes session data to the database
    */
    
try {
        
$dbh sess_connectDB();
        
$clean_data $dbh->qstr($sess_data);
        
$timestamp date(STAMP_FORM);
        
$sql "DELETE FROM `TS::Sessions` WHERE sid='$id'";
        
$dbh->Execute($sql);
        
$sql "INSERT INTO `TS::Sessions` (sid, sdata, modtime) VALUES('$id', $clean_data, '$timestamp')";
        
$dbh->Execute($sql);
        
//printr($sql);
    
}
    catch (
Exception $e){
        
var_dump($e);
    }
}
function 
sdestroy($id){
    
/*
        We are finished with the db, so wipe the session
    */
    
$dbh sess_connectDB();
    
$dbh->Execute("DELETE FROM `TS::Sessions` WHERE sid='$id'");
    
$_SESSION= array();
}
function 
s_gc($maxlifetime){
    
/*
        Garbaage collection.
    */
    
$dbh sess_connectDB();
    
$ts time() - $maxlifetime;
    
$timestamp date(STAMP_FORM$ts);
    
$dbh->Execute("DELETE FROM `TS::Sessions` WHERE modtime < '$timestamp'");
}

// Make PHP use *our* session handling.
if (session_set_save_handler
  
"sopen"
  
"sclose"
  
"sread"
  
"swrite"
  
"sdestroy"
  
"s_gc" 
)===false){
    die(
"Sessions are broken");
}

// As this will be included in any page that wants session access, we might as well start it here.
session_start();
Any suggestions?
tosbourn is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-29-2008, 08:44 AM Re: $_SESSION problem.
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
On many servers, session_start() belongs at the very top of the page, in fact it won't work if it isn't the very first line, and isn't on a line by itself, with not even a space after the semi-colon.

But besides that, all I see you doing is stuff with the database. Shouldn't you just be using the $_SESSION[] superglobal, as your title seems to indicate? As far as I am aware, session_start() is only used when you need access to it.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 08-29-2008 at 08:46 AM..
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-29-2008, 10:02 AM Re: $_SESSION problem.
Experienced Talker

Posts: 33
Name: Toby Osbourn
Trades: 0
Cheers for the advice, I have got it sorted, I needed to include the sessions.php file in the tester.php - I didn't think this was necessary because of the global nature of sessions. Oh well! Cheers everyone.
tosbourn is offline
Reply With Quote
View Public Profile
 
Old 08-29-2008, 11:48 AM Re: $_SESSION problem.
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
On many servers, session_start() belongs at the very top of the page, in fact it won't work if it isn't the very first line, and isn't on a line by itself, with not even a space after the semi-colon.
What??? The only requirement for session_start() is that it be called BEFORE any output is sent through the buffer. It would be impossible for modern apps to have sessions start the very first line because scripts require to include function and class files first, and in some cases a connection to the DB.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-29-2008, 01:14 PM Re: $_SESSION problem.
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
What???
Maybe it is not as widespread as I suggested. However, I have encountered this problem, and the only thing I could do on this one server, was to place the session_start() on a line by itself, before any other code, and A SINGLE SPACE after the semi-colon would mess up all my sessions. I wouldn't be saying this if I hadn't witnessed it with my own eyes, because I know, it makes zero sense. Since then, it has never happened to me, but I live in fear of it, I suppose, because it did happen. Has this ever happened to anyone else?
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 08-29-2008 at 01:15 PM..
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to $_SESSION problem.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25962 seconds with 12 queries