Jump to content

Archived

This topic is now archived and is closed to further replies.

lilywong

PHP Session Help

Recommended Posts

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.

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


[b]index.php[/b]
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>
[b]
page1.php[/b]
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");
}

Share this post


Link to post
Share on other sites
[quote]echo $user->user_role; //this CANNOT print out successfully ! I wonder why[/quote]
Because... whatever $user is, it is [b]not[/b] a session variable.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Because of this...

[code=php:0]
$user = $_SESSION["user"];
[/code]

Share this post


Link to post
Share on other sites
Also... be aware that the use of [url=http://php.net/session_register]session_register[/url]() has long been depricated.

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=101754.msg402965#msg402965 date=1153791849]
Because of this...

[code=php:0]
$user = $_SESSION["user"];
[/code]
[/quote]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote]what's the probelm with this line ? i use this to retrive the session, should i disable it ?[/quote]
There is nothing wrong with that line! You actually need to add that line to [b]page1.php[/b] if you expect

[code=php:0]
echo $user->user_role;
[/code]

to work.

Share this post


Link to post
Share on other sites
well i added the line echo $user->user_role; in page1.php, but it comes out blank page, it print nothing. :(

Share this post


Link to post
Share on other sites
Post your actual (relevent) code for each page.

Share this post


Link to post
Share on other sites
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. :(

Share this post


Link to post
Share on other sites
Read my previous post.

Share this post


Link to post
Share on other sites
<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

?>

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=101754.msg402966#msg402966 date=1153791940]
Also... be aware that the use of [url=http://php.net/session_register]session_register[/url]() has long been depricated.
[/quote]

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

Share this post


Link to post
Share on other sites
huh ? i did it there under index.php
session_start();
$cur_user = $_SESSION["cur_user"];

Share this post


Link to post
Share on other sites
[quote author=pixy link=topic=101754.msg403004#msg403004 date=1153799578]
[quote author=thorpe link=topic=101754.msg402966#msg402966 date=1153791940]
Also... be aware that the use of [url=http://php.net/session_register]session_register[/url]() has long been depricated.
[/quote]

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

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote]huh ? i did it there under index.php
session_start();
$cur_user = $_SESSION["cur_user"];
[/quote]
All that does is sets $cur_user to $_SESSION['cur_user']. Where do you assign a value to $_SESSION['cur_user']?

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=101754.msg403010#msg403010 date=1153800447]
[quote]huh ? i did it there under index.php
session_start();
$cur_user = $_SESSION["cur_user"];
[/quote]
All that does is sets $cur_user to $_SESSION['cur_user']. Where do you assign a value to $_SESSION['cur_user']?
[/quote]

I have already assign the  $_SESSION['cur_user'] in index.php. but it still not working :(

Share this post


Link to post
Share on other sites
[quote]it doesn't work[/quote]
No kidding. Your missing the basics. Maybe go find a tutorial on how to use sessions.

Share this post


Link to post
Share on other sites
[quote]I have already assign the  $_SESSION['cur_user'] in index.php. but it still not working[/quote]
WHERE? POST THE RELEVENT CODE!

Share this post


Link to post
Share on other sites

×

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.