Jump to content


Photo

PHP Session Help


  • Please log in to reply
22 replies to this topic

#1 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 01:24 AM

I have define the session in login.php, and index.php can retrieve it from login.php, however when i click a hyperlink to page1.php, i cannot get the session. i have check through but i can't find the error, below is my source code. thanks.

login.php
$this_user = new Obj_UserRole($UserID);  // UserID is taken from login username
// register sessions is in this line/
$user = $this_user;


index.php
session_start();
$user = $_SESSION["user"];
if (session_is_registered("user"))

    echo "is register";
    echo $user->user_role; //this can print out successfully !
}
else

    session_register("user");
}

<a href="page1.php">CLICK TO PAGE 1</a>

page1.php

session_start();
if (session_is_registered("user"))

    echo "is register";
    echo $user->user_role; //this CANNOT print out successfully ! I wonder why ???????
}
else

    session_register("user");
}



#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 July 2006 - 01:37 AM

echo $user->user_role; //this CANNOT print out successfully ! I wonder why

Because... whatever $user is, it is not a session variable.

#3 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 01:43 AM

but how come i can print out the dispaly in index.php ????

so how should i define the session in login.php or index.php, so that other pages also can get the session variable ? thanks thanks

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 July 2006 - 01:44 AM

Because of this...

$user = $_SESSION["user"];


#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 July 2006 - 01:45 AM

Also... be aware that the use of session_register() has long been depricated.

#6 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 01:55 AM

Because of this...

$user = $_SESSION["user"];


what's the probelm with this line ? i use this to retrive the session, should i disable it ?

#7 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 02:03 AM

login.php
$this_user = new Obj_UserRole($UserID);  // UserID is taken from login username
// register sessions is in this line/
$user = $this_user;

===============================
function Obj_UserRole($ID)
{
    $this->userid=$ID;
    // some database query here......
  // so can retrive the db result, become
  $this->user_role = $db->field('Role');
}
===========================

so when i echo something in login.php,
$user = $_SESSION["user"];
echo $user->user_role; // it works.



#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 July 2006 - 02:08 AM

what's the probelm with this line ? i use this to retrive the session, should i disable it ?

There is nothing wrong with that line! You actually need to add that line to page1.php if you expect

echo $user->user_role;

to work.

#9 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 02:15 AM

well i added the line echo $user->user_role; in page1.php, but it comes out blank page, it print nothing. :(

#10 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 July 2006 - 02:36 AM

Post your actual (relevent) code for each page.

#11 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 02:54 AM

it seems like, if i include(page1.php), when i echo $user->user_role, i can see the display.

but if i use hyperlink, the session is gone. :(

#12 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 July 2006 - 02:59 AM

Read my previous post.

#13 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 03:16 AM

<b>index.php</b>

<?
require ("config.php");

session_start();

$cur_user = $_SESSION["cur_user"];

if($action == 'logon'){ //process login
    //goes to this page if login success
  include(login.php);
}else {
    //goes to this page if login failed
  include('verify.php');
}

if (session_is_registered("cur_user")) {
      echo $cur_user->cur_role;
      echo "
";
      echo $cur_user->cur_userid; //all this cur_role and cur_userid can be displayed
}else {
      //echo '<--- is not registered  ---->';
      session_register("cur_user");
}
echo "<a href='page1.php'>page 1[/url]";

?>

<b>pro_login.php</b>

<?
if (!session_is_registered("cur_user")) session_register("cur_user");

$UserID = $_POST["UserID"];
$Password = $_POST["Password"];

    if(isset($UserID)){
            $this_user = new Obj_Role($UserID); //link to role.php, to get record
            $this_pass = $this_user->getPasswd();
            $this_status = $this_user->getStatus();
       
                    /* register sessions */
                    $cur_user = $this_user;
            }
    }
?>

<b>role.php</b>

<?
class Obj_Role
{
  // constructor method

  function Obj_Role($ID){

      $this->cur_userid=$ID;
      $db = new DB_Re;
      $tbl_name = 'User';
      $query = "SELECT * FROM $tbl_name WHERE ID = '$this->cur_userid'";
      $db->query($query);

      if($db->next_record()){
        $this->cur_passwd = $db->f('Passwd');
        $this->cur_role = $db->f('Role');
        $this->cur_domain = $db->f('Name');
      }

      $db->free();
}

?>

<b>verify.php</b>
<?
if (!session_is_registered("cur_user") || !is_Object($cur_user))
{
    echo "login failed!";
    session_destroy();
    include($Base_Temp_Dir.'logon_pg.tmp');
    print "";
    exit;
}

?>

<b>page1.php</b>

<?
session_start();
$cur_user = $_SESSION["cur_user"];

echo $_SESSION["test"];

echo $cur_user->cur_role; // this echo display nothing, this is my problem

?>

#14 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 July 2006 - 03:52 AM

Sorry... but I dont see anywhere in your code where your setting any $_SESSION variables. Keep in mind (as I said many posts ago) session_register() has long been depricated.

You might also want to look at using the header() function to force a redirect instead of uisng include() as some hack.

#15 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 25 July 2006 - 03:52 AM

Also... be aware that the use of session_register() has long been depricated.


Thats what I was going to say. There's a new way of doing it now. You just have to set the session...

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#16 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 03:56 AM

huh ? i did it there under index.php
session_start();
$cur_user = $_SESSION["cur_user"];

#17 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 03:57 AM

Also... be aware that the use of session_register() has long been depricated.


Thats what I was going to say. There's a new way of doing it now. You just have to set the session...


i have to do it in the old way as i'm chaning people's coding. i cannot simply change it to the new syntax coz it will effect the whole system.
any idea about it if i want to keep on with the old syntax?

#18 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 25 July 2006 - 04:00 AM

I mean the if (session_is_registered) is associated with the session_register() function... you could just use if (isset($_SESSION['cur_user'])); instead...

I dont know if it matters though.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#19 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 July 2006 - 04:07 AM

huh ? i did it there under index.php
session_start();
$cur_user = $_SESSION["cur_user"];

All that does is sets $cur_user to $_SESSION['cur_user']. Where do you assign a value to $_SESSION['cur_user']?

#20 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 July 2006 - 04:20 AM

it doesn't work :(






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users