Jump to content

PHP Session Help


lilywong

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
https://forums.phpfreaks.com/topic/15553-php-session-help/
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
https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63209
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
https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63228
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
https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63234
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
https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63235
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
https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63237
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
https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63246
Share on other sites

Archived

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

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