Jump to content

sending objects from page to page


sinista

Recommended Posts

 

I don't know what I have done, but if I try and echo the sessions var on the next page it is empty,

but I can see the variables have been created by using print_r()

 

I would seem that its easier (for me any way) to post a reference to the object around, thanks for your help though.

Link to comment
Share on other sites

echo the session var on the next page

You dont echo objects

<?php
// page1.php
session_start();
$x = new foobar();
$x->name = "John Doe";
$_SESSION['object'] = $x;
header("Location:page2.php");
exit();
?>

<?php
// page2.php
session_start();
echo $_SESSION['object']->name;
?>

Link to comment
Share on other sites

thanks neil you fixed it,

 

one other thing it doesn't work unless I include the original class (on the second page), before you start the session?

 

I was under the impression that you should start the session first if your using them?

 

Thanks again :)

 

 

Link to comment
Share on other sites

The class definition needs to exist before the session_start() so that the object can be recreated correctly.

 

I was under the impression that you should start the session first if your using them?
The only requirement is that the session_start() must come before any output is sent to the browser. Including/defining any classes/functions/settings that are used on the page can be thought of as an initialization/setup step for the page and generally should be done before the page actually does something (like a session_start() statement.)
Link to comment
Share on other sites

I got another question about objects when do they get disposed and does everything created through them die with them?

 

I have a method that set a session variable then returns a value, if I change the page I cant use the variable, so where has it gone? I've tried this with a cookie to and they both disappear.

Link to comment
Share on other sites

Both a $_SESSION variable and a class variable (with the instance of the class in a session variable) would be available when the session is resumed, assuming your session is actually being started  and resumed.

 

It would take seeing the code that is not working to be able to determine what it might be doing that is causing the symptom and are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php would help you by displaying all the errors it detects?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.