Page 1 of 1

using sessions problems

Posted: Fri Mar 26, 2010 6:11 am
by abolzouz
hi everyone...

im trying to use sessions to pass an object from one page to another...im facing the problem of sessions not being set in the next page...
i think i know the problem but i dont know how to fix it.
the problem in my opinion is that the second page in included inside the first page using a require...thats why i think sessions are not being transfered to the second page....

this is only what i think but i might be mistaken....i will include a small code to demonstrate what im doing:

the following is the code inside a file named profile.php

Code: Select all

 
<?php
session_start();
if(isset ($_POST['username']) && isset ($_POST['password'])) {
$username = htmlentities(addslashes($_POST['username']), ENT_QUOTES);
$password = htmlentities(addslashes($_POST['password']), ENT_QUOTES);
}
require 'Objects/DB_Objects/User.php';
 require 'Objects/Service_Providers/Tools.php';
require 'Objects/Service_Providers/config.inc.php';
 
                    $userObject = login($username, md5($password));
 
                    if($userObject != false) {
                        $path = "succeed";
                        $_SESSION['userObject'] = serialize($userObject);
print "i did set the session";
                    }else {
                        $path = "failed";
                    }
                    require 'http://localhost/HR/Objects/Service_Providers/Page_Parts/User_Area/content.php?login=' . $path;
                    ?>
 
the following is the code inside content.php

Code: Select all

 
<?php
session_start();
require '../../../DB_Objects/User.php';
 
 
if(isset ($_SESSION['userObject'])) {
    print "i am in the succeed part";
    $login = "succeed";
}else {
    print "i am in the failed part";
    $login = "failed";
    }
 
if($login == "succeed") {
    if(unserialize($_SESSION['userObject']) instanceof User) {
        $user = unserialize($_SESSION['userObject']);
}else {
print "login failed";
}
 
those are the code snippets where i think my problem reside.

thanks in advance

Re: using sessions problems

Posted: Fri Mar 26, 2010 7:35 am
by requinix

Code: Select all

require 'http://localhost/HR/Objects/Service_Providers/Page_Parts/User_Area/content.php?login=' . $path;
That's a horrible idea. Never, ever, ever try to include PHP scripts over HTTP.

Any reason you have this complicated method instead of the nice one you do with the files included on lines 8-10?

Re: using sessions problems

Posted: Fri Mar 26, 2010 7:53 am
by abolzouz
tasairis wrote:

Code: Select all

require 'http://localhost/HR/Objects/Service_Providers/Page_Parts/User_Area/content.php?login=' . $path;
That's a horrible idea. Never, ever, ever try to include PHP scripts over HTTP.

Any reason you have this complicated method instead of the nice one you do with the files included on lines 8-10?
because i cant include file and give them parameters....this is the only way that works....

if you have a better idea please let me know how because i spent 1 hour trying to figure something better than this....

do you have any idea why im facing the sessions problems that my post is originally about

Re: using sessions problems

Posted: Fri Mar 26, 2010 8:26 am
by requinix
I know what the problem is and it's because of that require you have there.

Here's the point I'm trying to make:
You have three other files you include: User.php, Tools.php, and config.inc.php. You include them normally and correctly. One of them defines login() and you use that function.
So why not make this content.php define a function too. Then you can call that with whatever arguments you want - such as that user object thing.

Re: using sessions problems

Posted: Fri Mar 26, 2010 8:30 am
by abolzouz
can you give me a small example because i couldnt follow up exactly with what you said :|

Re: using sessions problems

Posted: Fri Mar 26, 2010 8:52 am
by requinix
Actually... I don't see the point of content.php. What is it supposed to do?

Re: using sessions problems

Posted: Fri Mar 26, 2010 9:16 am
by abolzouz
tasairis wrote:Actually... I don't see the point of content.php. What is it supposed to do?
content.php is supposed to fill a <div> with some info based on the GET parameter passed by the profile.php