LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2012, 08:08 PM   #1
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Rep: Reputation: 53
Question PHP session variables


Hi. I have a script that assigns an object to a session variable. At the end of the script I var_dump the variable to the effect expected. But the session variable doesn't appear in the file where session variables are stored on the server. I want to use the object when the page reloads, but since it's not registered I can't use it. I would include the code but it involves several files including homemade class files. Help will be appreciated.
 
Old 12-12-2012, 01:43 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What do you mean by session variable? You don't have to paste your whole program, only a few lines, eg:

code1.php:
Code:
<?php
    session_start ();
    $_SESSION['myobject'] = <something>;
?>
code2.php:
Code:
<?php
    session_start ();
    if (!isset ($_SESSION['myobject'])) {
        print ("Where is my object!?\n");
    }
?>

Last edited by NevemTeve; 12-12-2012 at 01:44 AM. Reason: linebreak
 
Old 12-12-2012, 05:38 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Remember that you must update the persistent session-variable store at the conclusion of your HTTP transaction. No matter what language you are using, there will be some required protocol for handling this requirement.

During the next HTTP transaction, whenever it may be, the session-identifier (however it may be passed) will be used as a key to retrieve the session-variables pool from wherever it gets stored in your scenario. If you don't see the values changing, it's a dead giveaway that the values aren't being re-posted to whatever that persistent store is.

Last edited by sundialsvcs; 12-12-2012 at 05:39 PM.
 
Old 12-13-2012, 01:03 PM   #4
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
Originally Posted by NevemTeve View Post
What do you mean by session variable? You don't have to paste your whole program, only a few lines,
Okay, this is my best shot:

Code:
<?php
session_start();

$_SESSION["firstname"]="Barth";
$_SESSION["lastname"]="Jones";
$_POST["password"]="circle7";

//trylogin is a function that returns an object if the login info is valid:
if(!empty($_GET["metadata1"]))if($_GET["metadata1"]=="virginloggingin")	$_SESSION["youmember"]=trylogin($_SESSION["firstname"], $_SESSION["lastname"], md5($_POST["password"]));

//this works (it dumps the whole object):
var_dump($_SESSION["youmember"]);

//but when I reload this page after commenting out the trylogin line, var_dump returns null because $_SESSION["youmember"] wasn't written to the session file;  when it's commented out, $firstname and $lastname are written to the session file, otherwise the session file is written, but empty

//not sure what's going on

?>
 
Old 12-13-2012, 01:32 PM   #5
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
I think I figured it out. I failed to mention that my class returns a "simplexml" object, which I've now discovered can't be "serialized." I guess that also means it can't be written as a session variable. So I suppose I'll go back into my class and reconstruct the simplexml elements as some other kind of object. Thanks for your contributions.
 
Old 12-13-2012, 07:53 PM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi blue.

Please carefully read sundial's post here (#3). I agree with his view that you must invoke the appropriate protocol to store this (persistent) information on the server. In my view, this would lie outside of the functionality of your class.

OK
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about usage of the session variables in php Asha456 Programming 4 01-15-2010 05:02 PM
Are PHP session variables held in memory when you leave a PHP site? Locura Programming 11 11-16-2008 08:37 PM
PHP: session variables not being passed properly Robhogg Programming 4 07-28-2007 07:38 PM
php: session variables lost while redirecting appas Programming 2 10-13-2004 09:18 AM
PHP session variables in a database rmanocha Programming 0 07-16-2004 04:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration