using sessions problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abolzouz
Forum Newbie
Posts: 13
Joined: Fri Mar 26, 2010 5:54 am

using sessions problems

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using sessions problems

Post 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?
abolzouz
Forum Newbie
Posts: 13
Joined: Fri Mar 26, 2010 5:54 am

Re: using sessions problems

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using sessions problems

Post 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.
abolzouz
Forum Newbie
Posts: 13
Joined: Fri Mar 26, 2010 5:54 am

Re: using sessions problems

Post by abolzouz »

can you give me a small example because i couldnt follow up exactly with what you said :|
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using sessions problems

Post by requinix »

Actually... I don't see the point of content.php. What is it supposed to do?
abolzouz
Forum Newbie
Posts: 13
Joined: Fri Mar 26, 2010 5:54 am

Re: using sessions problems

Post 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
Post Reply