Jump to content

PHP Session Help


lilywong
 Share

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");
}

Link to comment
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 ?
Link to comment
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.

Link to comment
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.
Link to comment
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

?>
Link to comment
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.
Link to comment
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...
Link to comment
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?
Link to comment
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']?
Link to comment
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 :(
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.